Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Dependency Injection problem

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.

like image 269
Chris Klepeis Avatar asked Oct 15 '22 03:10

Chris Klepeis


1 Answers

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

like image 191
aqwert Avatar answered Oct 18 '22 22:10

aqwert