开发者

User-defined conversions to or from a base class and entity-framework

Using entity framework (EF) as ORM tool in a 3-t开发者_如何学运维ier project, I found entity framework generated code as DAL + a little BLL. Since the DAL and BLL are different layers in this scenario and different coders will work on each of them, there is a need for separating each layer as a different project.

The problem is I don't want changing EF generated code and still need an extra project for BLL (I'm aware of EF partial classes and On...Changing() methods but this doesn't make sense of a good separation of concepts to me and also a partial class cannot be implemented in a different project).

I wish EF would generate an interface for each entity and then implement it as the generated code. That way, I could implement those interfaces by my BLL classes. Then making changes to entities in EF designer would lead to automatically changing the interfaces and my BLL would stopped working (doesn't compile any more, since the interface has been changed). Unfortunately EF doesn't supply those interfaces and extracting them from generated code is hard to maintain since any new change to model need extracting them manually again.

Then I thought of wrapping entity framework generated classes with our own BLL classes (deriving BLL classes from EF classes) and add extra BLL logic there (validations, business rules...) and hide underlying methods and properties with BLL equivalents.

// example of a new property which facilitates using an EF object

class EFaccount // EF generated class
{
   DateTime CreationDate { get; set; }
   DateTime ExpiranDate { get; set; }
}

class BLLaccount : EFaccount // BLL class
{
   new DateTime CreationDate { get; set; }
   new DateTime ExpiranDate { get; set; }
   // Total age in days as a new property. Storing this, in dbase cause unnecessary redundancy
   int Days { get { return (ExpirationDate - CreationDate).TotalDays; } }
}

Since BLL classes are derived from their equivalent EF classes, I need casting from and to a base class which is not allowed.

In my case if I'm casting from a EF to BLL it means object is coming from dbase and extra properties can easily be calculated from base class but compiler doesn't allow casting from base. And if I'm casting from BLL to EF it means object is gonna to be stored in dbase so extra properties could be throw away but compiler doesn't allow casting to base.

What do you suggest ?


The suggestion is:

  • Use Entity framework 4
  • Use Entity Objects or preferably POCO
  • Use Entity Objects or POCO T4 template
  • Modify T4 template to add additional features for you - generating and implementing interface based on entity properties should be possible.

Argument that you don't want extra codings is ridiculous. You have already proved that with generated code you need a lot of extra codings and you have a lot of additional complications. Generated doesn't mean good. It is not easy to work with generated code if you can't modify its generation (this is possible only if you write your own custom tool for code generation). So here is clear advantage of T4 templates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