Custom base class on top of BusinessBase<T>

Custom base class on top of BusinessBase<T>

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


phillipjohnson posted on Wednesday, February 09, 2011

I have redundant code in a couple of my classes that inherit from BusinessBase<T>

I want to put this redundant code into a class that inherits from BusinessBase<T> then inherit that class in my subclasses but I am experiencing an issue with lmbda expressions... the issue makes sense but I'm sure there must be a way around it/something I am doing wrong.

Basically if I create a custom base class like this... what should I be passing in to the business base generic type?

 

 

 

 

 

 

public

 

 

class Line500BusinessBase<T> : BusinessBase<T>{}

If I implement my custom base class like above then when I get an error on lambda expressions inside the class as follows:

private static PropertyInfo<Audits> AuditsProperty = RegisterProperty<Audits>(p => p.Audits);

The lambda expression gets a compile issue because "it cannot be cast to type.... because it is not a delegate type".

I get the issue that it doesn't know at this point what type T is, but if I pass an actual type into the BusinessBase at this point then the lambdas understandably don't work in my class that inherits from my custom business base.

How do I implement a custom base class that inherits from BusinessBase<T>

Adalton4 replied on Wednesday, February 09, 2011

Basicly, you should declare your derived class like this :

 

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

phillipjohnson replied on Wednesday, February 09, 2011

Thanks Dominiek, that works a treat :-)

rxelizondo replied on Thursday, February 10, 2011

phillipjohnson

I have redundant code in a couple of my classes that inherit from BusinessBase<T>

I want to put this redundant code into a class that inherits from BusinessBase<T> then inherit that class in my subclasses .....

 

Rocky,

I am actually kind of surprised you didn't jump in and post a little something about such technique not being a good practice and to favor composition, collaboration, containment an delegation etc :)

Unless of course you are OK with what the original poster is trying to do, which will indicate to me that I misunderstood some of the other posts you have written!

I find the extra information on your post to be very valuable, so don't hold back, plop your toughs in here,  we all benefit from your knowledge and experience, even thou I realize it can get a little tiresome Smile

So, do you support the use of inheritance for what the OP has in mind? I know its fine and it works, but do recommend this approach?

Thanks.

RockfordLhotka replied on Thursday, February 10, 2011

It depends on why you are subclassing.

One of the primary ways to customize CSLA default behaviors is to override key methods or properties in a custom base class like the one described in this thread. That is a good thing, because it is an intended mechanism by which framework behaviors can be enhanced or altered.

On the other hand, if the intent is to lump business behaviors into a base class - that's generally a bad idea. Putting properties into a base class is a horrible idea, because the resulting complexity will cost a lot more than any reuse benefit you might get. Putting methods (pure behavior) into a base class isn't quite so bad, but still isn't ideal due to the coupling that results.

So yes, for business behaviors it is far better to use collaboration or composition.

This is a well-known and widely noted OOP best practice: favor composition over inheritance.

Copyright (c) Marimer LLC