Usage with [ninject] (http://www.ninject.org/)
kernel.Bind<DbContext>().To<YourContext>().InRequestScope();
kernel.Bind(typeof(IRepository<>)).To(typeof(BaseRepository<>)).InRequestScope();
kernel.Bind(IUnitOfWork).To(UnitOfWork).InRequestScope();
IRepository
is a shell of DBSet<>
and contains next methods:
public interface IRepository<T>
{
T Get(Expression<Func<T, bool>> predicate);
T FirstOrDefault(Expression<Func<T, bool>> predicate);
IList<T> GetAll(Expression<Func<T, bool>> predicate);
IList<T> GetAll();
void Add(T entity);
void Delete(T entity);
IQueryable<T> GetQueryable(Expression<Func<T, bool>> predicate);
IQueryable<T> GetQueryable();
int Count(Expression<Func<T, bool>> predicate);
int Count();
}
Usage with [Ninject.Extensions.Conventions] (https://github.com/ninject/ninject.extensions.conventions)
- Use
IDependency
interface to cast services to implemented interfaces - Use
ISelfDependency
interface to cast helpers and services to self
var assemblies = new Assembly[2];
assemblies[0] = typeof(IDependency).Assembly;
assemblies[1] = assembly; //your working assembly
kernel.Bind(x => x.From(assemblies)
.SelectAllClasses()
.InheritedFrom<IDependency>()
.BindAllInterfaces()
.Configure(b => b.InRequestScope()));
kernel.Bind(x => x.From(assemblies)
.SelectAllClasses()
.InheritedFrom<ISelfDependency>()
.BindToSelf()
.Configure(b => b.InRequestScope()));
_unitOfWork.CastedContext<YourContext>();
If it's not possible to cast to type, you'll get null reference exception
If you want to use UnitOfWork with multiple contexts, you should specify each IRepository<TDbSetType, TContextType>
manually with context type:
kernel.Bind<IUnitOfWork<YourContext1>>().To<UnitOfWork<YourContext1>>().InRequestScope();
kernel.Bind<IUnitOfWork<YourContext2>>().To<UnitOfWork<YourContext2>>().InRequestScope();
kernel.Bind<IRepository<DbSetType1, YourContext1>>()
.To<BaseRepository<DbSetType1, YourContext1>>().InRequestScope();
kernel.Bind<IRepository<DbSetType2, YourContext2>>()
.To<BaseRepository<DbSetType2, YourContext2>>().InRequestScope();