BusinessListBase in base class issue.

BusinessListBase in base class issue.

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


TheEld posted on Friday, August 21, 2009

Everything here pertains to CSLA for Windows 3.7...

I have a BusinessListBase<T,C> that contains polymorphyic child and when impleneted as below all works fine. 

public interface ICorrespondenceChild : IEditableBusinessObject
{
}


[Serializable] public class CorrespondenceChildList : BusinessListBase<CorrespondenceChildList, ICorrespondenceChild>
{
}

I also have dozens of other BusinessListBase<T,C> based classes that do not take polymorphic children and when implemeneted as below all works fine.

[Serializable] public class SubmittalItemChildList : ChildListBase<SubmittalItemChildList, SubmittalItemChild>
{
}

[Serializable] public Class SubmittalChild : BusinessBase<SubmittalChild>
{
}

 

 

 

I am now trying to introduce a base class for all my BusinessListBase<T,C> based classes and have the following


[Serializable] public class ChildListBase<T, C> : BusinessListBase<T, C>
    where T : ChildListBase<T, C>
    where C : BusinessBase<C>
{
}


When changing the non-polymorphic child list all is well.  I have...

[Serializable] public class SubmittalItemChildList : ChildListBase<SubmittalItemChildList, SubmittalItemChild>
{
}

But for the polymorphic child list (code below) I get the error below...


[Serializable] public class CorrespondenceChildList : ChildListBase<CorrespondenceChildList, ICorrespondenceChild>
{
}


error CS0311: The type 'ICorrespondenceChild' cannot be used as type parameter 'C' in the generic type or method 'ChildListBase<L,C>'. There is no implicit reference conversion from 'ICorrespondenceChild' to 'Csla.BusinessBase<Ccs.BusinessLibrary.ICorrespondenceChild>'.

 

 


I am looking for help on making this work.  I have tried changing ChildListBase to whats below, but have code in the class like C.Save() and get other compile errors about the call to Save() being unknown.  I would perfer to change ICorrespondenceChild, but will take any ideas.


[Serializable] public class ChildListBase<T, C> : BusinessListBase<T, C>
    where T : ChildListBase<T, C>
    where C : IEditableBusinessObject
{
}

 

 

THANKS IN ADVANCE FOR YOU HELP.  ESPECIALLY SINCE THIS IS PROBABLY MORE A C# ISSUE THEN A CSLA ONE.

 

tmg4340 replied on Friday, August 21, 2009

Well... CSLA's BusinessListBase only requires that "C" be an IEditableBusinessObject, which is why your initial code is working.  If you want to create polymorphic children using interfaces, you don't have much choice - you can't require that "C" be BusinessBase.  BusinessBase may implement IEditableBusinessObject, but as the compiler is telling you, there is no way to convert your ICorrespondenceChild interface into a BusinessBase instance.

An option is to use a base class instead of an interface.  Your base class should inherit from BusinessBase, but you can make it a non-generic abstract class.  Then you can require that "C" be your base-class type.

HTH

- Scott

TheEld replied on Friday, August 21, 2009

Thanks Scott.

That all looks good except in by child class I need to override Save() (with an eventual call to base.Save()) and if I derive from BusinessBase and not from BusinessBase<T> I loose Save() to override.

-David

tmg4340 replied on Friday, August 21, 2009

You should be able to create a virtual "Save" method in your base class that delegates to the BusinessBase<T> version.  Then your subclasses can override your new "Save", which ultimately gets back to the BusinessBase version.

HTH

- Scott

Copyright (c) Marimer LLC