locked
Using Entity objects with the Active Record pattern RRS feed

  • Question

  • We have noticed that the Entity Framework pattern very closely matches the Active Record pattern.  Ideally, in software design, the ViewModel should communicate with the Domain Model and not the Data Access Layer.  Has anybody had any experience using the auto-generated Entity objects as Active Records?

    If this could be done it would be a huge effort saver as the Entity classes could double as the Domain Model, otherwise, much effort would be required to wrap all the Entity classes in new Domain Model classes. 

    The problem we are running into in doing this is there does not seem to be a way to make certain data member properties have private setters.  For example assume we have a simple entity class like this below.  The fields Amount, Quantity, and Status are all database backed fields and are auto-generated by EF.  However, we would like that the Status property not have a public setter so we can validate the new value (or perform some other operations on it) before its changed.  Obviously we could just refrain from writing to this property from outside this class, but that defeats good OOP guidelines (and the use of the compiler to perform rule checks).  Is there a way we can override the default behavior and change the setter to private on Status, or is there another way we can accomplish creating Active Records from Entity objects?

    class Charge : Entity
    {
       public decimal Amount { get; set; }
       public int Quantity { get; set; }
       public string Status { get; private set; }
    
       public bool SetStatus(string newStatus)
       {
          //Validate new status or otherwise perform some
          //operation on it. Assign it if succesful
          //otherwise return false.
       }
    }

    Sunday, December 8, 2013 2:12 PM

Answers

  • Hello,

    >> We have noticed that the Entity Framework pattern very closely matches the Active Record pattern.

    Yes, they are similar, however, we cannot change the entity class to be Active Record pattern. If we do, then the class is not POCO class any more. It is designed to be contrary.

    >> Is there a way we can override the default behavior and change the setter to privateon Status

    If we change the setter to private, then we cannot update the property directly, we need to have additional expenses.

    >>or is there another way we can accomplish creating Active Records from Entity objects?

    Have a try use the AutoMapper to create DTO classes which are corresponded to entity classes, and then we can change these DTO class to active record pattern.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by dharveyg Monday, December 9, 2013 3:38 PM
    Monday, December 9, 2013 8:45 AM

All replies

  • Hello,

    >> We have noticed that the Entity Framework pattern very closely matches the Active Record pattern.

    Yes, they are similar, however, we cannot change the entity class to be Active Record pattern. If we do, then the class is not POCO class any more. It is designed to be contrary.

    >> Is there a way we can override the default behavior and change the setter to privateon Status

    If we change the setter to private, then we cannot update the property directly, we need to have additional expenses.

    >>or is there another way we can accomplish creating Active Records from Entity objects?

    Have a try use the AutoMapper to create DTO classes which are corresponded to entity classes, and then we can change these DTO class to active record pattern.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by dharveyg Monday, December 9, 2013 3:38 PM
    Monday, December 9, 2013 8:45 AM
  • Thanks for the feedback.  Using an auto mapper is an interesting approach.  At quick glance it looks like we can use it to create Active Record objects with minimal overhead.

    Monday, December 9, 2013 3:38 PM