0

In my controller I am calling a repository class which calls store procedures, sql...etc

i.e. Controller looks:

 Repository repo = new Repository();

 public ActionResult Index()
 {
       var getservice = repo.GetList(...);

        foreach (var servicegroup in ServicesSelected)
        {
                var Result = repo.CheckStatus(...);
          ....
        }
  }

my repository class looks like this:

   public int CheckStatus(...)
   {
        using (MyAppConnection context = new MyAppConnection())
        {
            return context.sp_web_Status(...);
        }
    }

Whenever I call a sp or a do database operation, I am surrounding within using (for implementing IDispose) and calling context.

EXAMPLE in this action it calls GetList() and then there is a loop it calls e.g. 5 times and every time It generates a new context and i dont think it is the right way to generate new context each time.

What would be the right way to use "one context" and also use "using" in each repository function to implement IDispose or should I not use "using" and create idisposable class

sample example would be much appreciated for the right approach.

UPDATE - Want to use DI framework

In My HomeController do I need to add reference to Repository class?

public class HomeController : Controller
{
     SubscriptionRepository _repo;

    public HomeController (Repository repo)
    {
        _repo = repo;
    }

Inside the Index action call repository class

    _repo.GetList(...);

Inside Repository class, do I do the same as what I did in the HomeController?

public class Repository
{
    private MyAppConnection _context;

    public Repository(MyAppConnection context)
    {
        _context = context;
    }

And then I remove the using from CheckStatus and have just

    public int CheckStatus(...)
    {
       return _context.sp_web_Status(...);
    }
}

and in the NinjectWebCommon: RegisterServices

Do I register both?

 kernel.Bind<Repository>().ToSelf().InRequestScope();
 kernel.Bind<MyAppConnection>().ToSelf().InRequestScope();

Is this the correct way to use DI framework and dispose MyAppConnection? More concern if I am doing correct in the Repository class?

2
  • 1
    Doesn't answer your question but you shouldn't be newing up repository instances in your controllers. It's best to inject them into the constructor and use a DI container like autofac or structuremap to configure them. This may help: codeproject.com/Articles/562871/… the DI container can also manage the lifetimes of these objects for you too. Commented Mar 7, 2016 at 15:27
  • anyone can confirm? if the syntax is correct? Commented Mar 11, 2016 at 6:52

2 Answers 2

2

Me personally I use a Dependency Injection (DI) framework to manage my database context and repositories and I also have one DBcontext for my web application which helps with change tracking. I find this solution much nicer than the using context pattern. Here is a really nice simple article explaining the patterns you can use

http://www.davepaquette.com/archive/2013/03/27/managing-entity-framework-dbcontext-lifetime-in-asp-net-mvc.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

I wanted to add DI and I got confused.. Do I add Repository ref in the HomeController constructor and then in the Repository constructor class also add context ref? I have updated my question, can you help me understand it. The link you provided works if I execute sp directly in the Controller. I want all my db operators to be inside the repository class Thanks
I think your best bet is to read a good book on dependency injection. I can recommend the book by Mark Seeman, DI in .net. You need to understand it before you implement it.
-1

You could wrap the whole foreach loop in a using block and then pass the context variable as an argument to the CheckStatus() method.

2 Comments

Downvotes? I know this is not a good design pattern, but it answers the question. At least explain the downvotes, please.
It wasn't me. Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.