> I'm still not sure why you should not mix generics with interfaces.
>
> Are we saying they are mutually exclusive? When it comes to
> constraints I thought this was a good approach?
Interfaces define types. Restrictions on a generic method implies
implementation specifics, as the where clause is solely
used by the compiler to check if the type passed in is obeying the code.
If I do:
public interface IFoo
{
void Bar<T>(T t) where T:MyBaseClass;
}
no matter what I do, every time I implement IFoo, T has to be MyBaseClass. IMHO
too restrictive. However that's not the main point.
The main point is this. Say you have an interface, IValidatable. In a
routine, you want to do:
IValidatable toValidate = myObject as IValidatable;
bool validateResult = toValidate.Validate();
all is well, right? This is in fact generic code. Ok. Now, say this
interface, IValidatable, is actually IValidatable<T>.
This then becomes:
IValidatable<T> toValidate = myObject as IValidatable<T>;
...
But, this only works in code where T is defined at compile time, OR in
code where you know what T is, thus like:
IValidatable<EntityValidator> toValidate = myObject as
IValidatable<EntityValidator>;
and then you also have to take into account that it's indeed possible,
covariance isn't supported in C#.
In a routine where T isn't defined, you're suddenly faced with a
problem: you have to specify T though you don't know what T
is.
With methods, you can overcome this a bit with this mechanism:
public void Foo<T>(T t)
where T...
{
T temp = t;
//...
//...
}
and if you then do:
MyClass c = new MyClass();
this.Foo(c);
T is automatically defined, you don't have to specify it.
Now, this all might sound like cornercase mumbo jumbo, but I can tell
you: if you have interfaces AND generic classes, it
can become very nasty very quickly, as the goal you want to achieve is generic
code: i.e. a routine which is usable with a lot of
classes. If you're not careful, you run into the situation where you have to
specify a generic type specifier however are unable to
do so at that point. This especially occurs if you have a generic type in an
inheritance hierarchy and implement an interface which
ALSO has a generic type specifier (though perhaps differnet or a subset of the
class does).
It's also unnecessary: interfaces are there to provide you with a type
specification which is implemented by other types,
though through that interface you can use whatever class is passed in, in
effect generic programming. In practise, this often leads
to cleaner code and thus more easy to understand code.
FB
>
>
>
>
>
> [EMAIL PROTECTED]
>
>
>
>
>
> >From: Yago Alvarado <[EMAIL PROTECTED]>
> >Reply-To: "Discussion of advanced .NET topics."
> ><[email protected]>
> >To: [email protected]
> >Subject: Re: [ADVANCED-DOTNET] Generics. How can I do a generic
> >declaration? - SOLVED
> >Date: Tue, 28 Mar 2006 04:59:38 -0500
> >
> >Thank you Frans and Bruno for your help. In the end I
> managed to get it
> >to work.
> >
> >I created an interface like below:
> >
> >public interface IDataManager<T>
> >{
> > List<T> SelectData();
> > int UpdateData(T objectToBeUpdated);
> > ...
> >}
> >
> >and then in the implementation I did as follows:
> >
> >public class StoreGroupDataManager : IDataManager<StoreGroup> {
> >
> >#region Implementation of IDataManager<T>
> >
> > public List<StoreGroup> SelectData() {...}
> >
> > public int UpdateData(StoreGroup objectToBeUpdated) {...}
> >#endregion }
> >
> >
> >This is not as flexible as using ArrayLists and avoiding generics
> >(hence Frans warning about mixing generics with interfaces) but is
> >flexible enough to allow me to pick among different
> >IDataManager<StoreGroup> implementations in my business layer.
> >
> >I am loosing a little bit of flexibility but I'm hoping to
> increase the
> >performance due to the use of generics over ArrayLists.
> >
> >Does anyone have any thoughts about this?
> >
> >
> >Regards,
> >Yago
> >
> >===================================
> >This list is hosted by DevelopMentorR http://www.develop.com
> >
> >View archives and manage your subscription(s) at
> >http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentorR http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com