C# 依赖注入问题

发布于 2024-09-07 17:47:41 字数 1176 浏览 3 评论 0原文

我有 3 个类:A、B 和 C

所有这些类都实现一个接口 IMyInterface

我希望像这样定义该接口:

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

以便它可以返回 E 类型的数据。 类型“E”将是使用实体框架 v4 创建的 POCO 对象。

在一个单独的类中,我有:

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

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

我尝试将 代替 但它无法转换我的 poco 实体类型。

我尝试让我的实体实现一个空接口 IEntity 然后使用

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

结果:

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

有什么建议吗?

编辑:我尝试将我的 A、B 和 C 类声明为:

internal class A : IBidManager<EntityObjectType>

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

导致相同的错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2024-09-14 17:47:41

它必须是

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

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

 public class MyClass
{
   IMyInterface<POCOObject> businessLogic;

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

如果您希望业务对象处理任何 POCO 对象并且它们每个对象都有一个接口,那么您将需要在类级别指定 where E : class, IEntity。否则你必须使用具体类型作为通用参数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文