我有一个名为'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'的实现。您是否忘记代理它?
我该如何解决这个问题?