Design Question

Design Question

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


cdkisa posted on Wednesday, October 29, 2008

Hi,

I have a situation where I need to implement a common set of fields/properties that would be really useful in a base class that the majority of my objects would inherit.

The fields/properties simply are: whoCreated, whenCreated, whoLastChanged, whenLastChanged.

My question is how to implement this in the design so that I can inherit from a base class with this functionality and also use the BusinessBase<> class?

I don't want to use an interface since the logic is always the same for each instance that uses it. However, I am not discounting the fact that I may need to.

Cheers,
cdkisa

rsbaker0 replied on Wednesday, October 29, 2008

We have this exact same set of 4 fields (different names, same use)

I used a combination of an 2 interfaces and reflection. All of our BO's inherit from a class that knows how to manage the auditing functionality, but not all the BO's actually have the fields.

The first interface was empty, mainly designed just for testing the derived class to see if it wanted changes audited.

The second interface was optional is just for indicating the values of the field names if they happen to not match the default names, which was true of some of our legacy objects.

So, the common base class can manage updating of the fields when an object is created or changed, even though the properties themselves reside in the low level object.

We did this before CSLA introduced managed properties though. I suspect it would be fairly simple now to just put the properties in the common Base class.

I get the impression that inheritance is something of a can of worms with regards to the static property registration. Seach for "_dummy" for details.

ajj3085 replied on Wednesday, October 29, 2008

I just have a base class which subclasses BusinessBase<T>.  It provides a common way for all BOs to manage this information.  I also have an interface on my Linq classes to allow the BO to properly set fields in the database.

cdkisa replied on Wednesday, October 29, 2008

Thanks for the quick replies!!

To subclass would look something like this right (using generics):

public class MyBusinessBase<T> : BusinessBase<MyBusinessBase<T>>
{
    private string _whoCreated;
    private SmartDate _whenCreated;
    private string _whoLastChanged;
    private SmartDate _whoLastChanged;

    public string WhoCreated{ get; set; }

etc...
}

Cheers,

cdkisa

ajj3085 replied on Wednesday, October 29, 2008

More less that's what I have.  I actually created methods, SetCreated, SetLastModified, to properly set the created by / last modified user.  I also have helper methods for manipulating the Linq data objects as well.

Of course, you need to build what will work best for you, and only you know that. Smile [:)]

cdkisa replied on Wednesday, October 29, 2008

Thanks for your help.

rsbaker0 replied on Wednesday, October 29, 2008

This is probably better...

public class MyBusinessBase<T> : BusinessBase<T>  where T: MyBusinessBase<T>

For example, you want the "Save" method in BusinessBase<T> to return a T, not MyBusinessBase<T>.

And since this declaration constrains T to be a MyBusinessBase<T>, you have the "is a" relationship between your base and derived classes properly established so BusinessBase will accept T as a generic parameter.

Copyright (c) Marimer LLC