2013-11-15 156 views
3

我有一個名爲'MyApp.DAL'的程序集,它包含和接口IRepository。我還有另一個程序集'MyApp.Repository',其中我有更復雜的源自IRepository的存儲庫。Castle Windsor註冊一個接口

我還有服務層'MyApp.Service',其中一些服務引用'MyApp.Respository'中的複雜存儲庫以及'MyApp.DAL'中的簡單接口IRepository。

這是接口:

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 
    } 

我m使用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()); 
相關問題