2013-11-15 5 views
3

'MyApp.DAL'이라는 어셈블리가 있으며 IRepository 인터페이스가 포함되어 있습니다. 또한 IRepository에서 파생 된 좀 더 복잡한 저장소가있는 또 다른 어셈블리 'MyApp.Repository'도 있습니다.Castle Windsor 인터페이스를 등록하십시오.

일부 서비스에는 'MyApp.Respository'의 복잡한 리포지토리와 'MyApp.DAL'의 IRepository라는 단순 인터페이스를 참조하는 'MyApp.Service'서비스 레이어가 있습니다.

는 인터페이스 :

public interface IRepository<T> where T : class 
{ 
    void Add(T entity); 
    void Update(T entity); 
    void Delete(T entity); 
    T GetById(long Id); 
} 

그리고 이것은 구현 :

public abstract class Repository<T> : IRepository<T> where T : class 
{ 
    private MyContext dataContext; 
    private readonly IDbSet<T> dbset; 

    protected Repository(IDatabaseFactory databaseFactory) 
    { 
     DatabaseFactory = databaseFactory; 
     dbset = DataContext.Set<T>(); 
    } 

    protected IDatabaseFactory DatabaseFactory 
    { 
     get; 
     private set; 
    } 

    protected MyContext DataContext 
    { 
     get { return dataContext ?? (dataContext = DatabaseFactory.Get()); } 
    } 

    public virtual void Add(T entity) 
    { 
     dbset.Add(entity); 
    } 

    public virtual void Update(T entity) 
    { 
     dbset.Attach(entity); 
     dataContext.Entry(entity).State = EntityState.Modified; 
    } 

    public virtual void Delete(T entity) 
    { 
     dbset.Remove(entity); 
    } 


    public virtual T GetById(long id) 
    { 
     return dbset.Find(id); 
    } 
} 

그리고 이것은 '서비스'레이어의 서비스 : 나는 '

public interface ICustomerService : IUpdateableService<Customer> 
{ 
    List<Customer> GetCustomers(); 
} 

public class CustomerService : ICustomerService 
{ 
    private IUnitOfWork unitOfWork; 
    private IRepository<Customer> customerRepository; 

    public CustomerService(IUnitOfWork unitOfWork, IRepository<Customer> customerRepository) 
    { 
     this.unitOfWork = unitOfWork; 
     this.customerRepository = customerRepository; 
    } 
    public List<Customer> GetCustomers() 
    { 
     return customerRepository.GetAll().ToList(); 
    } 

    public CustomerGet(int id) 
    { 
     return customerRepository.GetById(id); 
    } 

    public int Save(Customer customer) 
    { 
     //TODO 
    } 

    public bool Delete(Customer customer) 
    { 
     //TODO 
    } 

Castle Windsor를 사용하여 다음과 같이 유형을 등록하십시오.

 container.Register(
        Component.For(typeof(IRepository<>)) 
         .ImplementedBy(typeof(IRepository<>)) 
         .LifeStyle.PerWebRequest, 

        Classes.FromAssemblyNamed("MyApp.Repository") 
         .Where(type => type.Name.EndsWith("Repository")) 
         .WithServiceAllInterfaces() 
         .LifestylePerWebRequest()); 
내가 응용 프로그램을 실행할 때

그러나, 나는 다음과 같은 오류가 는 :

Type MyApp.DAL.Interfaces.IRepository1[[MyApp.Model.Customer, MyApp.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] is abstract. As such, it is not possible to instansiate it as implementation of service 'MyApp.DAL.Interfaces.IRepository1'. Did you forget to proxy it?

는이 문제를 어떻게 해결할 수 있습니까?

+0

IRepository <> 구현이 어떻게 보이는지 보여 줄 수 있습니까? ImplementedBy (typeof (RealRepository <>))와 같은 일부 사용법이 문제를 해결할 수 있습니까? – pil0t

답변

3

나는 Repository<T> 더 추상적 인 멤버가없는 것을 알 수있다.

Repository<T>의 파생 클래스가없는 경우 abstract이 아니어야하며 IoC는 Repository<Customer>의 인스턴스를 만들 수 있습니다.

container.Register(
    Component.For(typeof(IRepository<>)) 
     .ImplementedBy(typeof(Repository<>)) 
     .LifestylePerWebRequest()); 
5

ImplementedBy는 구체적인 클래스 여야하며 인터페이스/추상 클래스가 아니어야합니다.

당신이 IRepository<>의 모든 구현을 등록 할 경우 쓸 수 있습니다 :

container.Register(
    Classes.FromAssemblyNamed("MyApp.Repository") 
      .BasedOn(typeof (IRepository<>)) 
      .WithService.Base() 
      .LifestylePerWebRequest());