何时在实体框架中调用 Dispose?
在我的应用程序中,我使用 Spring.Net 进行 IoC。从 ASP.Net 文件调用服务对象以使用这些服务对象执行 CRUD 操作。例如,我有 CustomerService 对 Customer 表执行所有 CRUD 操作。我使用实体框架并注入实体..我的问题是在哪里调用 dispose 方法?
据我从 API 文档中了解到,除非我调用 Dispose(),否则不能保证它会被垃圾收集!那么我在哪里以及如何做呢?
示例服务类:
public class CustomerService
{
public ecommEntities entities = {get; set;}
public bool AddCustomer(Customer customer)
{
try
{
entities.AddToCustomer(customer);
entities.SaveChanges();
return true;
}
catch (Exception e)
{
log.Error("Error occured during creation of new customer: " + e.Message + e.StackTrace);
return false;
}
}
public bool UpdateCustomer(Customer customer)
{
entities.SaveChanges();
return true;
}
public bool DeleteCustomer(Customer customer)
.
.
.
我只是在 ASP 部分类中创建一个 CustomerService 对象并调用必要的方法。
预先感谢您提供的最佳实践和想法。
此致,
Abdel Raoof
In my application I am making use of Spring.Net for IoC. The service objects are called from the ASP.Net files to perform CRUD operations using these service object. For example, I have CustomerService to do all CRUD operations on Customer table. I use entity framework and the entities are injected .. my question is where do I call the dispose method?
As far as I understood from the API documentations, unless I call Dispose() there is no guaranty it will be garbage collected! So where and how do I do it?
Example Service Class:
public class CustomerService
{
public ecommEntities entities = {get; set;}
public bool AddCustomer(Customer customer)
{
try
{
entities.AddToCustomer(customer);
entities.SaveChanges();
return true;
}
catch (Exception e)
{
log.Error("Error occured during creation of new customer: " + e.Message + e.StackTrace);
return false;
}
}
public bool UpdateCustomer(Customer customer)
{
entities.SaveChanges();
return true;
}
public bool DeleteCustomer(Customer customer)
.
.
.
And I just create an object of CustomerService at the ASP partial class and call the necessary methods.
Thanks in advance for the best practice and ideas..
Regards,
Abdel Raoof
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的应用程序中有 HttpModule,它负责创建和处置上下文:
它在请求开始时创建它并在请求结束时处置。上下文由 Ninject 从 HttpContext.Current.Items 获取并注入到我的服务对象中。我不知道它到底是如何与 Spring.Net 一起工作的,也不知道创建 ObjectContext 的方法是什么,但我希望这个答案能有所帮助。如果 Sprint.Net 支持每个请求的生命周期并且不需要任何模块,它也可能会负责处理。在其他情况下,您可以像这里一样进行。
I have HttpModule in my application, which takes care of creating and disposing of context:
It creates it at the beginning of request and disposes at the end. Context is taken from
HttpContext.Current.Items
by Ninject and injected into my service object. I don't know how exactly it works with Spring.Net and what is your approach to creatingObjectContext
, but I hope this answer will help. If Sprint.Net supports per request lifetime and doesn't need any modules, it propably takes care of disposing too. In other case, you can do it like here.@LukLed 通常正确(+1)HTTP 请求是正确的范围。但是,我非常怀疑是否需要显式代码来使用 NInject 或 Spring.Net 调用 Dispose(),因为这两者都应该已经在处理它们管理的实例了。
HttpContext.Current
是存储引用的好地方。然而,你(@Abdel)的说法是错误的:
这是不对的。
Dispose()
使集合在某种程度上更加优化,并提供非托管资源(例如,您的数据库连接)的确定性释放。但无论是否调用 Dispose(),所有内容最终都会被收集。@LukLed is generally correct (+1) that the HTTP request is the correct scope. However, I very much doubt explicit code is required to call
Dispose()
with either NInject or Spring.Net as both of these should already be disposing instances they manage.HttpContext.Current
is as good a place to store the reference as anything.However, you (@Abdel) are wrong to say:
This is just not right.
Dispose()
makes the collection somewhat more optimal and provides for deterministic release of unmanaged resources (e.g., your DB connection). But everything will eventually be collected, with or without callingDispose()
.