Generating interfaces for collection

Posts   
 
    
stefcl
User
Posts: 210
Joined: 23-Jun-2007
# Posted on: 10-Jul-2007 13:29:35   

Hello,

In my project, a lot of the windows forms which I use for editing LLBLgen generated entities have almost the same behaviour. They are made of 4 buttons (Previous-Next-OK-Cancel) and several textboxes bound to the edited business object.

All these forms contains a collection Object, for example

class FrmEditCategories : Form {
... 
     protected CategoriesEntity editedEntity;
     protected CategoriesCollection editedCollection; 
}



class FrmEditBrands : Form {
...
     protected BrandsEntity editedEntity;
     protected BrandsCollection editedCollection;
}

Most code in both these forms would be exactly the same, so I thought it would be great to use windows form visual inheritance to avoid duplication. So here's my base class

class FrmEditBase<T>  : Form  where T:EntityBase {
...
    protected T editedEntity;
    protected EntityCollectionBase<T> editedCollection;
}

and then 

class FrmEditBrands : FrmEditBase<BrandsEntity> {
...
}

Unfortunately it doesn't work because even if the code is correct and builds successfully, FrmEditBrands can't show in the vs2005 designer because it says it can't load FrmEditBase. It seems to be an awful vs2005 bug (if someone knows a workaround, please tell me please!!!). I can't live without a visual form designer so I had to give up the idea.

My second idea was to use LLBLgen class Ancestors in my base class instead of generics I mean EntityBase and EntityCollectionBase<EntityBase> but It's not possible to cast
an EntityCollectionBase<BrandsEntity> into an EntityCollectionBase<EntityBase> because C# doesn't support covariance.

My last options to avoid getting sick copying-pasting the same code over 40 times are using reflection (which I hate) or having the generated Collections implementing interfaces which expose the few required methods I need. I have read the help and found a way to have LLBLGen generating Entities which implement user-defined interfaces but not collections yet .

Of course you can manually specify the interfaces by using partial classes but is there an another way? (I'm still using the demo)

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 10-Jul-2007 16:16:26   

Hi,

did you try using the corresponding interfaces rather than the direct ancestors? Have a look at IEntity(2) / IEntityCollection(2). You should be alright.

stefcl
User
Posts: 210
Joined: 23-Jun-2007
# Posted on: 10-Jul-2007 19:34:09   

Oh yes! I hadn't noticed that. This is exactly what I need, thanks a lot.

flushed I feel stupid flushed

thanks again.