I'm getting up to speed on CSLA and am a relative OO novice. In trying to design various parts of my application I keep coming up with a pattern for which I cannot figure out a CSLA solution. The pattern is:
- Root CSLA biz object, composed of one or more child CSLA biz objects and/or collections that implement behaviors specific to the root object.
- Subclassed CSLA biz objects, which add behaviors to the root biz object and inherit the behaviors of the root object via its child objects/collections.
To give a concrete example, assume the root object is "Entity". It holds a child collection "Roles", which holds one or more objects like "Customer", "Vender", etc. Entity is subclassed into either "Individual" or "Organization". The goal is that Individual or Organization inherits the Entity Role behaviors related to Customer and/or Vender and/or etc.
I've read many posts in the forum about related topics on using inheritance, composition, generics, and interfaces. While I understand much of it on an abstract level, when I try to write real code I keep hitting roadblocks. My head is swirling and my brain hurts. I think I'm missing the elephant in the room.
Could someone give a concrete example of how the various CSLA objects might be set up?
Thanks,
Wyatt
Here's a c# code sample on how to inherit from your base class...
[Serializable()]
public abstract class MyAppBusinessBase<T> : BusinessBase<T>
where T : MyAppBusinessBase<T>
{
// Base Class
}
---------------
[Serializable()]
public sealed partial class MyBizObjectBO : MyAppBusinessBase<MyBizObjectBO>
{
// Business Object Class which Inherits your base class
}
Hope this helps
Bayu and guyroch,
Thanks very much for your advice; you've given me new energy to try again. Let me work on it and see if I can get it to work. Will let you know how it goes.
Wyatt
Here's another helpful link...
http://www.primos.com.au/primos/Articles/CSLAversion2whatsinitforme/tabid/67/Default.aspx
I'm stuck trying to design the Role editable child, which would be held by Entity via its Roles collection property. Assuming the parent Entity is designed like guyroch suggests:
public
abstract class Entity<T> : Csla.BusinessBase<T> where T : Entity<T> { //base code }then for CRUD data access how does the child Role reference Entity, which presumably is its parent? To use Insert as an example, I've tried:
internal
void Insert(SqlConnection cn, Entity<T> parent) { // data access code here }..but the compiler doesn't like "Entity<T>". I've tried other variations to reference Entity, but they all create compiler errors.
I know I'm doing something wrong here. Any ideas?
Thanks,
Wyatt
Bayu and guyroch,
After much trial and error and pain, I finally got it working. I've learned a bit about generics in the process, though a fair amount of it still mystifies me. Many thanks for your help!
Having gone through this now, I'm curious to get your insight on something. I'm trying to use code generation (ala Ricky Supit's CodeSmith tool at Codeplex) for my business objects. I now see that using using inheritance involves a lot of hand-coding on the objects; at least it did for me. In addition to the OO mantra of preferring composition over inheritance, is there some truth that using composition in CSLA is more "codegen friendly" than using inheritance?
Wyatt
Glad to see you up and running... can you please post your working code for everybody to see :) It would be good for future readers.
Thanks
GuyRoch,
Glad to contribute; thanks for asking. Attached is a zip file with four classes I built to figure out how to use inheritance as described in this thread. The classes' members and behaviors are as simple as possible, since my focus was on how to implement generics. Class overview:
Role:
- Inherits from Csla.BusinessBase
- Has one string business property, RoleName, which would hold values like "vendor", "customer", "employee", etc.
Roles:
- Inherits from Csla.BusinessListBase
- Child to Party (see below)
- Holds the Role objects
Party:
- ("Party" is replacement name for "Entity" used earlier in the thread)
- Is abstract; inherits from BusinessBase
- Has one business property, PartyType, which holds a string value like "Person" or "Organization", describing what kind of Party it is.
- Holds a reference to Roles
Person:
- Inherits from Party, and therefore inherits Roles behavior/information
- Has one business property, FirstName
Again, the class methods are deliberately trivial since I was struggling getting generics to work. In the end it finally did, though I have to admit to being mystified by some of it.
As a question to pose to the group: Is there a way to avoid using generics with Roles and Role? Doing so seems to force client code to have to reference the concrete class, which isn't particularly intuitive to the user of the Role object. It looks like this:
_role = Role<Person>.NewRole(); // not intuitive
It would be nice to be able to simply use:
_role = Role.NewRole(); // intuitive
Thanks,
Wyatt
Copyright (c) Marimer LLC