0
\$\begingroup\$

I have a service class called ClientService, the service class is called using an interface IClientService. The service class simply does CRUD methods for the DbSet<Client> in my context class. In my service class constructor I have:

private MyContext context;
public ClientService()
{
    context = new MyContext();
}

I then have a void in the ClientService class that can be called from the IClientService interface called Save() that does this:

public void Save()
{
    context.SaveChanges();
    context.Dispose();
    context = new MyContext();
}

Is this safe or even necessary or am I good simply saying context.SaveChanges(); in the Save() void?

\$\endgroup\$
2
\$\begingroup\$

You shouldn't need to call Dispose and new up another context in the Save method. SaveChanges should be good enough. I would recommend you make ClientService IDisposable and when that class disposes then dispose of the context as well.

Update from comment

to implement IDisposable on the IClientService

public interface IClientService : IDisposable

The will now require a Dispose method on any class that implements IClientService. In the dispose method you should call the dispose method of the context.

\$\endgroup\$
  • \$\begingroup\$ The problem: public class ClientService : IClientService. I'm pretty new with disposing resources so how can I implement the IDisposable and my interface at the same time? \$\endgroup\$ – aaronmallen Oct 22 '14 at 20:37
  • \$\begingroup\$ @aaronmallen: A class can implement any number of interfaces. Interfaces are just contracts which specify what methods and properties will be available to be called. \$\endgroup\$ – ChrisWue Oct 22 '14 at 20:42
  • \$\begingroup\$ @ChrisWue so public class ClientService : IClientService : IDisposable ? \$\endgroup\$ – aaronmallen Oct 22 '14 at 20:44
  • \$\begingroup\$ I updated my answer with the code. If you want to make just the class IDisposable and not have it on the interface then you wouljd do public class ClientService : IClientService, IDisposable \$\endgroup\$ – CharlesNRice Oct 22 '14 at 20:45
  • \$\begingroup\$ @CharlesNRice: The IClientInteface should not derive from IDisposable - that's a leaky abstraction. \$\endgroup\$ – ChrisWue Oct 22 '14 at 21:05
2
\$\begingroup\$

Instead of creating instance of the context inside the constructor, you can instantiate the context inside the method and then use a using block for the safe disposal of resources. Like for example:

using (MyContext context = new MyContext())
{
     //do some operations here
}
\$\endgroup\$
  • \$\begingroup\$ Thats how I usually handle disposing the context but there are 6 or 7 different methods in the service class so that leads to a lot of repeated code. \$\endgroup\$ – aaronmallen Oct 23 '14 at 14:28
  • \$\begingroup\$ I agree that to some extent it does repeat the code; however to clean-up resources in a better manner, this code works the best. \$\endgroup\$ – Pankaj Nikam Oct 24 '14 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.