Generics & Object Hierarchy Question

Generics & Object Hierarchy Question

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


GEM posted on Tuesday, June 12, 2007

I'm new to CSLA, and am "kicking tires".  Simplest way to ask my basic question:

Does every CSLA business object need to directly inherit from BusinessBase(Of T), for example?

 

I have a very simple test application working with this hierarchy:

BusinessBase(Of T)

    UsrGrp (inherits BusinessBase(Of UsrGrp)

An add-to-database operation is working OK.

 

What I WANT to do is this:

BusinessBase(Of T)

    MyLocalBase(inherits BusinessBase(Of MyLocalBase)

        UsrGrp(inherits MyLocalBase)

 

MyLocalBase would exist only to contain common private variables with getters and setters as needed.  My "want-to" version of UsrGrp would be the same except for the diversion of the common private variables to MyLocalBase.

 

I have built my "want-to" version of the application, but I get a compile error on the Save() method in UsrGrp:

Public Overrides Function Save() As UsrGrp' cannot override 'Public Overridable Function Save() As Base.MyLocalBase' because they differ by their return types....

 

I get a compile to work (and successful add-to-database operation) by this code change:

    From - Public Overrides Function Save() As UsrGrp

    To     - Public Shadows Function Save() As UsrGrp

 

I've played the software game long enough to suspect that Shadows keyword may be a tool for kludge workarounds, and, sure enough, I found the following advice:

"Avoid Shadows (Visual Basic) or new (C#) keywords in favor of Overridable and virtual keywords, respectively, to redefine methods and properties in derived types."

    from "Practical Guidelines and Best Practices For Microsoft Visual Basic and Visual C# Developers" by Balena and Dimauro, page 54

 

Am I OK with the Shadows thing?  If I someday want to then extend UsrGrp, does Shadows cause a problem?

Is there something in my approach that is questionable?  A better way?

 

I looked in the forum for a bit, but didn't find this topic covered.

 

Thank you in advance!

Brian Criswell replied on Tuesday, June 12, 2007

No, you do not need to inherit the CSLA base classes directly.  It is actually encouraged that you subclass these yourself so that you can easily insert your own custom functionality for your application.  An example:

/// <summary>
/// Base class from which all objects are derived.
/// </summary>
/// <typeparam name="T">Type of the business object being defined.</typeparam>
public abstract class ObjectBase<T> : Csla.BusinessBase<T>
    where T: ObjectBase<T>
{
}

JoeFallon1 replied on Wednesday, June 13, 2007

Your base class that inherits from CSLA is recommended.

But maybe you shouldn't have the Save method in it.

Joe

 

GEM replied on Wednesday, June 13, 2007

Thank you both, Brian and Joe, for responding.

Generics are a new beast to me, and they "dumbed me down" momentarily.

GEM

Copyright (c) Marimer LLC