在 mvc3 控制器上实现 Idisposable
我有一个为 EF 实例化数据库上下文的控制器。 (我确信大多数都没有实现存储库模式。)
当我对项目进行代码分析时,它建议实现 IDisposable,因此我编写了以下代码。
#region Implementation of IDisposable
public void Dispose()
{
Console.WriteLine("Dispose");
Dispose(true);
GC.SuppressFinalize(this);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
protected override void Dispose(bool disposing)
{
Console.WriteLine("Dispose(disposing)");
if (disposing)
{
// free managed resources
if (_dataService != null)
{
((IDisposable)_dataService).Dispose();
_dataService = null;
}
// free managed resources
if (_db != null)
{
((IDisposable)_db).Dispose();
_db = null;
}
}
base.Dispose(disposing);
}
#endregion
我也尝试过做。
protected new virtual void Dispose(bool disposing)
但我的 console.writeline 语句永远不会执行。我做错了什么?为什么我的控制器上没有调用 Dispose()?
I have a controller that instantiates a database context for EF. (As I'm sure most that aren't implementing the repository pattern do.)
When I ran code analysis on my project it recommended implementing IDisposable so I wrote the following code.
#region Implementation of IDisposable
public void Dispose()
{
Console.WriteLine("Dispose");
Dispose(true);
GC.SuppressFinalize(this);
}
// The bulk of the clean-up code is implemented in Dispose(bool)
protected override void Dispose(bool disposing)
{
Console.WriteLine("Dispose(disposing)");
if (disposing)
{
// free managed resources
if (_dataService != null)
{
((IDisposable)_dataService).Dispose();
_dataService = null;
}
// free managed resources
if (_db != null)
{
((IDisposable)_db).Dispose();
_db = null;
}
}
base.Dispose(disposing);
}
#endregion
I've also tried doing.
protected new virtual void Dispose(bool disposing)
But my console.writeline statements never execute. What am I doing wrong? Why isn't Dispose() being called on my controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你没有做错任何事。您会得到建议,因为您在实现 IDisposable 的类 (cotnroller) 中保留了一些字段。该框架将调用“重载”版本,因此只需将所有代码移到那里即可。
MSDN 上的详细信息
顺便说一句,您将看不到 console-writeline - 使用改为 System.Diagnostic.Debug.WriteLine
you don't do anything wrong. You get the recommentation because you hold some fields in your class (cotnroller) that implements IDisposable. The framework will call the "overload"-version so just move all your code in there.
Details on MSDN
BTW you won't see the console-writeline - use System.Diagnostic.Debug.WriteLine instead
就我个人而言,我认为在这种情况下最好的做法是将数据库上下文从控制器中分离出来,并将其移至服务层。您可以使该类实现 IDisposable 并将对服务层类的引用传递给控制器以使用它。
这样做时,您将创建一个不依赖于数据库的控制器。术语瘦控制器、胖模型适用于此。在这种情况下,服务层将充当模型。您还可以彼此隔离地对服务层和控制器(如果需要)进行单元测试
Personally I think the best thing to do in this situation is to split out the database context away from the controller and move it to a service layer. You can them make that class implement IDisposable and pass the reference to the service layer class to the controller to use it
In doing this you will create a controller that is not dependant on the database. The term skinny controllers, fat models applies here. the service layer will act as a model in this instance. You can also unit test the service layer and controller (if you want) in isolation from each other