Model-to-Entity Mapper in ASP.NET
We all know we're not supposed to link our MVC2 models directly to entities in our Entity Framework. Doing so would let form hackers modify all sorts of data that you'd probably prefer to keep unmodified. Unfortunately, I haven't found good methods of copying model values to entities and vice-versa. I didn't feel like writing a million lines of code to copy each value explicitly, so I rolled a little tool that will convert a model to an entity and vice-versa using the power of reflection in C#.
Basically, you can create an entity via the Entity Framework (these are usually generated automatically by the Visual Studio IDE, though you can add functionality by writing partial classes) and a corresponding model via MVC2 and have complete control over which properties are able to be set on each form by specifying an array of property names. It's pretty simple, as you can see by the few number of lines I used to implement it.
It's simplicity is a warning, though, because this tool probably won't work in a variety of scenarios. For one, it will only copy the public properties of your model/entity; fields are ignored. For two, only shallow copies will be made of any reference types that exist in your objects. For three, nullable entity values aren't handled very well, as I can't seem to get at the attributes which define nullable values in the Entity Framework via reflection.
Anyway, enjoy. The source is below, and a short example follows.