More issues on Inheritance and Generics

More issues on Inheritance and Generics

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


triplea posted on Monday, July 16, 2007

I am trying to extend the CSLA base classes in one of my projects and came to a bit of a problem. I tried to use the following syntax (found here: http://forums.lhotka.net/forums/thread/10972.aspx):

public abstract class MyBase<T>: Csla.BusinessBase<T> where T : MyBase<T>{ ... }
public class MyClass : MyBase<MyClass>{ ... }

In this class I have a method that looks like this: public abstract int GetPropertyMaxLength(string propertyName);

Now I am calling this from my UI (from within a textbox to be more specific) but the problem is that I can't upcast the datasource of my control because a type is needed. So what I am trying to do is this:

Library.MyBase obj = ((BindingSource)this.DataBindings[0].DataSource).DataSource as Library.MyBase;

Obviously this does not compile and although I tried a number of ways, I could not get this to work.I ended extending Csla.BusinessBase as follows:

public abstract class MyBase : BusinessBase<MyBase>
public class MyClass : MyBase

It works now but this is ugly... I can't help thinking I am doing somethnig obviously wrong but just can't spot it... Any ideas?

JoeFallon1 replied on Monday, July 16, 2007

I feel your pain.

I think the key is to realize that generic classes are not really polymorphic.

So it is best to create an Interface in your project which all of your Base classes will use.

e.g.

Public Interface IMyCompanyBusinessObject
 
Inherits IEditableBusinessObject
 
ReadOnly Property BrokenRulesCollection() As Validation.BrokenRulesCollection
 
Property Table() As String
 
Property IsOwner() As Boolean
 
ReadOnly Property OwnedObjects() As IList
 
ReadOnly Property OwnedCollections() As IList
End Interface

Then you can cast to your Interface and use the property or method.

Joe

triplea replied on Monday, July 16, 2007

Thanks, I tried that but things are still a bit confusing Sad [:(]... There are 2 points I was not sure:

1. In my previous example what would I cast my object to? So this statement:
Library.MyBase obj = ((BindingSource)this.DataBindings[0].DataSource).DataSource as Library.MyBase;
would become what?
IMyCompanyBusinessObjectBase obj = ((BindingSource)this.DataBindings[0].DataSource).DataSource as ???;
2. I tried out something like this (out of frustration)
IMyCompanyBusinessObjectBase obj = ((BindingSource)this.DataBindings[0].DataSource).DataSource as IMyCompanyBusinessObjectBase

Although this compiled, the compiler complained about the absence of the method CanWriteProperty() for obj. I thought of adding it in the interface but then it seems I start hiding most other cool features that BusinessBase and other base classes have to offer...

JoeFallon1 replied on Tuesday, July 17, 2007

I would add any Property you need to the Interface and then just "forward the call" in the implementation of the interface to the base method.

Joe

 

Copyright (c) Marimer LLC