简体   繁体   English

重构方法调用具有不同参数签名的代理

[英]Refactor methods invoking delgates with different parameter signatures

Is there a pattern that I could apply to refactor this code? 是否可以使用某种模式来重构此代码? The only difference between the two methods is that that one method takes an extra parameter and passes it to the delegate? 两种方法之间的唯一区别是,一个方法需要一个额外的参数并将其传递给委托?

I found out that delegates cannot take overloaded method signatures. 我发现委托不能接受重载的方法签名。 How could I add one more level of indirection? 我如何再添加一个间接级别? :) :)

public static void ProcessFolder(
    ProcessFolderDelegate processFolderDelegate
)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processFolderDelegate(esb, contactFolder);
    }
}

public static void ProcessContact(  
    ProcessContactDelegate processContactDelegate,  
    Contact contact  //extra param
)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processContactDelegate(esb, contactFolder, contact); //extra param
    }
}
    public delegate void Action(TYPE_OF_ESB esb, TYPE_OF_CONTACT_FOLDER contact folder);
    private static void Process(Action action)
    {
        using (var esb = ExchangeService.GetExchangeServiceBinding())
        {
            var contactFolder = FolderService.GetPublicFolder(esb, Properties.Settings.Default.ExchangePublicFolderName);
            action(esb, contactfolder);
        }
    }

Process((esb, contactfolder)=>processFolderDelegate(esb, contactFolder));
Process((esb, contactfolder)=>processContactDelegate(esb, contactFolder, contact));

No, the code you posted is actually a very terse method of applying this patter. 不,您发布的代码实际上是应用此模式的一种非常简洁的方法。 You're not going to find a terser way unless there is some way of abstracting the type of method being called, or by removing all type safety, which I would not recommend 除非有某种方法可以抽象化被调用方法的类型,或者通过消除所有类型安全性,否则您将不会找到更简单的方法,我不建议这样做

public static void ProcessFolder(ProcessFolderDelegate del)
{
    Process((b, f) => del(b, f));
}

public static void ProcessContact(ProcessContactDelegate del, Contact contact)
{
    Process((b, f) => del(b, f, contact));
}

private static void Process(
    Action<ExchangeServiceBinding, ContactsFolderType> action)
{
    // i've guessed that esb is of type ExchangeServiceBinding
    // and that contactFolder is of type ContactsFolderType
    // if that's not the case then change the type parameters
    // of the Action delegate in the method signature above

    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(
            esb, Properties.Settings.Default.ExchangePublicFolderName);
        action(esb, contactFolder);
    }
}

You could use anonymous methods or lambdas like this: 您可以使用匿名方法或lambda:

delegate void ProcessDelegate<T>(T param);
.
.
public static void Process<T>(ProcessDelegate<T> processDelegate)
{
    using (var esb = ExchangeService.GetExchangeServiceBinding())
    {
        var contactFolder = FolderService.GetPublicFolder(esb,  
            Properties.Settings.Default.ExchangePublicFolderName);
        processDelegate(contactFolder);
    }
}

and then call the method like this 然后像这样调用方法

Process(contactFolder => MyMethod(esb, contactFolder));
Process(contactFolder => MyMethod(esb, contactFolder, contact));

Where MyMethod is the actual method you're calling, so you contain it within your lambda expression rather than a delegate. MyMethod是您要调用的实际方法,因此您将其包含在lambda表达式中,而不是委托中。 Think something like that may work? 认为类似的方法可能有效?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重构具有不同签名但几乎相同的主体的方法? - How do I refactor methods with different signatures but almost identical bodies? 从接收委托类型参数的方法中调用具有不同签名的方法 - Calling methods with different signatures from a method that receives a delegate type parameter C#的设计模式,并根据字符串参数调用不同的方法 - Design Pattern for C# and invoking different methods based on a string parameter 具有不同签名的控制器操作方法 - Controller Action Methods with different signatures 有许多不同的调用方法? - Many different Invoking methods? 如何结合签名略有不同的两种方法? - How to combine two methods with slightly different signatures? 传递和执行具有不同参数签名的函数 - Passing and executing functions with different parameter signatures 当方法接受不同的签名时要实现哪种工厂模式? - what kind of factory pattern to implement when methods accept different signatures? 具有不同签名,调用前后的包装方法 - Wrapping methods with different signatures, pre-and-post-invocation 有两种不同签名的方法,jquery没有调用正确的方法 - Two methods with different signatures, jquery is not calling correct method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM