In MVC, does an ORM represent the model?

2020-02-10 20:30发布

问题:

In MVC, is the ORM the same as the model or just a way the model can be designed? In other words, the "model" doesn't care how you get data as long as you get it. Or, does "model" imply that I no longer have a bunch of SQL statements in my code like in code behind forms? Something else?

Thank you.

回答1:

No, the ORM is the thing that maps your model to your database and vice versa.

To elaborate, you would create your model in your code to represent the Domain Model (i.e. the various elements of your problem domain), then configure an ORM (object relational mapper) to map that to a database. I.e. Generate SQL statements that will update the database based on the model objects you give to it.

I can understand some confusion, because there are tools (LINQ to SQL being one) that actually generate model classes in a designer for you. This isn't pure ORM, like NHibernate, where you provide the ORM plain old objects and some mapping configuration that it uses (often in conjunction with reflection) to automatically generate the SQL statements for the database.



回答2:

If you'd like to have a look at a good real world implementation of MVC with an ORM, have a look at S#arp Architecture which is based on MS ASP.NET MVC, Nhibernate and the repository pattern.



回答3:

The model should be decoupled from the backend data store technology as much as possible. I thought this was a pretty good article that discusses the relationship between data access layers, DTOs, etc. http://msdn.microsoft.com/en-us/magazine/dd263098.aspx