Best solution to exclude EF 4.1 + MVC + JSON?

I use the first approach of EF 4.1 Database First, while the T4 template generates my POCO classes in a separate assembly. I have repositories for receiving data and the level of service that is used to communicate with the user interface.

I tried to create cascading dropdowns. I am new to MVC and EF 4.1, so I was looking for stackoverflow for possible solutions.

This is an example viewmodel class:

public class MyViewModel { public int CustomerId { get; set; } public string CustomerName { get; set; } public IEnumerable<Phone> Phones { get; set; } } 

What I have read so far, solutions:

  • Use ScriptIgnoreAttribute in System.Web.Script.Serialization on ScriptIgnoreAttribute properties - I don't really want to do this because I don't want to add a link to System.Web in my POCO project

  • Disable lazy loading in EF 4.1 DbContext - I'm not sure I want to use Include to complete my project

  • Returning anonymous types - will I have problems with this approach when my project gets big?

  • Use the ViewModel - suppose I have a Client that can have 1 or more phones. In the first drop-down list you can select "Customer", and in the second drop-down menu you will see all your phones.
    But won't this create a circular exception on my Phones facility? Or would I make a special class for my Phone object? This seems like a lot of unnecessary code.

  • Use AutoMapper - no experience with AutoMapper, so I don’t know how complicated it is.

What would you vote for and why?

+4
source share
1 answer

Use the view model and AutoMapper to map between your domain models and the view model that you will submit to the view. Thus, you have full control over which properties are sent to the view, which, as a result, reduces the amount of data sent between the server and the client. Also, since you are now using view models, your code is more resilient to changes in your domains. If you change them, only the mapping layer will be affected, and you will never need to touch your controllers or views.

So, my advice is to download AutoMapper, read the documentation, and start using it. It changes a life, believe me.

+5
source

All Articles