Best Practices ?? C# Inheritance Using Generics ????

Best Practices ?? C# Inheritance Using Generics ????

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


tsaltd posted on Monday, January 08, 2007

After reviewing the threads on the above subject, I'm having trouble coming up with the best way to go developing my 2.1 ( should I go with 2.1.1 today??)  Inheritance functionality

A lot of the discussions have focused on VB.NET solutions and translating those examples to C# is overly challenging.

Right now I'm finding the 12/25 Inheritance And Generics thread and the 5/26 CSLA Inheritance problem thread to be fairly straighforward introductions to the matter

Am I on the right track ???

Thanks,

Steve

skagen00 replied on Tuesday, January 09, 2007

Hello Steve,

I'd say those threads are probably decent ones to get started on.

One very useful mechanism, which I don't know is or is not mentioned in those two threads you mentioned, is the employing of interfaces (Rocky does this within the framework as well).

So if my "Profiles" are either Individuals or Organizations, and I have Individuals : Profile<Individual> and Organizations : Profile<Organization> - how can I refer to a generic "Profile"? With generics, this is not directly possible!

So the idea is - throw an IProfile interface onto Profile<T>, expose public members via IProfile, and pass around an IProfile, which both Individuals and Organizations will implement by nature of descending from Profile<T>. So IProfile might have members such as Id, Name, Sort Name, Status, etc.

For factory methods, obviously one cannot call a static method on a generic template (on a generic class, sure, but not something like: Profile<T>.GetProfile(1928);.) So what I've done in this circumstance is to create factory classes - ProfileFactory, for example - which implements the ProfileFactory.GetProfile(int id), ProfileFactory.Exists(int id), etc.

Inheritance going through BusinessBase<T> is a little tricky (an option is to go around BusinessBase<T> and inherit from Core.BusinessBase), but I decided to attempt to remain true to the more advocated route - all editable business objects descend from BusinessBase<T>.

Copyright (c) Marimer LLC