簡體   English   中英

重構方法調用具有不同參數簽名的代理

[英]Refactor methods invoking delgates with different parameter signatures

是否可以使用某種模式來重構此代碼? 兩種方法之間的唯一區別是,一個方法需要一個額外的參數並將其傳遞給委托?

我發現委托不能接受重載的方法簽名。 我如何再添加一個間接級別? :)

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));

不,您發布的代碼實際上是應用此模式的一種非常簡潔的方法。 除非有某種方法可以抽象化被調用方法的類型,或者通過消除所有類型安全性,否則您將不會找到更簡單的方法,我不建議這樣做

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);
    }
}

您可以使用匿名方法或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);
    }
}

然后像這樣調用方法

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

MyMethod是您要調用的實際方法,因此您將其包含在lambda表達式中,而不是委托中。 認為類似的方法可能有效?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM