Entity Framework Revisited

I just spent the greater portion of today going through the new Entity Framework 4.0 and made some interesting discoveries.

I’ve mentioned some of the concepts in a past post here, but think the product of my experiment today warrants a restating.

In my efforts, and frustrations, with Nhibernate, I began looking at entity framework again, in the hopes that I can do what I wanted to do using the other tool.

I found that I not only could, but with slightly better results.

First, I started a new test domain project (EFT.Domain) and dropped in a fresh designer. That’s right, a designer. Now, while you can create your conceptual models first, and generate the database later, I already had a database, so I just imported it into the designer.

Ok, nothing special here. Seen it a thousand times right? Well here’s where it get’s interesting…

I set all the entity properties that I needed to wrap domain logic around to protected set (including the Id, back references and any domain signature properties). I also set all the collections to private get and set, and renamed them in all lowercase. With that done, I was through with the designer.

I then added partials to the project which included all the logic methods (AddForum, RemoveForum, SetCategory, etc…). I gave each class a protected no-arg constructor and a public constructor which included all the domain signature elements.

The rest I coded up pretty straight forwardly. The only other real change was in my I/Repository<T> classes. I wanted to use the same context across all repositories, so I wrote a little static class that provided it.

The overall structure and functionality then had the exact same functionality as what I had with nhibernate, with less effort. The domain is rich, inspects input, and is otherwise secure.

The only thing I could possibly complain about is the mapping being in the EFT.Domain project instead of the EFT.Persistence project, but honestly, who cares? If the schema or database vendor changes, you are going to have to open up at least one project and monkey with the mapping anyway.

In the end, I like the way this all fell together. I never really cared about poco in the first place (and I’m not sure why everybody does to be honest) but at least the domain logic parts of the classes are poco, and that’s the important part.

My thoughts for the day…

EF 4.0 is actually quite nice when you get to know it.