Disposing of things in ASP.NET Class - c#

I have a site which uses a C# class file, and at the top of the file, I have:
JTSEntities database = new JTSEntities(); (an ADO.NET thingy).
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.), e.g. something like
using (JTSEntities database = new JTSEntities())
{
// Use database
}
if it implements 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.

Ok so, based on the type of the attribute, we can say that it's an 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.
If you just use this object on a regular webform page lifecycle, then you don't have to worry with disposing it. The page lifecycle will do it for you.
Regards

Implement IDisposable in this class and dispose of this object inside the Dispose() method. When using this class, make sure you call Dispose when you are done (preferably with a using block)
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.

Related

C# Initialize Disposable in inherited constructor

Hey so I have a base class coming from a 3rd party dll, which is dependent on a disposable. Context: IDisposable
public class BaseValidator
{
public BaseValidator(Context context) {}
}
We're trying to move away from tying our classes to these dependencies. So we started relying on providers instead
public interface IContextProvider
{
Context Create();
}
I have a new validator that I'm writing which inherits from the BaseValidator, but I would like it to be dependent on the IContextProvider instead. So I'd like to create the context in the inherited constructor, but I would like to dispose of it in the destructor to prevent memory leaks, However I'm not sure if this is possible.
public class EntityValidator: BaseValidator
{
public EntityValidator(IContextProvider provider) : base(provider.Create())
{
}
~EntityValidator()
{
//I'm not how I can dispose the entity I've passed into it.
}
}
My question is, is there a trick I can use to Capture the variable passed into the base?
Note: I know I can make a work around with an external helper class, but I'm interested if anyone knows how to do this in a more savvy way.
If the BaseValidator class does not expose Context in a public manner, your current design would require you use reflection and knowledge of the internal implementation of BaseValidator to dispose of it, which is of course fragile.
I would instead capture the context using an intermediate constructor:
Context _context;
private EntityValidator(Context context) : base(context)
{
_context = context;
}
public EntityValidator(IContextProvider provider) : this(provider.Create())
{
}
Note, disposing via a finalizer (a.k.a. destructor) is not ideal due to constraints it places on the garbage collector. I'd instead have EntityValidator implement IDisposable
Before, in other language like C++, developpers used to rely on destructors to do a lot of cleaning. It was guaranteed by compiler so it was a strong behavior.
Smart pointer was a good pattern that used this behavior (to implement something like very basic but automatic garbage collection system).
Code was elegant but people used it for a lot of other need. With time a lot of code used to happen in destructor making the debug and readability hard.
IDisposable has been made to force developpers to write explicitely the call to Dispose... This is also useful when you want to Dispose internal resource without your object being destructed. For example in some "Close" method of Stream, where the stream is definitively closed but you can still have reference on steam... and use IsOpen property...
So for me you should not try to hide it. If you depends on code that needs to be Disposed, embrace this dependency... or chose another third party library.
You can simply make the class that need to manipulate Disposable object (BaseValidator) IDisposable too and delegate the need to call to the user.
Usually people that write class implementing IDisposable, in open source project, also write destructor (just in case someone forgot to call Dispose) This is true for a lot of class of .Net framework too (for example Pen() { where nobody thinks to call Dispose on it in some Control drawing events...)
So I would recommend :
Go get the information about if they did it with their classes, if yes they are strong probability they will keep this behavior/code forever. So you can just make your class to inherit IDisposable too, call context.Dispose in your own Dispose implement and that's enough... no need to worry because if your user forgot to call Dispose, the third party do the cleaning. Add a destructor/finalizer on your class only if you have other unmanaged resource to clean too...
Now if they did not, you can just wrap third party "Context" class in your own Context class. Your Context class will have to implement destructor that call Dispose on the instance of third party Context class. And that's it. You can even make your Context class sealed. Your Context class is here to make the behavior of your app as close as if 1) was true and implemented by third party. So this class will be easy to remove later because third party will probably implement finalizer one day (if they are serious). Doing just a sealed wrapper around just one class will avoid some complexity/issue related to how destructors (finallizers) works in .Net : They are all called in any order. Because of this underterministic behavior it makes your code hard to maintain later. For example if third party Context class's finalizer is called before your wrapping class => exception can occured and at a bad time (when gc is doing its stuff) which can make your app crash. Because of all these problems, you better go back to 1)

View is not disposed after removing it from a region

Adding Tabs to an application I realize that my child views are not disposed (finalized) after I remove them from the Region.
regionManager.Regions[regionName].Remove(tabItem.Content);
Everytime close the Tab and reopen it, a new instance is created correctly, but the old instance keeps open until I close the application. Checkt it via Finalizer Breakpoint. This causes my application not to release the Region and crash when RegionManager tries to create a region which already exists.
Even
[RegionMemberLifetime(KeepAlive = false)]
will remove it from the Region, but the object is still alive.
I had a similar issue and i just solved it this morning.
Just before adding the new Region with regionManager check if it doesn't already exist using:
this.regionManager.Regions.ContainsRegionWithName("Your region name")
Then, when i remove the view from the region, i just invoke garbage collector GC.Collect() method to "Dispose" the removed views and free memory.
However make sure that you use the attribute [RegionMemberLifetime(KeepAlive = false)] on your view.
For more information, see this post
Edit
Another solution, using Disposable pattern that i use also for some views.
Where my view implement IDisposable interface, then the method looks as the following:
public void Dispose()
{
GC.SuppressFinalize(this);
}
After that, when you remove the view from the Region, just invoke Dispose method :
myRegion.Deactivate(view);
myRegion.Remove(view);
var disposable = view as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
Note that i'm using mvvm pattern, and i don't have to free any another managed object, Otherwise, if you have some managed objects inside your view, please see more about IDisposable pattern here

