BusinessBase(of T) vs. BusinessBase

BusinessBase(of T) vs. BusinessBase

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


MadGerbil posted on Wednesday, August 09, 2006

I've read a couple of posts on this topic and I think I've gained some ground but I'd like some more information.  The line Inherits BusinessBase (Of Employee) made the Mort in me wanna cry the first time I saw it.

My understanding of this line line so far:  Inherits BusinessBase(Of Employee)

1: Business base is a generic type that itself implements IBusinessObject which allows all editable business objects to have a base type of IBusinessObject.  This supports polymorphism because I could add to that interface (maybe have Employee, Student, Contact business objects) something like a 'Birthdate' and know that a collection of these various objects would all have that property.

2: (Of Employee) allows the generic type to expose the properties and methods of Employee.

What I don't understand is why a generic is being used here at all.  In the previous version of CSLA (1.0) we just inherited BusinessBase which could have an interface as well so I'm not seeing the rationale behind the use of a generic here.  I appreciate what a generic can do for a collection - strongly typed collections are a beautiful thing - but I don't understand what the use of a generic is doing for an individual business object over just having it inherit BusinessBase ala' CSLA 1.0

 

ajj3085 replied on Wednesday, August 09, 2006

The generic is used so that Save returns a strongly typed instance, instead of one of type 'object.'  Also, an implementation of Clone may return the strongly typed instance.

It saves QUITE a bit of typing, and increases peformance.  Everytime you cast, the runtime needs to check that the object may actually be cast to the specified class.  Using this technique of generics, this check is now eliminated since Save and Clone return the exact type.  Casting checks are quick, but do take up time, and eliminating the check does improve performance.

That said, there's no reason you can't just inherit Core.BusinessBase; its just prefered you inherit the generic version.

Although there are a few things only defined in BusinessBase<T> which I think could be moved down to Core.BusinessBase, such as GetIdValue, Equals, GetHashCode, and ToString.  It seems to me that if there is a good reason NOT to use BusinessBase<T> and using Core.BusinessBase, these methods should still be available.

Andy

MadGerbil replied on Wednesday, August 09, 2006

Ah...okay.

It really helps me work with the material if I know the WHY behind stuff.  I figured there was a good reason and now I know it.  Thank you very much.

ajj3085 replied on Wednesday, August 09, 2006

No problem.  Much of the why is in the book, so if you don't have it, I really recommend it.  If you haven't read it and memorized it yet... well I can't say much, I haven't finished it either... yet! Smile [:)]

Copyright (c) Marimer LLC