简体   繁体   中英

Interface method and parameter constraints

I have an interface and would like to include a method inside it that has a class with parameter constraints. Is it in any way possible to create it in such a way that you do not need to include the constraints in the interface decleration?

public interface IPlugin
{
   void InitializeSession(MBROContext context, Reporter<TEntity, TContext> reporter);
}

TEntity is a class that inherits from IEntity. TContext is a DbContext that inherits from IDbcontext.

the signature for the reporter class is as follows:

public class Reporter<TEntity, TContext> where TEntity : class, IEntity where TContext : IDbContext, IDisposable, new()
{
    private IUnitOfWork uow;
    private IRepository<TEntity> entryRepository;
    private IService<TEntity> entryService;

    public Reporter()
    {
        this.uow = new UnitOfWork<TContext>();
        this.entryRepository = uow.GetRepository<TEntity>();
        this.entryService = new Service<TEntity>(this.uow);
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

I hope this makes sense.

Sure, you can specify a concrete type as the generic argument (that meets the given constraints). If you don't know what the concrete type will be, then no, you don't have a way around making the interface generic (or that one method, in this case, that might be the best option) and having these generic arguments use the same constraints as your Reporter type.

If that weren't the case then the constraints could be violated. If the constraints could be violated then there would be no reason to have constraints in the first place. The purpose of constraints is that they cannot be violated.

The short answer is no, you cannot. However you could specify interfaces that implements the constraints so as to not be bound to concrete implementations.

In your case for example:

public interface IPlugin
{
   void InitializeSession(MBROContext context, Reporter<IEntity, IDbContext> reporter);
}

I have resorted to the following code as a solution:-)

namespace USSDDomain.Core.Abstract.Contracts
{
    public interface IPlugin
    {
        void InitializeSession<TEntity, TContext>(MBROContext context, Reporter<TEntity, TContext> reporter) where TEntity : class,IEntity where TContext : IDbContext, IDisposable, new ();
        void Execute(MBROContext context);
        string CoreCycle(MBROContext context, int stage);
        void ProcessCycle(MBROContext context, int stage, int returnStage);
        void SendResponse(MBROContext context, string xml);
        void PrepareStage(Session session);
    }
}

This allows me to call the method in the base class like so:

base.InitializeSession(context, new Reporter<Entry, DemoDbContext>());

Thanks for taking the time to consider my problem:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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