出厂输出类型

时间:2017-02-09 18:25:34

标签: c# design-patterns dependency-injection factory

我有两个类 - ModelBuilders:每个类都使用几乎相同的方法实现一个接口:

public interface ICanBuildLocalListingsFacebookModel
{
    Task<LocalListingsFacebookModel> BuildAsync(Guid userId, int folderId, DateTime? startDate, DateTime? endDate,
        Guid? companyId);
}

 public interface ICanBuildLocalListingsGoogleMyBusinessModel
    {
        Task<LocalListingsGoogleMyBusinessModel> BuildAsync(Guid userId, int folderId, DateTime? startDate,
            DateTime? endDate, Guid? companyId);
    }

如您所见,BuildAsync几乎与输出类型相同。

我有Factory来检测ModelBuilder要调用的内容:

public class InsightsModelFactory : FactoryBase
{
    private readonly IDbSessionManager dbSessionManager;
    public InsightsModelFactory(IDbSessionManager dbSessionManager)
    {
        this.dbSessionManager = dbSessionManager;
    }

    public override dynamic Create(LocalListingEngagement? type)
    {
        switch (type)
        {
            case LocalListingEngagement.Facebook:
                return new LocalListingsFacebookModelBuilder(dbSessionManager);
            case LocalListingEngagement.GoogleMyBusiness:
                return new LocalListingsGoogleMyBusinessModelBuilder(dbSessionManager);
            default:
                throw new NotImplementedException();
        }
    }
}

public abstract class FactoryBase
{
    public abstract dynamic Create(LocalListingEngagement? type);
}

目前,在Factory中,我有一个返回Create对象的方法dynamic

因为我无法为2个接口创建一些通用类型。

有什么好的解决方案吗?

我正在考虑创建一些将返回Task<object>的常用界面,并且在特定界面中只使用关键字new隐藏该方法,但不要认为它是&#39; sa好的解决方案。

public interface ICanBuildLocalListingsGoogleMyBusinessModel: ICanBuildLocalListingsModel
{
    new Task<LocalListingsGoogleMyBusinessModel> BuildAsync(Guid userId, int folderId, DateTime? startDate,
        DateTime? endDate, Guid? companyId);
}

public interface ICanBuildLocalListingsModel
{
    Task<object> BuildAsync(Guid userId, int folderId, DateTime? startDate,
        DateTime? endDate, Guid? companyId);
}

1 个答案:

答案 0 :(得分:0)

有一个共同的模型基础。可以是接口或抽象基类

abstract class ModelBase {
    public int MyProperty1 { get; set; }
    public string MyProperty2 { get; set; }
    public Guid MyProperty3 { get; set; }
}

通用界面可以构建通用模型

public interface IModelBuilder{
    Task<BaseModel> BuildAsync(Guid userId, int folderId, DateTime? startDate, DateTime? endDate,
        Guid? companyId);
}

派生接口可以构建它,确保它们返回的模型是从基础模型派生的。

Factory将返回公共接口

public abstract class FactoryBase {
    public abstract IModelBuilder Create(LocalListingEngagement type);
}

实现可以使用商店来保存可用类型以及如何初始化实现。

public class InsightsModelFactory : FactoryBase {

    private readonly IDictionary<LocalListingEngagement, Func<IDbSessionManager, IModelBuilder>> strategies;
    private readonly IDbSessionManager dbSessionManager;

    public InsightsModelFactory(IDbSessionManager dbSessionManager) {
        this.dbSessionManager = dbSessionManager;
        strategies = new Dictionary<LocalListingEngagement, Func<IDbSessionManager, IModelBuilder>>();
        //...populate available builders or create an abstraction that can be injected.
    }

    public override IModelBuilder Create(LocalListingEngagement type) {
        Func<IDbSessionManager, IModelBuilder> stratergy;
        if (strategies.TryGetValue(type, out stratergy)) {
            return stratergy(dbSessionManager);                    
        } else {
            throw new NotImplementedException();
        }
    }
}