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?