Overriding generic methods in generated abstract base class.

Overriding generic methods in generated abstract base class.

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


shahram posted on Wednesday, June 14, 2006

Hi!
I have used this templates to generate a editable child object-split baseclass.Then I have a parent(rootobject) which has a ID and i want use it when i unsert a new child object

Client parent - Root object

 BankAccount child object

-first a little code:

[Serializable()]
public abstract class BankAccountBase<T> : Csla.BusinessBase<T> where T :BankAccountBase<T>
{
#region Data Access - Insert
internal void Insert<P>(SqlConnection cn, P parent) where P : ClientBase<P>
{
if (!IsDirty) return;

ExecuteInsert(cn, parent);
MarkOld();

//update child object(s)
UpdateChildren(cn);
}

protected virtual void ExecuteInsert<P>(SqlConnection cn, P parent) where P : ClientBase<P>

{

}
protected virtual void AddInsertParameters<P>(SqlCommand cm, P parent) where P : ClientBase<P>
{
}
#endregion //Data Access - Insert

}

--------------------------------------
Now in user class which is
[Serializable()]
public class BankAccount : BankAccountBase<BankAccount>
 {
}

I want to override base methods like this

 #region Data Access - Insert

 protected override void ExecuteInsert<Client>(SqlConnection cn, Client parent)//Client is the parent class
{
}
It doesn,t work .
So how you guys do when you eant override methods in generated abstract base classes and use a specific type like Client ?
Regards

rasupit replied on Wednesday, June 14, 2006

in base class:

protected virtual void ExecuteInsert<P>(SqlConnection cn, P parent) where P : ClientBase<P>

in user class:

protected override void ExecuteInsert<Client>(SqlConnection cn, Client parent)//Client is the parent class

What error message did you get?

Notice the constraint in base class, so make sure your Client object is inherited from ClientBase, or you can stay with P.

Ricky

 

shahram replied on Thursday, June 15, 2006

 This is my Client class:

namespace InvoiceServices.Library

{

[Serializable()]

public class Client : ClientBase<Client>

{}

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

in base class:

protected virtual void ExecuteInsert<P>(SqlConnection cn, P parent) where P : ClientBase<P>

now if i do like this:

in user class:

protected override void ExecuteInsert<Client>(SqlConnection cn, Client parent)//Client is the parent class

The result is that "Client" become a generic identifer not InvoiceServices.Library.Client

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

 if i do like this:

in user class:

protected override void ExecuteInsert<Client>(SqlConnection cn, InvoiceServices.Library.Client parent)//Client is the parent class

I get error:

no suitable method found to override 

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

 if i do like this:

in user class:

protected override void ExecuteInsert<InvoiceServices.Library.Client>(SqlConnection cn, InvoiceServices.Library.Client parent)//Client is the parent class

I get error:

 Type parameter declaration must be an identifier not a type 

 

shahram replied on Thursday, June 15, 2006

The issue is if you can not override this virtal generic methods so why using virtual

and why anyway using generic and all this .. I mean Code generator knows the type of parent class so it wouldn't be better for code generator to use ´ClientBase type directly.

The thing is i want use something like parent.ClientID and i can do this in base class

but in overrided method i can't!

Baseclass:

protected virtual void AddInsertParameters<P>(SqlCommand cm, P parent) where P : ClientBase<P>

{

parent.ClientID //intellisense Works

}

in Child class:

protected overridevoid AddInsertParameters<P>(SqlCommand cm, P parent) // I can not define constraint here it it usese constraint in baseclass

{

parent.ClientID //intellisense doesn't work no ClintId property

}

Regards

rasupit replied on Thursday, June 15, 2006

protected overridevoid AddInsertParameters<P>(SqlCommand cm, P parent) // I can not define constraint here it it usese constraint in baseclass  //Yes, because it always inherited from your base class 

{

parent.ClientID //intellisense doesn't work no ClintId property  //Yes, however it will compile and it will work

//or you can create a local method with constraint (of course) and pass the parent, from there you'll get intellisense

LocalMethod(parent);

}

private void LocalMethod<P>(P parent) where P : ClientBase<P> { //intellisense here }

Your argument about using generic on virtual method is well taken however.  Initially, I have used non generic method like this:

protected override void AddInsertParameter(SqlCommand cm, Client parent) {}

I did change however to generic, thinking that all code in base class should stays on base class land.  This gives flexibility not to enforce contract with any concrete class. 

But when I think of it again, this not really an issue.  Our base class really a _different type_ of base class.  It is intended to be used by one concrete class only.

Any Generics Guru wants to chime in? I would like to hear from anybody, if there is a better way of implementing this.

Ricky

shahram replied on Friday, June 16, 2006

Thanx it compiles!

Copyright (c) Marimer LLC