I have read that Entity Framework (I am using EF6) is an abstraction from the database. I am using viewmodels to serve up the data to my views so the EF entity would not go past my controller. I am using repositories with linq queries to get the data. In the controller I instantiate the repo and get the data.
So is there any reason to create a dedicated object for the transaction between the controller and the repo?
Example:
Database has a table Person.
In MVC app I create an edmx file, which creates a partial class called Person.
In my Repository:
class Person PersonRepository()
{
public Person GetPerson()
{
using (var db = new MyEntity())
{
var data = db.Person.FirstOrDefault();
var person = new Person()
{
Id = data.PersonId,
FirstName = data.FirstName,
LastName = data.LastName
};
return person;
}
}
Or do I need to do this:
class Person PersonRepository()
{
public Person GetPerson()
{
using (var db = new MyEntity())
{
var data = db.Person.FirstOrDefault();
var person = new PersonDTO()
{
Id = data.PersonId,
FirstName = data.FirstName,
LastName = data.LastName
};
return person;
}
}