【发布时间】:2015-01-05 12:59:37
【问题描述】:
我有两个独立的数据库用于存储文档和用户。我还实现了通用存储库模式:
public class Repository<T> : IRepository<T> where T : class
{
public DbContext Context { get; set; }
public Repository()
{
}
public IEnumerable<T> Get(Expression<Func<T, bool>> expression)
{
return Context.Set<T>().Where(expression).AsEnumerable();
}
public void Add(T entity)
{
Context.Set<T>().Add(entity);
}
public void Delete(T entity)
{
Context.Set<T>().Remove(entity);
}
public void Update(T entity)
{
Context.Set<T>().Attach(entity);
Context.Entry<T>(entity).State = EntityState.Modified;
}
public void SaveChanges()
{
Context.SaveChanges();
}
}
问题是实体存储在不同的 DbContexts 中,我不能使用这样的东西:
container.Register(Component.For(typeof(IRepository<>)).ImplementedBy(typeof(Repository<>));
如何指定每个实体应使用哪个 DbContext?
例如,如果我想创建存储库,这意味着应该使用一个数据库,但如果我想存储库,则应该使用另一个上下文。
或者我应该创建两个 repo 类,如下所示:
public class AttachmetRepository<T> : IRepository<T> where T : class
{
public AttachmetsDbContext Context { get; set; }
...
}
public class UserRepository<T> : IRepository<T> where T : class
{
public UsersDbContext Context { get; set; }
...
}
我不想使用两个不同的存储库的原因是为了保持服务简单,如下所示:
public class SomeService: ISomeService
{
public IRepository<User> UserRepository { get; set; } //database 1
public IRepository<Comment> CommentsRepository { get; set; } //database 1
public IRepository<Attachment> AttachmentRepository { get; set; } //database 2
...
}
统一更新: 正如 Ognyan 建议的那样,我使用了 FactoryMethod,这很有帮助!非常感谢,奥格尼安! 我是 CastleWindsor 的新手,我不确定这是最好和最快的方法,但这是我的代码:
public class EFDatabaseInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<AttContext>().LifeStyle.PerWebRequest);
container.Register(Component.For<DefContext>().LifeStyle.PerWebRequest);
container.Register(Component.For(typeof(IRepository<>)).UsingFactoryMethod((kernel, context) =>
{
var genericType = context.RequestedType.GetGenericArguments()[0];
Type type = typeof(Repository<>).MakeGenericType(genericType);
object repository = Activator.CreateInstance(type);
PropertyInfo dbContextProperty = type.GetProperty("Context");
if (genericType == typeof(Attachment))
{
dbContextProperty.SetValue(repository, kernel.Resolve<AttContext>());
}
else
{
dbContextProperty.SetValue(repository, kernel.Resolve<DefContext>());
}
return repository;
}).LifeStyle.PerWebRequest);
}
}
【问题讨论】:
标签: c# entity-framework castle-windsor