ASP.NET MVC without Models and Entity Framework c# entity-framework model


As I prepare to convert an ASP.NET application to ASP.NET MVC, I'm considering skipping Entity Framework and Models. Instead, I'll employ techniques for direct database access.

Is it even possible in this case? What are the performance variations between the two methods?


12/31/2013 10:09:58 AM

Accepted Answer

My question here is that is it possible?

That is certainly a possibility. With one small exception: MVC is not MVC without Models. It's VC, a term I was personally unfamiliar with. neither as a framework nor as a design pattern. More like (WC:-) sounds

What is the performance difference between both ways?

There is nothing faster than raw ADO.NET. So, yes, it will be quicker than using an ORM.

Since you will still need models to map the results of your queries, you will inevitably need to write a lot more code. Do not assume that the fact that you are not utilizing EF absolves you of the obligation to utilize models. Don't believe that you'll be using DataTables, either.

In essence, your Data Layer will interact with those models. The implementation will be the sole difference.

Take this as a case study.

Create a model that will serve as a representation of the business entity you plan to use in your application:

public class Person
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime Dob { get; set; }

then specify the operations you are ready to carry out with your model in your data access contract:

public interface IPeopleRepository
    IEnumerable<Person> Get();

    ... other operations you want to perform with this model

then you could put it into practice:

public class ADOPeopleRepository: IPeopleRepository
    public IEnumerable<Person> Get()
        string connectionString = ...;
        using (var conn = new SqlConnection(connectionString))
        using (var cmd = conn.CreateCommand())
            cmd.CommandText = "SELECT id, name, age, dob FROM people";
            using (var reader = cmd.ExecuteReader())
                while (reader.Read())
                    yield return new Person
                        Id = reader.GetInt32(reader.GetOrdinal("id")),
                        Name = reader.GetString(reader.GetOrdinal("name")),
                        Age = reader.GetInt32(reader.GetOrdinal("age")),
                        Dob = reader.GetDateTime(reader.GetOrdinal("dob")),

    ... implementation of the other operations you want to perform with this model

then, as per usual, a controller to use with this repository might be available:

public class PeopleController: Controller
    private readonly IPeopleRepository repository;
    public PeopleController(IPeopleRepository repository)
        this.repository = repository;

    public ActionResult Index()
        var people = this.repository.Get().ToList();

        // Depending on the requirements of your view you might have
        // other method calls here and collect a couple of your domain models
        // that will get mapped and aggregated into a single view model
        // that will be passed to your view

        return View(people);


The last remaining step is to register theADOPeopleRepository implementation of your data access layer in a specific container of your choice.

Observe how the layers build. Now, if your present ASP.NET application was properly coded, you likely already have the implementations for the models, the interface, and the repository. Therefore, converting it to ASP.NET MVC will be simple; all you need to do is create a few view models and views.

12/31/2013 10:03:00 AM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow