2012-01-12 65 views
0

我有以下程序在MEF如何在MEF組合引擎中結合a)MEF和泛型?

方法1:

public ObjectResult<PartnerListingStatement> GetCommissionListingRecords(string uRL, PortalConstant.DataSourceType DataSourceType) 
     { 

      ObjectResult<PartnerListingStatement> lstCommissionPartner = null; 

      var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString()); 

      if (dataPlugin != null) 
      { 
       lstCommissionPartner = dataPlugin.Value.GetCommissionListingRecords(uRL); 
      } 
      return lstCommissionPartner; 
     } 

方法B

public ObjectResult<CommissionEarned> GetCommissionPaidToPartners(string uRL, PortalConstant.DataSourceType DataSourceType) 
     { 
      ObjectResult<CommissionEarned> lstCommissionEarned = null; 

      var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString()); 

      if (dataPlugin != null) 
      { 
       lstCommissionEarned = dataPlugin.Value.GetCommissionPaidToPartners(uRL); 
      } 
      return lstCommissionEarned; 
     } 

使用泛型或類似物可以在這兩種組合。數據類型也不同。 注:〜這個問題是不是Generics program to access WCF service from client

感謝

回答

2

的第一個問題,詢問之後問不同的「我可以結合這些方法?」是「這些方法有什麼共同點?」我的情況下,這個問題的答案會是這樣的:

public ObjectResult<***SomeType***> GetValues(string uRL, PortalConstant.DataSourceType DataSourceType) 
{ 
    ObjectResult<***SomeType***> ret = null; 

    var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString()); 

    if (dataPlugin != null) 
    { 
     ret = dataPlugin.Value.***SomeMethod***(uRL); 
    } 
    return ret; 
} 

其中***SomeType******SomeMethod***是方法之間兩個有意義的差異。與類型的交易,使方法通用,並用通用參數替換所有***SomeType***。要處理該方法,請向該方法添加一個委託參數。根據其使用情況,代表將是Func<PluginType, string, ObjectResult<***SomeType***>>類型,其中PluginType是dataPlugin.Value的任何類型。現在您有:

public ObjectResult<T> GetValues<T>(//do come up with a better name 
    string uRL, 
    PortalConstant.DataSourceType DataSourceType, 
    Func<PluginType, string, ObjectResult<T>> resultSelector) 
{ 
    ObjectResult<T> ret = null; 

    var dataPlugin = DataPlugins.FirstOrDefault(i => i.Metadata["SQLMetaData"].ToString() == DataSourceType.EnumToString()); 

    if (dataPlugin != null) 
    { 
     ret = resultSelector(dataPlugin.Value, uRL); 
    } 
    return ret; 
} 

這是改變GetCommissionListingRecords到(泛型類型應推斷)

GetValues(uRL, DataSourceType, (p, u) => p.GetCommissionListingRecords(u)); 

和類似的其他方法。

+0

無法獲得此工作...數據pulgin是[ImportMany] public System.Lazy > [] DataPlugins {get;組; } – user1025901 2012-01-13 05:13:13

+0

在這種情況下,PluginType會出現什麼情況? – user1025901 2012-01-13 05:21:51

+0

好吧,我解決了它..我需要通過界面..感謝您的解釋 – user1025901 2012-01-13 05:44:21