【发布时间】:2015-02-24 12:24:03
【问题描述】:
我有一个 MVC 5 应用程序,它使用 EF 6 并使用 DI 容器 Ninject 通过依赖注入实现存储库模式。 dbcontext 的连接字符串存储在 EF 上下文正确找到的 Web.config 文件中。一切正常。最近,我需要在运行时确定与我的 DBContext 的连接并连接到不同的数据库(但具有完全相同的结构)。因此,在实例化存储库之前,我需要在运行时从实体连接字符串中更改 sql 连接字符串部分。我真的很感激一些帮助。我不是 DI 大师;知道足够的 Ninject 来让我的事情顺利进行。
这是我的存储库基础接口:
public interface IRepositoryBase<T> where T : class
{
void Add(T entity, string userGuid = "");
void Delete(T entity);
// ... removed other method signatures for brevity
}
我的存储库基础实现:
public abstract class RepositoryBase<D, T> : IRepositoryBase<T>, IDisposable
where T : class
where D : DbContext, new()
{
private Guid? currUserGuid = null;
private D dataContext;
protected D DataContext
{
get
{
if (dataContext == null)
dataContext = new D();
return dataContext;
}
set { dataContext = value; }
}
public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
{
return DataContext.Set<T>().Where(predicate);
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = DataContext.Set<T>();
return query;
}
public virtual void Delete(T entity)
{
OperationStatus stat = TryDelete(entity);
}
// .... removed rest for brevity
}
具体类的接口和实现:
public interface ICustomerRepository : IRepositoryBase<Customer>
{
Customer GetCustomerAndStatus( Guid custGuid );
}
public class CustomerRepository : RepositoryBase<PCDataEFContext, Customer>, ICustomerRepository
{
public Customer GetCustomerAndStatus( Guid custGuid )
{
return DataContext.Customers.Include( x => x.CustStatusType )
.SingleOrDefault( x => x.PKGuid == custGuid );
}
}
我的 Ninject 依赖解析器:
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver()
{
kernel = new StandardKernel();
AddBindings();
}
public IKernel Kernel { get { return kernel; } }
private void AddBindings()
{
kernel.Bind<ICustomerRepository>().To<CustomerRepository>();
// ... other bindings are omitted for brevity
}
}
最后,这是我的实体框架生成的 DBContext:
public partial class PCDataEFContext : DbContext
{
public PCDataEFContext()
: base("name=PCDataEFContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Customer> Customers { get; set; }
}
以上所有代码都很好用!但正如我在开始时所说,我不知道如何在运行时将连接字符串注入到我的 Repositorybase 类中,这样我就不必修改任何继承的存储库(我有很多)。有人请帮忙。
巴布。
【问题讨论】:
标签: c# asp.net-mvc-5 repository ninject