简体   繁体   English

在ASP.NET类中处理事物

[英]Disposing of things in ASP.NET Class

I have a site which uses a C# class file, and at the top of the file, I have: 我有一个使用C#类文件的站点,在该文件的顶部,我有:

JTSEntities database = new JTSEntities(); (an ADO.NET thingy). (有点麻烦的ADO.NET)。

I have it right up the top because I don't really feel like writing the same thing over and over again. 我把它放在首位,因为我真的不喜欢一遍又一遍地写同样的东西。

But it raises one question... Because it's up the top there, how will it get disposed, and when - and how can I dispose of it (or do I need to) when the user closes the page? 但这引发了一个问题……因为它位于顶部,将如何处置,以及何时处置-以及在用户关闭页面时如何处置(或需要处置)?

It is bad practice to hold on to anything that wraps a database connection for longer than needed. 坚持将包装数据库连接的所有内容都保留超过所需时间的做法是不好的做法。

The best practice would be to release the database connection as soon as it is no longer needed for the current operation (such as populating initial values, etc.), eg something like 最佳实践是在当前操作不再需要数据库连接(例如填充初始值等)时立即释放它。

using (JTSEntities database = new JTSEntities())
{
    // Use database
}

if it implements IDisposable. 如果实现IDisposable。

If for some reason you must keep it alive for the duration of the page, be sure you call an appropriate method to release resources (.Close(), .Dispose(), etc.) in the page close event handler. 如果由于某种原因必须在页面的整个过程中保持活动状态,请确保调用适当的方法以释放页面关闭事件处理程序中的资源(.Close()、. Dispose()等)。

Implement IDisposable in this class and dispose of this object inside the Dispose() method. 在此类中实现IDisposable ,并在Dispose()方法中处置此对象。 When using this class, make sure you call Dispose when you are done (preferably with a using block) 使用此类时,请确保完成后调用Dispose(最好使用using块)

Beyond that, you don't need to worry about it. 除此之外,您无需担心。

public class MyClass : IDisposable
{
    protected JTSEntities database = new JTSEntities();

    public void Dispose() 
    {
        database.Dispose();
    }
}

// When calling this class

using(MyClass cls = new MyClass())
{
    // Do Stuff
}  // Dispose is automatically called here.

Ok so, based on the type of the attribute, we can say that it's an instance attribute . 好的,根据属性的类型,我们可以说它是一个instance attribute This means that this attribute will have the same lifecyle as the object that has it , unless its instance is passed to another object. 这意味着该属性将与具有该属性的对象具有相同的生命周期 ,除非将其实例传递给另一个对象。

If it's protected, then only the owner object and its childs (that is, objects that inherit from this class) will have access to it. 如果受保护,则只有所有者对象及其子对象(即从此类继承的对象)可以访问它。 If you don't pass it as a reference argument, then we're ok with the first statement: it will be cleaned by the GC as soon as the object is cleaned. 如果您不将其作为参考参数传递,那么我们可以接受第一个语句:一旦清除对象,它将由GC清除。

If you just use this object on a regular webform page lifecycle, then you don't have to worry with disposing it. 如果仅在常规的Webform页面生命周期中使用此对象,则不必担心对其进行处理。 The page lifecycle will do it for you. 页面生命周期将为您完成它。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM