帮助 Windsor 和存储库以及工作单元模式
我有这些接口:
public interface IUnitOfWork
{
IPersonRepository People { get; }
IBookRepository Books { get; }
int Commit();
}
public interface IBookRepository
{
Book GetBookById(int id);
IQueryable<Book> GetAllBooks();
}
public interface IPersonRepository
{
Person GetPersonById(int id);
IQueryable<Person> GetAllPeople();
}
我实现 IUnitOfWork
如下:
public class SqlUnitOfWork : IUnitOfWork
{
private readonly DbContext dbContext;
public SqlUnitOfWork()
{
dbContext = new DbContext("name=SQLContainer");
}
public IPersonRepository People
{
get { return IoC.Container.Resolve<IPersonRepository>(new { DbContext = dbContext }); }
}
public IBookRepository Books
{
get { return IoC.Container.Resolve<IBookRepository>(new { DbContext = dbContext }); }
}
public int Commit()
{
return dbContext.SaveChanges();
}
}
IBookRepository
和 IPersonRepository
的实现使用采用 DbContext
的构造函数code> 作为参数,并且此 DbContext 在 SqlUnitOfWork(上面的代码)中创建,并且我使用 Resolve 方法的重载传递此参数。
我的问题是,这是正确的方法吗?这是一个好的做法吗?
谢谢!
I have these interfaces:
public interface IUnitOfWork
{
IPersonRepository People { get; }
IBookRepository Books { get; }
int Commit();
}
public interface IBookRepository
{
Book GetBookById(int id);
IQueryable<Book> GetAllBooks();
}
public interface IPersonRepository
{
Person GetPersonById(int id);
IQueryable<Person> GetAllPeople();
}
I implement IUnitOfWork
as follow:
public class SqlUnitOfWork : IUnitOfWork
{
private readonly DbContext dbContext;
public SqlUnitOfWork()
{
dbContext = new DbContext("name=SQLContainer");
}
public IPersonRepository People
{
get { return IoC.Container.Resolve<IPersonRepository>(new { DbContext = dbContext }); }
}
public IBookRepository Books
{
get { return IoC.Container.Resolve<IBookRepository>(new { DbContext = dbContext }); }
}
public int Commit()
{
return dbContext.SaveChanges();
}
}
The implementations of IBookRepository
and IPersonRepository
uses a constructor that takes a DbContext
as a parameter, and this DbContext is created in the SqlUnitOfWork (code above) and I pass this parameter using an overload of the Resolve method.
My question is, is this the right way to do it? Is this a good practice?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 DI 容器作为服务定位器很难说是一个好的实践。除此之外,在解析接口时将
DbContext
传递给容器是一个泄漏抽象,因为这意味着您知道一些您不应该知道的具体实现。相反,我会推荐构造函数注入,它会像这样:
即使没有显式共享
DbContext
,也可以通过容器进行配置。由于此问题的上下文表明 Castle Windsor 是正在使用的容器,因此默认生命周期已经是 Singleton,因此您不必显式设置它。使用 Castle Windsor,DbContext
将自动在SqlUnitOfWork
类和两个存储库之间共享。但是,您也可以显式配置要共享的上下文,如下所示:
如果您要使用另一个 DI 容器,API 会有所不同,但概念是相同的。
额外信息:我不知道整体上下文是什么,但是如果要在 Web 应用程序中使用,并且
DbContext
是实体框架或 LINQ to SQL 上下文,正确的生命周期配置将是 PerWebRequest,因为这些上下文类都不是线程安全的:Using a DI Container as a Service Locator can hardly be said to be good practice. In addition to that, passing the
DbContext
to the container while resolving an interface is a Leaky Abstraction because it implies that you know something about the concrete implementation that you should not.Instead I would recommend Constructor Injection, which would go something like this:
Even though there's no explicit sharing of
DbContext
, this can be configured through the container. Since the context of this question indicates that Castle Windsor is the container being used, the default lifetime is already Singleton, so you don't have to explicitly set this up. With Castle Windsor, theDbContext
will automatically be shared between theSqlUnitOfWork
class and both repositories.However, you can also explicitly configure the context to be shared, like this:
If you were to use another DI Container, the API would be different, but the concept is the same.
Bonus info: I don't know what the overall context is, but if this is to be used in a web application and
DbContext
is an Entity Framework or LINQ to SQL context, the correct lifetime configuration would instead be PerWebRequest as none of those context classes are thread-safe: