Building a tree in CSLA

Building a tree in CSLA

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


sixeyes posted on Tuesday, June 24, 2008

I'm just starting out with CSLA. I'm using C# 3.0 and I've read the 2005 book and the ebooks but I am still struggling.

I'm trying to create a tree view consisting of three levels (Groups, Prototypes and Samples). I decided to create a base class (I'm using the PropertyInfo<P> stuff but removed it here for clarity):

public abstract class NodeItem : ReadOnlyBase<NodeItem>
{
    public int Id;
    public string Name;
    public bool HasChildren;
    abstract protected List<NodeItem> GetChildren();
}

The idea is to create derived classes for each different type of node in the tree. There are two types of group, Favourites and Filtered but only one type of Prototype and Sample.

So I was going to do:

public class FavouriteNodeItem : NodeItem
public class FilteredNodeItem : NodeItem

GetChildren() for these classes would return a List<PrototypeNodeItem>

public class PrototypeNodeItem : NodeItem

GetChildren() for this class would return a List<SampleNodeItem>

public class SampleNodeItem : NodeItem

GetChildren() for this class would return an empty list and HasChildren() would always be false.

This is how I've done trees in C++. However I'm wondering how this fits in with ReadOnlyListBase?

Do I create a ROLB for each different node type and load them in GetChildren() or do I remove GetChildren() from NodeItem class and require the UI to call the correct ROLB?

Iain

tiago replied on Wednesday, June 25, 2008

http://www.codeproject.com/KB/recipes/CslaGen.aspx is a CslaGen project that builds a tree of mothers and daughters. By the way, using CslaGen is a great way of learning Csla.

Tiago

vdhant replied on Friday, June 27, 2008

If i understand what you are after correctly, generics (i.e. templates) will be able to do what you want.

To implement this the NodeItem, the class will look something like the below;

public abstract class NodeItem[T] : ReadOnlyBase[T]
where T : NodeItem[T]
{
.....
abstract protected List[T] GetChildren();
}

If you do this you will be able to get the object structure you want. See below;

public class FavouriteNodeItem : NodeItem[PrototypeNodeItem] {...}
public class FilteredNodeItem : NodeItem[PrototypeNodeItem] {...}

public class PrototypeNodeItem : NodeItem[SampleNodeItem] : {...}

public class SampleNodeItem : NodeItem[SampleNodeItem] : {...}

Hope this helps.
Anthony

PLEASE NOTE -- I have had to use square brackets (i.e. [ and ]) as my browser is playing up when i use diamond brackets (i am currently on a mac)

Copyright (c) Marimer LLC