Therefore, I never did and I have no idea, in an Asp.net mvc project, how should I proceed with this?
If you haven’t developed an Asp.net MVC project yet this one introduces the concept and has some references to help you.
...if I use Controller’s and View’s generated using scaffold, where they directly use my context, starting in Mysql...
Using your context directly you will be tied to the technology, because at every point in the code where you have reference to this technology you would need to change. Depending on its implementation it is a very arduous rework, in addition to running the risk of code duplicity and having greater difficulty to test your application.
Exemplo:
Using direct access with Nhibernate to recover data on Controller I’d go with references like Nhibernate, Nhibernate.Linq, in addition to specific objects of Nhibernate technology such as Isession, etc..
using System;
using System.Web.Mvc;
using NHibernate; //Código relacionado a tecnologia de acesso a BD
using NHibernate.Linq; //Código relacionado a tecnologia de acesso a BD
using System.Linq;
using NhibernateMVC.Models;
namespace NhibernateMVC.Controllers
{
public class ExemploController : Controller
{
public ActionResult Index()
{
//Você provavelmente iria duplicar código como esse em outros Controllers
//gerando possíveis retrabalhos
using (ISession session = NHibertnateSession.OpenSession())
{
var empregados = session.Query<Empregado>().ToList();
return View(empregados);
}
}
}
}
One alternative is to use the design pattern Repository, it centralizes its access code to the database (insertion, update, deletion and data recovery), adding a separation layer so that when changing the data access technology, you change only one layer of your application with a very small rework compared to the question of using context directly.
Given the example, in the same way, directly using specific data access technologies in your Controllers, be it Entity Framework, Mysql, X, Y, etc... you would be left with references and codes that generate rework if you change the technology.
In short, instead of creating an Asp.net MVC project and developing your application, it would be interesting for you to create at least one more project in your solution to centralize your database access code, isolating domain (business-related) objects of access code details.
In fact just by being MVC, if it is really MVC is quiet, because the structure is already prepared for this.
– Alex Schmitt
I’ve never worked with ASP. Does it have an ORM? In this case the abstraction of the ORM would help a lot, nor would it be tempting to question the MVC.
– user7261
@Andrey has several. Entity Framework is one of them.
– Leonel Sanches da Silva