Unity and TransientLifetimeManager

This is a small question, just to make sure I'm understanding Unity correctly.
I'm using Unity in an ASP.NET MVC application, and I have registered a type as follows:
container.RegisterType<IPizzaService, PizzaService>();
And I'm using it in a controller like:
public class PizzaController : Controller
{
private IPizzaService _pizzaService;
public PizzaController(IPizzaService pizzaService)
{
_pizzaService = pizzaService;
}
[HttpGet]
public ActionResult Index()
{
var pizzasModel = _pizzaService.FindAllPizzas();
...
}
}
Every time a page request is done, a new instance of IPizzaService is injected and used. So this all works fine.
My question: do I have to do anything special to dispose this instance? I assume that, once the request has ended, the controller is disposed and the PizzaService instance eventually gets garbage collected.
If I need deterministic disposal of an instance because it uses an entity framework context or an unmanaged resource for example, I have to override Dispose of the controller, and there make sure I call the dispose of the instances myself.
Right? If not, please explain why :)
Thanks!
IMO, whatever creates a disposable object is responsible for disposing it. When the container injects a disposable object via RegisterType<I, T>(), I want to guarantee that the object is ready to be used. However, using RegisterInstance<I>(obj) does dispose of your object automatically.
This can be difficult with an IOC container, and is impossible with Unity out of the box. However, there is some really nifty code out there that I use all the time:
http://thorarin.net/blog/post/2013/02/12/Unity-IoC-lifetime-management-IDisposable-part1.aspx
The blog has code for a DisposingTransientLifetimeManager and DisposingSharedLifetimeManager. Using the extensions, the container calls Dispose() on your disposable objects.
One caveat is that you'll need to reference the proper (older) version of Microsoft.Practices.Unity.Configuration.dll & Microsoft.Practices.Unity.dll.
ContainerControlledTransientManager was added in Unity on Jan 11, 2018
Add container 'owned' transient lifetime manager ContainerControlledTransientManager #37
So, ContainerControlledTransientManager is required. This lifetime manager is the same as TransientLifetimeManager except if the object implements IDisposable it will keep strong reference to object and dispose it when container is disposed.
If created object is not disposable, container does not maintain any object references so when that object is released GC will collect it immediately.

"Server failed to resume the transaction" - Best practices using LINQ to Classes

Using SQLSERVER 2008R2, VisualStudio 2010, .NET 4.0
I'm getting this periodic exception: "Server failed to resume the transaction - Desc 2000003" (number changes)
Reading online forums and here, I found the main cause is to properly instance and close the Connections.
What should my classes look like?
Create the DataContext on Class level and use it on all methods?
public class BusProcess
{
RENDBDataContext db = new RENDBDataContext();
public void Insert()
{
//Do stuff here...
db.InsertProcedure(...);
}
}
Create and dispose a new DataContext on each method through using() ?
public class BusProcess
{
public void Insert()
{
using(RENDBDataContext db = new RENDBDataContext())
{
//Do stuff here...
int sample = db.SomeObject.SingleOrDefault(...).Id;
db.InsertProcedure(...);
}
}
}
Or maybe a different approach (Best practice)
Generally speaking, the lifetime of a DataContext instance should be bound to a single unit of work -- which is exemplified by your second usage.
The first usage can also be fine, depending on how long-lived the enclosing class is -- but whatever the case there, ensure that class implements IDisposable, dispose of the enclosed DataContext in your implementation of Dispose(), and wrap usages of that class with using().
Second approach is better than having DataContext around all time. It will prevent memory consumption when calling Load etc. Also remember that connections are pooled, so creating new DataContext is not a big deal.
A combination of both. Use the first method but have your classes implement IDisposable and call dispose on the context in your classes dispose method. You then use your classes in using statements. You could also pass in a DataContext in a constructor of your class for added flexibility.

How to avoid a "DisposableFieldsShouldBeDisposedRule" defect on fields I don't want to dispose?

There is a IDisposable which contains a field for the Logger:
class DoesNotDisposeMember : IDisposable {
public IDisposable Logger { get; set; }
public void Dispose ()
{
logger = null;
}
}
Gendarme reports that there is a DisposableFieldsShouldBeDisposedRule-Defect, but I don't want to dispose the logger.
Can anyone help me?
Setting side why you wouldn't want to dispose of it; if you don't want to dispose of it, you probably should not be storing it in an IDisposable member, then. The only real purpose of that interface is to signify/enable that item being disposed.
If it's a logging object, is there not another common base interface/class you can use, such as one derived from Stream or StreamWriter?
Now that I've written that, it of course strikes me that this still includes IDisposable in the hierarchy... which I suppose brings us back to what I said I would set aside:
Why are you setting a variable here that you do not intend to dispose of? If you are disposing of it elsewhere, you should probably also use it there. The code that is wrapping the logger object should handle all the functionality of it, including exposing the separate interface to your model/business objects that enables logging.
Basically, if you are encapsulating the logging in another object, then you should not be referencing the internal logging stream object outside of that object. If you aren't encapsulating logging elsewhere, then this class should dispose of it appropriately.

Categories