I have dilemma about how to organize connection (repository class) to data layer in my application that uses the Entity Framework.
How would you write it? And why?
public class ResearchRepository
{
public Research GetResearch (int id)
{
using (MnijenjeEntities mnijenjeContext = new MnijenjeEntities())
{
ObjectQuery<Research> researchs = mnijenjeContext.Research;
var research = (from r in researchs.Include("SurveyPaper")
where r.ResearchID == id
select r).First();
return research;
}
} //....
OR
public class ResearchRepository
{
private MnijenjeEntities mnijenjeContext;
public Research GetResearch(int id)
{
mnijenjeContext = new MnijenjeEntities();
ObjectQuery<Research> researchs = mnijenjeContext.Research;
var research = (from r in researchs.Include("SurveyPaper")
where r.ResearchID == id
select r).First();
return research;
} //....
The difference is in the dataContext
object usage.