2014-03-06 2 views
0

인터페이스가있어서 매개 변수 제약 조건이있는 클래스가있는 메서드를 포함하고 싶습니다. 당신은 인터페이스 decleration에 제약 조건을 포함 할 필요가 없습니다 그런 식으로 그것을 만드는 방법으로 가능한가요?인터페이스 메서드 및 매개 변수 제약

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

TEntity는 IEntity에서 상속 한 클래스입니다. TContext는 IDbcontext로부터 상속 한 DbContext입니다.

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

나는이 말이 희망을 다음과 같이

기자 클래스의 서명입니다.

답변

2

확실한 유형을 (주어진 제약 조건을 충족시키는) 일반적인 인수로 지정할 수 있습니다. 구체적인 유형이 무엇인지 알지 못하면 인터페이스를 일반적으로 만들거나 (이 경우 가장 좋은 옵션 일 수있는 한 가지 방법) 이러한 일반적인 인수를 사용하는 방법이 없습니다. Reporter 유형과 동일한 제약 조건을 사용하십시오.

그런 경우가 아니면 제약 조건을 위반할 수 있습니다. 제약이 위반 될 수 있다면 우선 제약 조건을 가질 이유가 없습니다. 제약이있는 목적을 위반할 수 없다는 것입니다.

0

짧은 대답은 아니오입니다. 그러나 구체적인 구현에 구속되지 않도록 제약 조건을 구현하는 인터페이스를 지정할 수 있습니다. 예를 들어, 귀하의 경우에는

:

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

내가

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

:-) 솔루션으로 다음 코드에 의존 한이 날과 같이 기본 클래스에서 메소드를 호출 할 수 있습니다 : 고려하는 내 문제를 시간을내어

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

감사합니다 :)

+0

이것은 정확하게 내 대답이 제안한 것입니다 ... – Servy

+0

그 이유는 그것을했습니다 : - P는 –