How can I extend the model in ASP.NET MVC and Entity Framework?

In my first ASP.NET MVC applications, the model was a simple O / R mapping between a table and classes, managed by the Entity Framework.

Now I would like to add some meat to this skeleton and introduce business methods for the generated classes. What is recommended for this in ASP.NET MVC (with Entity Framework)? My favorite would be a solution that can also be used at the service level, without ASP.NET MVC links, so the same domain logic can also be reused in the desktop client.

Technically, I think it should be possible to extend the generated classes in such a way as to preserve additional business logic, even if O / R classes need to be updated. (This is more of a question related to the Entity Framework.)

Edit: Thank you very much for your input and information on the next version of Entity Framework (4.0). Creating two sets of classes, one oscillator to represent data in the level of stability, and the other for real business logic sounds interesting.

+4
source share
4 answers

If you use EF v1.0 right now, the Entity Framework is very intrusive into your application, which means that you cannot easily create POCO. A way to extend your model is to use a partial class. Therefore, when you update your model, the partial class you made will still be valid. The Entity Framework team understands that this is a problem and improved it in the next version (EF V4.0).

NHibernate is much more friendly and allows you to easily expand your business logic.

I really think this post blog by Jeremy D. Miller is very good at pointing out a problem.

+1
source

Inside MVC.Net, the model is the least clearly defined part. In my opinion, this is basically the rest of your application (i.e. Nothing that is not related to View or Controller). The O / R Mapping component of your application should probably also be outside the Model, as it is more of a data layer. A model must really deal with business objects and create representations of your data in order to transition to a representation.

There are many different opinions on this, but I think it’s better not to think of MVC.Net as a traditional MVC architecture.

+4
source

Tell us about your business layer in another project, and then transfer its instance to your mvc controller using something like a structure. Then you can call this business layer from your controller to get your business objects (Model) and transfer them to the user interface. This will allow you to reload your business layer in your production application.

+1
source

You can add not only meat to this project, but also clothes and style to make them seem chic. It depends on the time you have for the project. If you have time, I can offer you a look at TDD and the framework that you can use with TDDs such as Castle, NUnit, Moq, etc.

As you already mentioned, the level of service is a must for any project, but with these types of frameworks you could create your architecture more reliable.

0
source

All Articles