Since you will probably have many IReposotory<T>
implementations (for Product, Customer, Employee, etc), it's better make a single open generic registration for IRepository<T>
like this:
container.Register(typeof(IRepository<>), typeof(EFRepository<>), Lifestyle.Scoped);
Where the scoped lifestyle is defined as:
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
This registration ensures that Simple Injector will return a EFRepository<Product>
, every time a IRepository<Product>
is requested, an EFRepository<Customer>
for IRepository<Customer>
, and so on, and so on.
Since you want the same DbContext
instance to be used over all repositories within the same request, you should also register the DbContext
with the scoped Lifestyle:
container.Register<DbContext, PASContext>(Lifestyle.Scoped);
In the BLL i have a class ProductBLL and i want to get all products from the database and pass it to, lets say HomeController
In that scenario, this ProductBLL
seems like a useless abstraction to me. If all it does is passing data through, you can as easily let your HomeController
depend on IRepository<Product>
directly.