Subclassing syntax for BusinessListBase<T,C>

Subclassing syntax for BusinessListBase<T,C>

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


Sarosh posted on Wednesday, September 20, 2006

Hi!

Trying to create our own base classes for the 7 main ones.

1] BusinessBase<T>

[Serializable()]
public abstract class MyNewBusinessBase<T> : BusinessBase<T> where T : MyNewBusinessBase<T>


2] BusinessListBase<T,C>

Having trouble with this one!!!


3] CommandBase

?

4] ReadOnlyBase<T>

Similar to 1] ?

5] ReadOnlyListBase<T,C>

?

6] NameValueListBase<K,V>

?

7] EditableRootListBase<T>

Similar to 1] ?

 

Thanks

 

Sarosh


Michael Hildner replied on Wednesday, September 20, 2006

Hi Sarosh,

I just did this yesterday or the day before. This is what I have - not really sure if it's correct or not, but it compiles :) I've only used my BusinessBase so far.

2)

using System;

using System.Collections.Generic;

using System.Text;

using Csla;

namespace Buoy.BusinessObjects203

{

[Serializable]

public abstract class BuoyBusinessListBase<T, C> :

BusinessListBase<T, C>

where T : BuoyBusinessListBase<T, C>

where C : Csla.Core.IEditableBusinessObject

{

}

}

 

3)

using System;

using System.Collections.Generic;

using System.Text;

using Csla;

namespace Buoy.BusinessObjects203

{

public abstract class BuoyCommandBase : CommandBase

{

}

}

 

4) Yes

5)

using System;

using System.Collections.Generic;

using System.Text;

using Csla;

namespace Buoy.BusinessObjects203

{

public abstract class BuoyReadOnlyListBase<T, C> :

ReadOnlyListBase<T, C>

where T : BuoyReadOnlyListBase<T, C>

{

}

}

6)

using System;

using System.Collections.Generic;

using System.Text;

using Csla;

namespace Buoy.BusinessObjects203

{

public abstract class BuoyNameValueListBase<K, V> :

NameValueListBase<K, V>

{

}

}

7) I'm not using 2.1 yet.

Regards,

Mike

Sarosh replied on Wednesday, September 20, 2006

Hi!

That's exactly what I was looking for.

Thanks a lot!

Sarosh

Fabio replied on Wednesday, September 20, 2006

1)
    [Serializable]
    public class YourBusinessBase<T> : Csla.BusinessBase<T>
                                            where T : YourBusinessBase<T>
2)
  [Serializable]
  public class YourBusinessListBase<T, C>: Csla.BusinessListBase<T,C>
    where T: YourBusinessListBase<T,C>
    where C : Csla.Core.IEditableBusinessObject
5)
  [Serializable]
  public class YourReadOnlyListBase<T, C> :
    Csla.ReadOnlyListBase<T,C>
    where T : YourReadOnlyListBase<T, C>

And so on.......


Sarosh replied on Wednesday, September 20, 2006

Hi!

Thanks for the info. but what about  EditableRootListBase

 

Sarosh

Sarosh replied on Wednesday, September 20, 2006

Hi!

I got it for EditableRootListBase<T> as well.

Thanks.

Sarosh

JoeFallon1 replied on Wednesday, September 20, 2006

The VB code version of your code is:

<Serializable()> _

Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As Core.IEditableBusinessObject)

  Inherits BusinessListBase(Of T, C)

End Class

============================================================

Is there anything wrong with this code though:

<Serializable()> _

Public MustInherit Class MyBusinessListBase(Of C As Core.BusinessBase)

  Inherits BusinessListBase(Of MyBusinessListBase(Of C), C)

End Class

 

My actual BO lists then can use code like this:

<Serializable()> _

Public Class SomeList

Inherits MyBusinessListBase(Of SomeChildBO)

 

Joe

 

 

Brian Criswell replied on Wednesday, September 20, 2006

My guess is that you could not then use T in your template.

JoeFallon1 replied on Wednesday, September 27, 2006

Rocky,

Can you please comment on the "correct" way to derive from BusinessListBase if you plan to have a layer between CSLA and your own BOs?

I showed 2 alternatives above and was planning on usng the simpler one for my custom layer and then deriving all my lists from it. Will I be losing any functionality? Is this a mistake? Shjould I use the more complex form? If so, what does it gain me in my lists?

Joe

RockfordLhotka replied on Wednesday, September 27, 2006

Brian is correct - you lose the ability to use T in your own template base class (with your second example). But if that doesn't bother you then it should be fine.

