Entity Framework .NET 3.5 SP1 as DAL and Code Generation

Entity Framework .NET 3.5 SP1 as DAL and Code Generation

Old forum URL: forums.lhotka.net/forums/t/6277.aspx


g18c posted on Wednesday, January 28, 2009

Hi, I’ve used a number of free code generation tools so far and I must say that I'm very impressed with the new Microsoft Entity Framework in the recent .NET 3.5 SP1 and have used it for a number simple data access requirements.

For my more complex projects I need to add business object functionality, field validation, user rights, and abstract myself away from the database and implement objects by function. This got me thinking and out of interest I would like to ask the following points:

A lot of code generation tools that I have seen for CSLA examine databases and create the requisite business objects that are tightly coupled with the database schema. Although these tools are excellent, this in my humble opinion is no different than a generation tool for nHibernate or similar. Please don’t get me wrong, this is not a dig and I don’t think on my own I would be able to do better.

To summarise the above, should we instead not look at mapping the CSLA business object and database schema but instead map between the data access layer and the business object fields? After all, a business object may very well reference more than one table at a time if the functionality requires it. Whilst the DAL may very well be the encapsulated database schema, we are at least taking one step back and can then cleanly reference more than one DAL object lists from the same business object.

So onto my question, are there any code generation tools that attempt to do the above (rather than explicitly aiming to create the DAL also)? In my mind I envisage a generation tool  that examines the data access layer (in code or via some xml metadata specification file), lists the different classes and lists of the data access layer. The user can then create their own business object, and drag and drop the various properties from different lists into their business object. The code generation would then tie up the properties exposed in the business object to the properties in the persistence object during the data portal load and save methods. Do any of  the commercial CSLA code generation tools take the above approach?

In the limit, the business object is a wrapper to one or more lists and the properties are serialised to and from the data access object when the business object is created or saved. Whilst I want full control of what happens inside my business object it could take a lot of time writing the code for chaining up the properties of all the business objects.

An alternative to a codegen tool is if I write a utility helper/wrapper class which synchronises the business object and underlying DAL on loads and saves either with reflection or attributes. Of course what would be really sexy is CSLA and Microsoft Entity Framework as the DAL working together and a nice code generation tool to boot J.

Sorry for rambling on, I hope I got my question across and I would appreciate peoples thoughts on the above. Please be as open as you like, this is bouncing ideas of fellow developers.

Regards,

Chris

Philip replied on Thursday, January 29, 2009

That is exactly what we are currently working on at Eurowise for Code Complete. In the next release, Code Complete will be able to take as input an EF EDMX file  or an NH HBM file and will generate the apropriate mappings between your entities and you CSLA objects. The mappings will be then stored in a data source project as it does now when you point it to a database. The solution generated by Code Complete  will then use the EF or NH for the persistent layer.

Philip

g18c replied on Sunday, February 01, 2009

Hi Philip, thanks for the reply. Thats great, any idea as to when it will be available?

One thing i forgot to add, in Code Complete are you able to create custom business objects (that arent linked to a database)? This would allow BOs that are designed by function rather than database schema. It would then be nice to couple the fields of the BO to the DAL and also across multiple tables if the BO is a little more complex. Is this possible?

Regards

Chris

Philip replied on Monday, February 02, 2009

Hi Chris

We are planning a Code Complete 4.0 Beta 1 around the beginning of April. The current version does not support custom objects. It is somethning we would like to support in the future. If you include custom objects in your EDMX file, Code Complete will include these in the generation process in tne next version. We are also looking on how Code Complete can allow developers to map a CSLA object to more than one entity in an entity model.

Regards

Philip

Copyright (c) Marimer LLC