[英]How to use Simple injector, Repository and Context - code first
I'm trying to use Simple Injector to create my repository and use it in the Business logic layer ( also i want to use PerWebRequest method ) . 我正在尝试使用Simple Injector来创建我的存储库并在业务逻辑层中使用它(我也想使用PerWebRequest方法)。
In the DAL layer i have : 在DAL层我有:
public interface IRepository<T> where T : class
{
void Add(T entity);
void Delete(T entity);
void Delete(int id);
void Update(T entity);
T GetById(int Id);
IQueryable<T> All();
IEnumerable<T> Find(Func<T, bool> predicate);
}
and : 并且:
public class EFRepository<T> : IRepository<T>, IDisposable where T : class
{
#region Members
protected DbContext Context { get; set; }
protected DbSet<T> DbSet { get; set; }
#endregion
#region Constructors
public EFRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("dbContext");
Context = dbContext;
DbSet = Context.Set<T>();
}
and my context : 和我的背景:
public class PASContext : DbContext, IDbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
public PASContext()
: base("PostAndSell")
{ }
}
As you can see EFRepository
has only one constructor that takes one argument - this is because i want to use Simple Injector to create an instance of the context and pass it to the repository while it is created . 正如您所看到的, EFRepository
只有一个带有一个参数的构造函数 - 这是因为我想使用Simple Injector创建上下文的实例,并在创建时将其传递给存储库。
In the BLL i have a class ProductBLL
and i want to get all products in that class (with some GetAll method) from the database and pass it, lets say to HomeController . 在BLL中我有一个ProductBLL
类,我想从数据库中获取该类中的所有产品(使用一些GetAll方法)并传递它,比如说HomeController。
I really need someone to talk me through this . 我真的需要有人来跟我说话。
I started by installing the right packages from the nuger (Simple Injector and Simple Injector ASP.NET Integration) 我首先从nuger安装正确的软件包(Simple Injector和Simple Injector ASP.NET Integration)
also in my global.asax.cs file, under Application_Start()
function I`ve added : 同样在我的global.asax.cs文件中,在Application_Start()
函数下我添加了:
var container = new SimpleInjector.Container();
container.RegisterPerWebRequest<IRepository<Product>, EFRepository<Product>>();
but where do i create the Context instance ? 但是我在哪里创建Context实例? and how can i access it in the business layer ? 以及如何在业务层中访问它?
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: 由于您可能会有许多IReposotory<T>
实现(对于产品,客户,员工等),因此最好为IRepository<T>
进行单一的开放式通用注册,如下所示:
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. 此注册确保Simple Injector将在每次请求IRepository<Product>
时返回EFRepository<Product>
,为IRepository<Customer>
提供EFRepository<Customer>
IRepository<Customer>
,依此类推,等等。
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: 由于您希望在同一请求中的所有存储库上使用相同的DbContext
实例,因此您还应该使用作用域生活方式注册DbContext
:
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 在BLL中我有一个ProductBLL类,我想从数据库中获取所有产品并将其传递给,让我们说HomeController
In that scenario, this ProductBLL
seems like a useless abstraction to me. 在那种情况下,这个ProductBLL
对我来说似乎是一种无用的抽象。 If all it does is passing data through, you can as easily let your HomeController
depend on IRepository<Product>
directly. 如果只是传递数据,您可以轻松地让HomeController
直接依赖IRepository<Product>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.