2016-11-28 3 views
0

제네시스 저장소로 SimpleInjector를 구현하는 데 문제가 있습니다.일반 저장소가있는 간단한 인젝터 구현

나는 인터페이스 IRepository<T> where T : class과 인터페이스를 구현하는 추상 클래스 abstract class Repository<C, T> : IRepository<T> where T : class where C : DbContext을 가지고 있습니다. 마지막으로 추상 클래스를 상속받은 엔티티 리포지토리가 있습니다. 여기 미장 예입니다 : 내 global.asax.cs 파일에서

public interface IRepository<T> where T : class 
{ 
    IQueryable<T> GetAll(); 
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate); 
    void Add(T entity); 
    void Remove(T entity); 
} 


public abstract class Repository<C, T> : IRepository<T> 
    where T : class where C : DbContext, new() 
{ 
    private C _context = new C(); 
    public C Context 
    { 
     get { return _context; } 
     set { _context = value; } 
    } 
    public virtual IQueryable<T> GetAll() 
    { 
     IQueryable<T> query = _context.Set<T>(); 
     return query; 
    } 
    ... 
} 

public class PortalRepository : Repository<SequoiaEntities, Portal> 
{ 
} 

, 위해 Application_Start() 함수에서, 나는 추가 :

Container container = new Container(); 
container.Register<IRepository<Portal>, Repository<SequoiaEntities, Portal>>(); 
container.Verify(); 

내 프로젝트를 시작

는 단순 인젝터는 컨테이너를 확인하려고 오류가 발생합니다.

추가 정보 : 주어진 유형 Repository<SequoiaEntities, Portal>은 구체적인 유형이 아닙니다. 다른 오버로드 중 하나를 사용하여이 유형을 등록하십시오.

일반 클래스로 Simple Injector를 구현하는 방법이 있습니까? 아니면 특정 클래스를 통과해야합니까?

+4

당신은 추상적하지 유형을 등록해야합니다 : 또는

container.Register<IRepository<Portal>, PortalRepository>(); 

당신이 간단한 인젝터의 일괄 등록 기능을 활용 한 호출에 모든 저장소를 등록 할 수 있습니다, 다음과 같이 구성 그러므로 보일 것입니다 그러나 마지막 하나 : PortalRepository : * container.Register , PortalRepository>(); * 추상 클래스의 문제점은 인스턴스화 할 수 없다는 것입니다. – 3615

+0

@ 3615 당신은 대답으로 넣어야합니다 – Nkosi

답변

1

Register<TService, TImpementation>() 메서드를 사용하면 지정된 서비스 (TService)가 요청되었을 때 Simple Injector에서 만들 구체적인 유형 (TImplementation)을 지정할 수 있습니다. 그러나 지정된 구현 Repository<SequoiaEntities, Portal>abstract으로 표시됩니다. 이렇게하면 Simple Injector가이를 만들 수 없습니다. 추상 클래스를 만들 수 없습니다. CLR은 이것을 허용하지 않습니다.

그러나 구체적인 유형은 PortalRepository입니다. 그 유형을 반환하는 것이 목표라고 생각합니다. ,

Assembly[] assemblies = new[] { typeof(PortalRepository).Assembly }; 

container.Register(typeof(IRepository<>), assemblies); 
+0

두 번째 경우'typeof (PortalRepository) '는 PortalRepository 클래스에 대해서만 사용되지만 모든 저장소를 등록하는 것에 대해 이야기합니까? –

+0

@DervisFindik이 예제는'PortalRepository'가있는 곳과 동일한 어셈블리 * 내의 모든 저장소의 등록을 보여줍니다. – Steven