2010-05-28 1 views
4

컨트롤러 및 저장소를 포함하는 질문 또한 공통 작업을 포함하는 기본 컨트롤러 클래스가 있고이 컨트롤러에서 상속받습니다. 레포지토리가 DI에 의해 주입됩니다. 예 :C#을 제네릭 내가 (일반적으로) 모든 공용 리포지토리 방법을 포함하는 기본 저장소 클래스가

public class UserController : BaseController<User> 
{ 
     private readonly IUserRepository userRepository; 

     public UserController (IUserRepository userRepository) 
     { 
      this.userRepository= userRepository; 
     } 

내 질문은 이것이다 : 기본 컨트롤러는 기본 저장소에 정의 된 저장소 메서드에 액세스 할 수 있어야합니다. 그러나 DI를 통해 각 컨트롤러에 다른 저장소 유형을 전달합니다 (기본 저장소에서 모든 것을 상속 함에도 불구하고). 기본 컨트롤러는 어떻게 전달되었는지 저장소에 액세스 할 수 있습니까 (형식이 무엇이든간에). 그러면 공통 기본 메서드에 액세스 할 수 있습니까?

+0

인가? –

+0

@marc 예, 죄송합니다 - 오타 (실제 코드는 괜찮습니다) - 게시물을 업데이트하겠습니다. – UpTheCreek

답변

1

당신은 BaseController

public class BaseController <T, IdType> 
{ 
    ... 
    ... 
    protected BaseRepository<T, IdType> Reposirory 
    { 
     { get; set; } 
    } 
    ... 
    ... 
} 
+0

안녕하세요 Itay, 그게 내가하려는거야 -하지만 서브 클래 싱 된 저장소에 전달할 때이 참조를 어떻게합니까? – UpTheCreek

+0

질문을 이해할 수 없습니다 –

+0

아, 미안 해요. 알았어, 고마워. – UpTheCreek

1

그것을 모든 저장소에 BaseReposiroty에 대한 참조를 보유 할 수는 IBaseRepository<T,IdType>에서 파생됩니다 다음과 같습니다

interface IUserRepository : IBaseRepository<User,int> {...} 

지금 IUserRepository에 대한 참조에 대해 알 클래스 또는 BaseRepository<> 클래스와 같은 구체적인 유형은 말할 필요도없이 IBaseRepository<> 회원입니다. 현장에있을 의미했다 ctor에의`I`는 여기에 그것을 할 수있는 한 가지 방법입니다

+0

안녕하세요 마크, 그래, 내 게시물에 표시하지 않았지만, 이미 IBaseRepository에서 IUserRepository를 상속 받고있다 . – UpTheCreek

+1

@UpTheCreek -'BaseRepository <>'(클래스)의 메소드가 필요하다면 추상화의 포인트를 인터페이스로 깨뜨린 것입니다. 만약'IBaseRepository <>'인터페이스를 사용한다면, 이미 일하고있어. –

1

..

public abstract class BaseController<TEntity, TRepository, TIdType> 
    where TEntity : class, new() 
    where TRepository : IBaseRepository<TEntity, TIdType> 
{ 
    protected TRepository Repository = RepositiryFactory.GetRepository<TEntity, TRepository>(); 

    public IList<TEntity> GetAll() 
    { 
     return Repository.GetAll().ToList(); 
    } 
    public IList<TEntity> GetAll(string sortExpression) 
    { 
     return Repository.GetAll(sortExpression).ToList(); 
    } 
    public int GetCount() 
    { 
     return Repository.GetAll().Count(); 
    } 
    public IList<TEntity> GetAll(int startRowIndex, int maximumRows) 
    { 
     var results = Repository.GetAll().Skip(startRowIndex).Take(maximumRows); 
     return results.ToList(); 
    } 
    public IList<TEntity> GetAll(string sortExpression, int startRowIndex, int maximumRows) 
    { 
     var results = Repository.GetAll(sortExpression).Skip(startRowIndex).Take(maximumRows); 
     return results.ToList(); 
    } 
} 
관련 문제