Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate service and repository layers for MVC3

I recently completed an MVC3 project for a client using a Repository pattern implemented using a micro ORM, and with a service layer for my business logic. IMHO the application works well and the client is happy.

But I had to write a lot of boilerplate code whenever I wanted to add/implement a new service and associated DTO's. Plus there was a fair amount of code duplication converting DTO's to View Models and back again, with associated PEBKAC. And as the requirements evolved there were changes to the database schema that needed to be propagated right through to the View Models.

For a different client I inherited a .netTiers code generation project that caused me a lot of grief until I fixed some defects in the base templates, automated the code generation using MSBuild and, also using MSBuild, massaged the resulting code to get it to build without the catalog of manual tweaks previously required. .netTiers ultimately generated a lot of useful code but with enormous amounts of duplication, a bucket of complexity, and it felt like using a sledgehammer to skin a cat.

Now I'm looking at another MVC3 project and I want to avoid having to write all the boilerplate myself but I also want to avoid a full .netTiers-type code generation. I haven't used EF. I've tended to think of it as too big a tool for the size of projects I undertake but if it can take away some of the manual steps for me then that would be a big time saving. What are the merits of EF and will it scaffold the service layers for me?

The other option I'm considering is LightSpeed which would require me to spend some dollars (not many) but if it can generate service layer code for me that would be money well spent. Does LightSpeed support this type of code generation?

Obviously as the domain model and database schema evolve the services need to be updated to accommodate those changes. .netTiers implements this by generating partial classes. How do these other tools handle those changes without overwriting any custom logic in the service layer?

What other options are there?

Update: Thanks for all the feedback, lots of positive options to look at. Has anyone had a look at MVC Scaffolding?

Update #2: I'm going to pursue the MVCScaffolding option which produces code for EF Code First. Out of the box it produces a Repository class and then somewhat unfortunately sticks it with the Model which in MVC is actually the View Model and not the Domain Model. There is a pull request on the MVCScaffolding project for Service layer scaffolding so will investigate that option. Plus AutoMapper for mapping POCOs <-> DTOs.

like image 824
David Clarke Avatar asked Sep 15 '11 22:09

David Clarke


1 Answers

T4 templates work very well and are easily modifiable. They can be used for the datatier layer as well as any service layers.

The process we use for EF Code Only:

  1. Build an edmx from our database
  2. Install the T4 POCO class generation templates
  3. Generate our POCO and context classes
  4. Delete our edmx and T4 templates, keeping the POCO and context classes

The new tools in Entity Framework are great as they give you various options:

Model First:

  1. Model in edmx
  2. Generate database from edmx
  3. Generate POCO classes from edmx
  4. (optional) Delete edmx, run Code Only

Code First:

  1. Write POCO classes
  2. Generate database from POCO classes
  3. (Optional) Generate edmx from POCO classes

Database First:

  1. Build database
  2. Generate edmx from database
  3. Generate POCO classes from edmx
  4. (optional) Delete edmx, run Code Only

Addendum (14/01/2012):

A beta release of Code First Migrations has been released. We haven't investigated it yet but it looks interesting.

  • Code-first Migrations - Beta Announcement
  • Code-first Migrations - No Magic Walkthrough
  • Code-first Migrations - Automatic Migrations
like image 113
Thomas Coats Avatar answered Nov 15 '22 12:11

Thomas Coats