我有一个名为“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”的实现。你忘了代理吗?
我怎么解决这个问题?