jeudi 9 février 2017

Output Type in Factory

I have 2 classes - ModelBuilders: each of it implements an interface with almost the same method:

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

As you can see, BuildAsync are almost the same except output type.

I have Factory to detect what ModelBuilder to call:

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

Currently, in the Factory, I have a method Create which returns dynamic object.

It's because I can't create some common type for 2 interfaces.

Is there any good solution?


I'm thinking about creating some common interface which will return Task<object> and in specific interfaces just hide that method with keyword new, but don't think it's a good solution.

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

Aucun commentaire:

Enregistrer un commentaire