Using ASP.NET MVC 2 and model binding with ADO.NET EntityCollections - asp.net-mvc-2

I found a great tutorial on how to use Model Binding and List, Editable Grid / List Binding in MVC2. It shows how to create objects containing lists of type List<T>. But when I use the ADO.NET entity data model I cannot make the this:
SomeEntityCollection[i]
And thereby I can not make what is done in the tutorial.
Is there a way to work around this? Maybe make the ADO.NET use lists instead, if that's possible?

The best way I have found to map from ADO.NET to models is to use AutoMapper. It's a very elegant way to formalize mapping between structures.
From their site:
AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

Related

AutoMapper - Convert two entity objects to a single DTO

I have two EntityFramework models that I want to combine into a single DTO. Is there a way to do this? There are a couple ideas in the following question, but you would either have to create a composite model, or lose the ability to call Mapper.AssertConfigurationIsValid to verify all of the properties will be set.
Is it possible to map multiple DTO objects to a single ViewModel using Automapper?
single-viewmodel-using-automappe
From my point of view, It is highly recommanded to create a composite type for merging entities. Entities are part of your Business logic or your Domain logic (depending on your architecture), whereas DTO are part of Presentation logic or Transport layer. You can create an explicit mapping that can be easily tested ; automatic mapping (create maps without options) is good for testing only. If you are using a DTO, then you will probably use it somewhere : in WCF ? as a ViewModel ?
Visual Studio and .Net Framework can manage many files and you have not to sacrifice testability or simplicity (do you know "Technical Debt"?)
Note : The role of Mapper.AssertConfigurationIsValid is to validate all mapping, generated by automatic or explicit mapping. I suggest you call this every time.

Entity Framework & Class Models in MVC

I'm new to the MVC way of developing applications and for the most part am enjoying. One thing I'm a bit confused about is the use of the Entity Framework. The EF usually (at least in my experience) defines multiple tables and relationships through the .edmx table. A couple of questions:
Why would I define a separate class file for a specific table if EF is building all of the classes that I need in the background?
From some of the validation approaches that I've seen, they want to define validation logic in the class related to a model for a table. If I'm using EF, will I have a .cs file describing the model and a .edmx describing that same table (in addition to its associated tables)?
If yes, how do you connect the .cs file to the .edmx definition so that CRUD flows easily from the EF?
Sorry if these seem like easy questions but I'm just trying to get my head wrapped around these fundamental concepts. Too many examples out there use only a single table where in my business, I NEVER write an application that uses a single table. There are always multiple tables in relation to each other with foreign keys. Thanks for your prompt responses.
For a tutorial that shows the use of partial classes -- in a Web Forms application but for MVC the same technique would be used -- see Adding Metadata to the Data Model in this tutorial:
http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-8
From your comment "The EF usually (at least in my experience) defines multiple tables and relationships through the .edmx table." it sounds like you are familiar only with Database First and Model First -- for an introduction to Code First and an explanation of the differences, followed by a series of tutorials with an MVC example using Code First, see this tutorial:
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Good questions, Darryl. Here are my responses to your bullet points:
Defining separate model classes that match the data models that EF creates is generally a good idea for the simple sake of separating your data access "stuff" from your business model objects that will get used throughout your app. Some people don't like this approach because it creates some amount of overhead when it comes to mapping your entities to POCOs but, if you use a tool such as AutoMapper, the overhead is minimal. The benefit lies in you creating a layer of separation between you and your (likely) evolving data model.
You could define validation logic in a buddy class (just a partial class that sits along-side your entity) but that would mean that you would be using that entity across your app and some would debate that that isn't the best idea. The alternative method, as mentioned above, is to create your own POCOs to mirror the entities that EF creates and place your validation attributes on the POCOs.
I mentioned this in the previous item but the way to do this would be to define buddy classes. Give EF buddy classes a Google and you should find plenty of examples on how to do that.
Just to add to all of this, if you choose to create POCO classes that mirror your EF entities, tools like AutoMapper can handle fairly complex relationships when it comes to mapping classes. So, if you have foreign key relationships in your data model, AutoMapper can understand that and map your POCO classes accordingly (i.e.: You have an entity that has a 1-to-many relationship and a POCO with a list of objects to mirror that relationship.)
I hope some of that helps...

OData / WCF Data Services / EDM - Mapping to disparate data

I'm researching OData as a RESTful interface for a database. The data is structured in a very unusual way and normal tables and rows do not apply, in fact, some stuff just exists in in-memory collections and objects.
Can I build my own arbitrary mapping system between the entities that make up 'feeds' and the sources behind, this might mean aggregating from sources and building the entities on the fly?
I'm just looking for yes/no (why not) and maybe some pointers to relevant reading material.
Many thanks
Luke
Yes and no.
You can build an OData feed of anything. In a WCF Data Service implementation of same, you can implement IDataServiceMetadataProvider.
However, the easiest way to define an EF data service is:
public class MyOData : DataService<MyObjectContext>
...and that won't work if you need to return non-entity objects. Such services are limited to entities and simple types only.
So yes, you can do it, but it's quite a bit more work than the one-liner above!

Can Entity Framework generate the DAL code?

I know that entity framework has a database first approach. Now the question is whether it can generate the DAL (data access layer) code (not the models) for me.
When using a Object Relational Mapper (ORM), you don't typically have CRUD code in the traditional sense. Rather, it abstracts those operations into more object oriented operations.
For example, you don't "insert", you add the model class to the table, then save changes. The ORM automatically generates the SQL needed to make the Object model match the data model.
So my point is, your question displays a basic lack of understanding of how ORM's work and how they relate to data models. You should probably do a little reading.
I'm not sure what you mean specifically by "DAL code", as that's a rather ambiguous term. I would consider your Entity types part of the DAL.
When you use a model-first or database-first approach, the Entity Framework tools can auto-generate a context class from your model .edmx, which will inherit from ObjectContext. It's easy to customize the generated context class with T4 templates by finding one online that already generates from a .edmx, and modifying to your liking.
Code-first development uses the DbContext, which is not typically auto-generated. Please see this post on Scott Gu's blog for more details on this.

Use of DTO in ASP.NET MVC

In the context of ASP.Net MVC 2.0, can anybody please explain why do we need to use DTO (Data Transfer Object) if there can already be Models? I have seen an example where a web service returns DTO to asp.net and then it is converted to Model using some factory class. This web service talks to database and returns data in the form of DTO.
In my previous projects, I used to communicate to DB using Data context and repository, which used to return model object to my controller. Then I used to pass this model to the corresponding view. Isn't this simpler? I cannot find out the exact use of DTO pattren.
Models represent the logical data model that your views are coded against. This may or may not map 1:1 with the source(s) of the data. In a situation where Model == DTO, I agree, the DTO is somewhat redundant.
In most situations where I've used MVC, it's been pretty rare to have a single source of data, or lack the desire to separate the logical view from the physical sources. For example I often make multiple service and database calls to build single logical model.