I'd also point out that you lose consistency with the "real" CSLA base class, so your new base class isn't interchangeable without some re-coding. Again, if that doesn't bother you then it should be fine.

JoeFallon1 replied on Wednesday, September 27, 2006

Rocky,

Thanks. I will use the more complex syntax so I don't accidentally lose functionality.

I *do* want to be as compatible as possible.

Joe

JoeFallon1 replied on Wednesday, September 27, 2006

I updated my BusinessListBase class to code like this:

=========================================================

<Serializable()> _

Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As Core.IEditableBusinessObject)

Inherits BusinessListBase(Of T, C)

=========================================================

I also have a BusinessBase class like this:

<Serializable()> _

Public MustInherit Class MyBusinessBase(Of T As MyBusinessBase(Of T))

Inherits BusinessBase(Of T)

Inside this base class I declare 2 variables.

The first is simple:

Private Shared BIZ_TYPE As Type = GetType(MyBusinessBase(Of T))

The 2nd used to be simple but now I can't get it to compile.

It used to be like this:

Private Shared COLL_TYPE As Type = GetType(MyBusinessListBase(Of Core.IEditableBusinessObject))

=========================================================

But now I can't figure out how to re-write the 2nd variable using the base classes above.

Every combination fails to compile.

Any advice?

=========================================================

Joe

 

JoeFallon1 replied on Thursday, September 28, 2006

bump

RockfordLhotka replied on Thursday, September 28, 2006

Wouldn't it be

Private Shared COLL_TYPE As Type = _

GetType(MyBusinessListBase( _

Of MyBusinessListBase(Of Core.IEditableBusinessObject), Core.IEditableBusinessObject))


JoeFallon1 replied on Thursday, September 28, 2006

Rocky,

That won't compile:

Private Shared COLL_TYPE As Type = GetType(MyBusinessListBase(Of MyBusinessListBase(Of Core.IEditableBusinessObject), Core.IEditableBusinessObject))

It gives the message:
"Too few type arguments to MyBusinessListBase(Of T,C)".

============================================================

This is how my BusinessListBase class is declared now:

<Serializable()> _
Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As Core.IEditableBusinessObject)
 
Inherits BusinessListBase(Of T, C)

============================================================

I can't seem to find a syntax that will compile for T.

I am open to other attempts to fix this.

Update: Omit the T and C parameters (but leave the comma.) Then GetType works.

Private Shared COLL_TYPE As Type = GetType(PNIBusinessListBase(Of ,))

===========================================================

I am planning to use this COLL_Type variable in code like this:

Dim coll As PNIBusinessListBase(Of Core.BusinessBase)
.....
If fi(j).FieldType.IsSubclassOf(COLL_TYPE) Then
 
coll = CType(fi(j).GetValue(root), MyBusinessListBase(Of Core.IEditableBusinessObject))

But none of the lines above will compile now either. Is it incorrect to use generics this way? I am simply trying to get a list of all the broken rules from a given BO which contains other BOs. This code is supposed to loop over all the contained BOs and fetch their broken rules too. This is the method called ALLRules from 1.x which was contributed to the community. Has anyone else used it and re-writen it for 2.0?

Joe

Sarosh replied on Thursday, September 28, 2006

Hi!

This is what I am using.

///

[Serializable()]

public abstract class MyBusinessListBase<T, C> : BusinessListBase<T, C>, IMyEditableBusinessObject

where T : MyBusinessListBase<T, C>

where C : Csla.Core.IEditableBusinessObject

///

Sarosh

 

sidwall replied on Friday, October 06, 2006

Is there anything wrong with:

[Serializable()]
public abstract class MyBusinessBase<T> : Csla.BusinessBase<T>
  
where T : MyBusinessBase<T>

[Serializable
()]
public abstract class MyBusinessListBase<T, C> : Csla.BusinessListBase<T, C>
   where T : MyBusinessListBase<T, C>
   where C : MyBusinessBase<C>

It compiles and seemes to work fine.

JoeFallon1 replied on Friday, October 06, 2006

"Is there anything wrong with"

Nope.

Those are correct for C#.

For VB use:

 <Serializable()> _
  Public MustInherit Class MyBusinessBase(Of T As MyBusinessBase(Of T))
    Inherits BusinessBase(Of T)

 <Serializable()> _
  Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As MyBusinessBase)
    Inherits BusinessListBase(Of T, C)

Joe

 

Copyright (c) Marimer LLC