views:

73

answers:

1

I have 3 classes: A, B, and C

All of these classes implement an interface IMyInterface

I would like the interface to be defined like so:

internal IMyInterface<E> where E: class
{
    E returnData();
}

So that it can return data of type E. The type "E" will be a POCO object created with the Entity Framework v4.

In a separate class I have:

public class MyClass()
{
   IMyInterface<??> businessLogic;

   public setBusinessLogic(IMyInterface<E> myObject)
       where E : class
   {
       businessLogic = myObject;
   }
}

I tried putting <object> in place of <??> but it could not cast my poco entity type.

I tried having my entities implement an empty interface IEntity then using

IMyInterface<IEntity> businessLogic;
...
businessLogic = new A<POCOObject>();

results in:

 Cannot implicitly convert type
 'A<POCOObject>' to
 'IMyInterface<IEntity>'. An explicit
 conversion exists (are you missing a
 cast?)

Any recommendations?

Edit: I've tried declaring my A, B, and C classes as:

internal class A : IBidManager<EntityObjectType>

and

internal class A<E> : IBidManager<E> where E : class

results in the same error.

+3  A: 

It will have to be either

public class MyClass<E> where E : IEntity, class
{
   IMyInterface<E> businessLogic;

   public setBusinessLogic(IMyInterface<E> myObject)
   {
       businessLogic = myObject;
   }
}

or

 public class MyClass
{
   IMyInterface<POCOObject> businessLogic;

   public setBusinessLogic(IMyInterface<POCOObject> myObject)
   {
       businessLogic = myObject;
   }
}

If you wnt your business object to handle any POCO object and they each have an interface then you will need to specify where E : class, IEntity at the class level. Otherwise you have yo use concrete type for the generic arg

aqwert
Hrmm initially it was working when I just did IBidManager<IEntity> businessLogic; then did businessLogic = (IBidManager<IEntity>)new A(); .... but for some reason it isnt working anymore...
Chris Klepeis
Although the solutions above work, I cannot do them that way. In the first example you use <E> in the class definition. Our system isn't setup to work that way. I want to create a class, and then call a function to change the IMyInterface object to reference something else. In the second example its not dynamic, the POCOObject is different for A, B, and C
Chris Klepeis
It's telling me an explicit conversion exists... hrmm
Chris Klepeis
Yes, because you want IMyInteface<E> as a field in the class the class needs the generic argument. Since you want to swap in different objects then having them share a common interface would work. I have not dabbled with c#4 so not sure if the covarience/contravarience conversions would help. Not knowing what your business object does I would think at some level an interface would need to exist so you can deal with different implementations. I think even if you try explicit conversion then you will need to convert it to a known type which in your generic class would get ugly.
aqwert
Sorry for the delay.. went for a run ;)
aqwert
covariance worked... in my interface I just set it to IMyInterface<out E> where E : IEntity ... thanks :)
Chris Klepeis
Yes, glad that worked for you in the end.
aqwert