从泛型方法返回对象作为接口
我有一个接口 InterfaceBase
和一些从它派生的接口 Interface1、Interface2
。接下来,我有一些实现 InterfaceX
接口的类,而不是基本接口。
现在,我是泛型的初学者,很多新方法在我的头脑中造成了很大的混乱:(。我想创建工厂(静态类),我称之为类似
Interface1 concrete1 = Factory.Get<Interface1>();
以下是我的工厂(示例)实现,这并不工作:
public static class Factory {
public static T Get<T>() where T: InterfaceBase{
Type type = typeof(T);
//return new Concrete1() as T; // type T cannot be used with the as
//return new Concrete1() as type; //type not found
//return new Concrete1(); // cannot implicitly convert
//return new Concrete1() as InterfaceBase; //cannot convert IBase to T
//return new Concrete1() as Interface1; //cannot convert Interface1 to T
}
}
我想要实现的是从应用程序的其余部分隐藏类(它们是Web服务处理程序),以便轻松地交换它们,因为这些类将是单例,并且它们将存储在工厂内的字典中,这样工厂就可以将它们分散到各地通过此方法应用程序,但作为接口.. 也许我没有正确使用约束 我做错了什么吗?我的方法不好吗?有没有更好的东西,也许整个架构应该重新设计?图表以更好地显示架构。工厂不在里面
I have one interface InterfaceBase
and some interfaces derived from it Interface1, Interface2
. Next I have classes that are implementing the InterfaceX
interfaces, not the base one.
Now, i am beginner in generics and so many new approaches in this made great mess in my head :( . I want to create factory (static class) where I call something like
Interface1 concrete1 = Factory.Get<Interface1>();
Here is my (sample) implementation of factory, that does not work:
public static class Factory {
public static T Get<T>() where T: InterfaceBase{
Type type = typeof(T);
//return new Concrete1() as T; // type T cannot be used with the as
//return new Concrete1() as type; //type not found
//return new Concrete1(); // cannot implicitly convert
//return new Concrete1() as InterfaceBase; //cannot convert IBase to T
//return new Concrete1() as Interface1; //cannot convert Interface1 to T
}
}
What I want to achieve is hide the classes (they are webservice handlers) from the rest of the application to exchange them lightly. I wanted use the factory as the classes will be singletons and they will be stored in Dictionary inside the factory, so the factory can spread them across the application through this method, but as interfaces..
Maybe i am not using the constraints correctly
am I doing smthing wrong? is my approach bad? can be there something better, maybe the whole architecture shoul be reworked? diagram to show better the architecture. The factory is not in it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找的是“穷人依赖注入”。我想你应该使用一个真正的 IoC 容器,有很多选择(Unity、Castle Windsor、Ninject...)。
但无论如何,如果您坚持自己做,请遵循@Sergey Kudriavtsev 的推荐。只需确保对于每个接口,您返回正确的具体类。像这样:
你通过将接口引用传递给工厂来调用它:
Methinks what you are looking for is a "Poor-Man Dependency Injection". I guess you should use a real IoC container for that, there are a lot of options (Unity, Castle Windsor, Ninject...).
But anyway, If you insist in doing it by yourself, go with what @Sergey Kudriavtsev is recomending. Just make sure that, for each interface, you return the proper concrete class. Something like this:
And you call it by passing the interface reference to the factory:
这应该可行:
此外,调用工厂方法的代码应该如下所示:
This should work:
Also the code for calling factory method should be like this:
回答您问题的一部分:
类型
T
不能与as
一起使用,因为T
未知为引用类型。您可以使用where T : class [, ...]
约束将类约束为引用类型。这将允许您使用as
运算符,当然,假设您不需要能够将此方法与值类型一起使用。编辑话虽如此,我更喜欢rsenna的答案。由于您在编译时知道直接转换会起作用,因此使用直接转换比我也同意他的建议,即您研究“真正的”IoC 容器,但我想补充一点,当您了解泛型时,对泛型的深入理解将对您非常有用。既然您说您是泛型的初学者,那么像这样的练习可能是一个好主意,因为它将帮助您了解泛型,并让您更好地理解 IoC 容器的价值可以添加。as
更有意义。编辑2
我看到另一个问题:您将 T 约束为 InterfaceBase,然后将 Concrete1 转换为 T。不知道 Concrete1 是从 T 派生的!这当然就是您使用
as
转换的原因。所以答案是,添加class
约束就可以了。编辑3
正如rsenna指出的,您还可以通过向上和向下进行类型检查:
或者
我想知道这在效率方面与
我今天晚些时候检查(如果我有时间的话) 相比如何这样做。
To answer one part of your question:
Type
T
cannot be used withas
becauseT
is not known to be a reference type. You can constrain the class to be a reference type with thewhere T : class [, ...]
constraint. This will allow you to use theas
operator, assuming, of course, that you don't need to be able to use this method with value types.EDITHaving said that, I prefer rsenna's answer. Since you know at compile time that the direct cast will work, it makes more sense to use a direct cast thanI also agree with his recommendation that you investigate "real" IoC containers, but I would add that a strong understanding of generics will be very useful to you as you learn about them. Since you say that you are a beginner in generics, an exercise like this one is probably a good idea, because it will help you learn about generics, and give you a better appreciation for the value that IoC containers can add.as
.EDIT 2
I see another problem: You constrain T to be InterfaceBase, and then you cast Concrete1 to T. Concrete1 is not known to derive from T! Which is of course why you use the
as
cast. So the answer is, add theclass
constraint and you should be fine.EDIT 3
As rsenna points out, you can also get a runtime type check with an upcast and downcast:
or
I wonder how this compares in terms of efficiency with
I'll check later today if I find some time to do so.
做这样的事情
不能安全地输入。您不能保证将在 T == Concrete1 的情况下调用该方法。 T 可以是 InterfaceBase 的任何子类型,Concrete1 只是这些子类型之一,不一定是相同的 T,因此编译器不会允许您转换为 T,就像它不允许您转换为字符串或任何其他类型一样其他不相关的类型。
Activator.CreateInstance() 是处理该问题的一种方法:CreateInstance 确实保证构建的实例是 T 类型,这是该方法的预期输出值。
Doing something like this
Cannot be typed safely. You cannot guarantee that the method will be invoked with T == Concrete1. T can be ANY subtype of InterfaceBase, Concrete1 is just one of those subtypes and not necessarily the same T, therefore the compiler won't allow you to cast to T, the same way it won't allow you to cast to string or any other unrelated type.
Activator.CreateInstance() is one way of handling that: CreateInstance does guarantee that the built instance is of type T, which is the expected output value of the method.
如果您知道 T 是 InterfaceBase 的子类型(其中 T : InterfaceBase),那么您可以将 Get 方法的返回类型设置为 InterfaceBase:
可以使用 InterfaceBase 的子接口作为 T 来调用此方法:
如果您传递以下任何内容 : InterfaceBase 的子接口作为 T 编译器会抱怨。
我认为这是一个更好的方法,但正如 @phhoog 指出的那样,它仅适用于 T 类型参数的具体类:
If you know that T is a subtype of InterfaceBase (where T : InterfaceBase) then you can make the return type of the Get method be InterfaceBase:
This method can be invoked using a sub-interface of InterfaceBase as T:
If you pass anything but a sub-interface of InterfaceBase as T the compiler will complain.
I think this is a better approach but as @phoog points it only works with concrete classes for the T type parameter: