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