Interface Inheritance Issues

I have IServiceHandlerand ISalesHandlerand both are inherited fromIhandler

IHandler.cs

public interface IHandler
{
    Task AddAsync(IEnumerable<IDictionary<string, object>> toAdd, int dataFileId);
    Task AddAuditAsync(IEnumerable<IDictionary<string, object>> toAdd, int dataFileId);
}

IServiceHandler.cs

public interface IServiceHandler : IHandler
{
    Task<IEnumerable<ACService>> GetAsync();
    Task<IEnumerable<ACServiceAudit>> GetAuditAsync();
}

ISalesHandler.cs

public interface ISalesHandler : IHandler
{
    Task<IEnumerable<ACSale>> GetAsync();
    Task<IEnumerable<ACSaleAudit>> GetAuditAsync();
}

Then I have a method that returns Sales or Service, but the problem is that I return it as Ihandler

private IHandler CreateHandler(FileType type)
{
    switch (type)
    {
        case FileType.Sales:
            return  _container.GetExportedValue<ISalesHandler>("Sales");
        case FileType.Service:
            return _container.GetExportedValue<IServiceHandler>("Service");
        case FileType.None:
            return null;
    }
    return null;
}

which makes me access methods in Ihandler, not in IServiceHandleror ISalesHandler.

How can I structure interfaces to access all methods? I would rather save the method CreateHandler.

+4
source share
2 answers

Indeed, you are returning IHandler, so the methods available there are available without casting.

, , , , .

?

IHandler.cs:

public interface IHandler // I usually split the generic and non-generic methods
{
    Task AddAsync(IEnumerable<IDictionary<string, object>> toAdd, int dataFileId);
    Task AddAuditAsync(IEnumerable<IDictionary<string, object>> toAdd, int dataFileId);
}

public interface IHandler<TService, TServiceAudit> : IHandler
{
    Task<IEnumerable<TService>> GetAsync();
    Task<IEnumerable<TServiceAudit>> GetAuditAsync();
}

IServiceHandler.cs:

public interface IServiceHandler : IHandler<ACService, ACServiceAudit>
{ }

ISalesHandler.cs:

public interface ISalesHandler : IHandler<ACSale, ACSaleAudit>
{ }
+4

?

public interface IServiceHandler<TSale, TAudit> : IHandler
{
    Task<IEnumerable<TSale>> GetSaleAsync();
    Task<IEnumerable<TAudit>> GetAuditAsync();
}

GetAsync() GetSaleAsync(), , . , Factory :

private IServiceHandler<TSale, TAudit>CreateHandler(FileType type)
+3

All Articles