ASP.NET MVC without models and Entity Framework

I am going to port my ASP.Net application to ASP.NET MVC, and I think to avoid Model and Entity Framework. Instead, I will use methods for direct access to databases.

My question here is, is this possible? What is the difference in performance between both ways?

Thanks.

+6
source share
2 answers

My question here is, is this possible?

Of course it is possible. With one exception: MVC without models is not MVC :-) This is a VC that I personally have never heard of. Neither as a design template, nor as a structure. Sounds more like (WC: -))

What is the difference in performance between both ways?

You cannot get anything faster than the original ADO.NET. So yes, it will be faster compared to using ORM.

Of course, you will have to write a lot more code, as you will still have models to match the results with your queries. Do not think that the fact that you are not using EF relieves you of responsibility for using models. Also do not think that you will use DataTables.

Thus, you will have your data level with these models. The only difference will be in the implementation.

Take an example.

Define a model that will represent your business entity that you intend to work with 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 define a data access agreement (aka operations that you are ready to perform with your model):

 public interface IPeopleRepository { IEnumerable<Person> Get(); ... other operations you want to perform with this model } 

then you could execute your implementation:

 public class ADOPeopleRepository: IPeopleRepository { public IEnumerable<Person> Get() { string connectionString = ...; using (var conn = new SqlConnection(connectionString)) using (var cmd = conn.CreateCommand()) { conn.Open(); 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 } 

and then, as usual, you can have a controller to work with this repository:

 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); } ... } 

All that remains now is to register a specific ADOPeopleRepository implementation of your data access level in your favorite container.

See how things get layered. Now that you have written your ASP.NET application correctly, you probably already have the models, interface, and repository implementations. Therefore, porting it to ASP.NET MVC will be part of the cake, where all you have to do is write a couple of models and views.

+27
source

Using the Entity Framework is fine, using ADO.NET may be acceptable. Yes it is possible.

If you are careful, you can make the data access code more efficient than the EF code. However, service may be less efficient because EF works a lot for you. Keep in mind that the cost of the service is very expensive.

However, I don’t understand why you want to avoid models and still use MVC. I would not recommend this approach.

+2
source

All Articles