简体   繁体   English

如何在MEF合成引擎中结合a)MEF和Generics?

[英]How to combine a) MEF and Generics in MEF composition engine?

I have the below program in MEF 我在MEF中有以下程序

Method 1: 方法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;
        }

Method B 方法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;
        }

Using generics or the like can these two be combined. 使用泛型等可以组合这两者。 Also the data types are different. 数据类型也不同。 NB~ This question is different than Generics program to access WCF service from client NB~这个问题与从客户端访问WCF服务的Generics程序不同

Thanks 谢谢

The first question to ask after asking "Can I combine these methods?" 在询问“我可以结合这些方法吗?”之后要问的第一个问题。 is "What do these methods have in common?" 是“这些方法有什么共同之处?” I your case, the answer to that would be something like this: 我的情况,答案是这样的:

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

where ***SomeType*** and ***SomeMethod*** are the two meaningful differences between the methods. 其中***SomeType******SomeMethod***是方法之间的两个有意义的差异。 The deal with the type, make the method generic and replace all the ***SomeType*** with the generic parameter. 处理类型,使方法通用,并用泛型参数替换所有***SomeType*** To deal with the method, add a delegate parameter to the method. 要处理该方法,请向该方法添加委托参数。 Based on its usage, the delegate will be of the Func<PluginType, string, ObjectResult<***SomeType***>> type where PluginType is whatever type dataPlugin.Value is. 根据其使用情况,委托将是的Func<PluginType, string, ObjectResult<***SomeType***>>类型,其中PluginType是任何类型dataPlugin.Value是。 Now you have: 现在你有:

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

which is changes GetCommissionListingRecords to (the generic type should be inferred) 这是将GetCommissionListingRecords更改为(应该推断泛型类型)

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

and similarly for the other method. 和其他方法类似。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM