我正在使用StructureMap来解析对我的存储库类的引用.我的存储库接口实现了IDisposable,例如
public interface IMyRepository : IDisposable
{
SomeClass GetById(int id);
}
Run Code Online (Sandbox Code Playgroud)
使用Entity Framework实现接口:
public MyRepository : IMyRepository
{
private MyDbContext _dbContext;
public MyDbContext()
{
_dbContext = new MyDbContext();
}
public SomeClass GetById(int id)
{
var query = from x in _dbContext
where x.Id = id
select x;
return x.FirstOrDefault();
}
public void Dispose()
{
_dbContext.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
无论如何提到我正在使用StructureMap来解析IMyRepository.那么何时,何地以及如何调用我的处理方法?
我对使用Autofac的实现中的Dispose()方法有点困惑IDisposable
说我的对象有一定的深度:
Controller取决于IManager;Manager取决于IRepository;Repository取决于ISession;ISession是IDisposable.这导致以下对象图:
new Controller(
new Manager(
new Repository(
new Session())));
Run Code Online (Sandbox Code Playgroud)
我是否还需要使我的Manager和Repository实现IDisposable,并在Controller中调用Manager.Dispose(),在Manager中调用Repository.Dispose()等,或者Autofac会自动知道我的调用堆栈中哪些对象需要正确处理?Controller对象已经是IDisposable,因为它派生自基本ASP.NET Web API控制器
.net dependency-injection idisposable inversion-of-control autofac