Windsor Castle 注册一个接口。

6

我有一个名为'MyApp.DAL'的程序集,它包含一个名为IRepository的接口。我还有另一个程序集'MyApp.Repository',其中有一些稍微复杂的仓储从IRepository派生而来。

我还有服务层'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
    }

我使用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());

然而,当我运行应用程序时,出现以下错误:

类型MyApp.DAL.Interfaces.IRepository1 [[MyApp.Model.Customer, MyApp.Model,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] 是抽象的。因此,无法将其实例化为 服务'MyApp.DAL.Interfaces.IRepository1'的实现。您是否忘记代理它?

我该如何解决这个问题?


你能展示一下你的IRepository<>实现是什么样子吗? 也许像ImplementedBy(typeof(RealRepository<>))这样的用法可以解决你的问题? - Vladimir
2个回答

5

ImplementedBy必须是一个具体的类,而不是接口/抽象类。

如果您想注册所有

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

5

我看到Repository<T>没有抽象成员。

如果你没有任何Repository<T>的派生类,那么它就不应该是abstract的,IoC会能够为你创建Repository<Customer>的实例。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接