我正在尝试使用DI来绑定我的网络类的其他实现。我可以使用该类的非通用版本成功完成此操作。我的实现如下:
class MainClass
{
public static void Main(string[] args)
{
IKernel kernel;
// Hardcode here but will be managed by build system.
bool runningInProd = false;
if (runningInProd)
{
kernel = new StandardKernel(new RealNetworkModule());
}
else
{
kernel = new StandardKernel(new FakeNetworkModule());
}
Session session = kernel.Get<Session>();
session.Authenticate();
}
public class RealNetworkModule : NinjectModule
{
public override void Load()
{
Bind(typeof(IRequestSender)).To(typeof(RealRequestSender));
}
}
public class FakeNetworkModule : NinjectModule
{
public override void Load()
{
Bind(typeof(IRequestSender)).To(typeof(FakeRequestSender));
}
}
}
使用我的IRequestSender的类:
public class Session
{
IRequestSender requestSender;
[Inject]
public Session(IRequestSender requestSender)
{
this.requestSender = requestSender;
}
public void Authenticate()
{
Console.WriteLine(requestSender.Send("Hello There"));
}
}
IRequestSender接口:
public interface IRequestSender
{
string Send(string request);
}
以及两种不同的实现方式:
public class RealRequestSender: IRequestSender
{
public string Send(string request)
{
return "RealRequestSender right back at you: " + request;
}
}
public class FakeRequestSender: IRequestSender
{
public string Send(string request)
{
return "FakeRequestSender right back at you: " + request;
}
}
这非常简单并且有效;但是,我需要IRequestSender使用通用类型而不是字符串作为输入输出:
public interface IRequestSender<RequestT, ResponseT> where RequestT: class where ResponseT: class
{
RequestT Send(RequestT request);
}
和展示次数:
public class FakeRequestSender<RequestT, ResponseT> : IRequestSender<RequestT, ResponseT> where RequestT : class where ResponseT : class
{
public RequestT Send(RequestT request)
{
throw new NotImplementedException();
}
}
public class RealRequestSender<RequestT, ResponseT> : IRequestSender<RequestT, ResponseT> where RequestT : class where ResponseT : class
{
public RequestT Send(RequestT request)
{
throw new NotImplementedException();
}
}
我遇到了几个解决此问题的示例,但我尝试将其实现作为基础,但失败了。这是我遇到的两个问题:
1)绑定:这是主要问题。根据我在网上看到的解决方案,这是我的绑定外观:
public class RealNetworkModule : NinjectModule
{
public override void Load()
{
Bind(typeof(IRequestSender<>)).To(typeof(RealRequestSender<>));
}
}
VSCode给我错误:
Program.cs(29,29): Error CS0305: Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments (CS0305) (DI)
基于此错误和我在网上阅读的内容,我仍然不清楚在这里需要做什么。
2)访问IRequestSender:一旦我知道如何解决绑定问题,解决方案就很清楚了。在原始实现中,我使用[Inject]来访问Sessions类中所需的IRequestSender。但是现在在通用版本中,我想我将无法执行此操作。如果我要使用没有DI的RequestSender,它将看起来像:
RequestSender <AuthRequest, AuthResponse> requestSender = new RequestSender<AuthRequest, AuthResponse>();
或
RequestSender <UserRequest, UserResponse> requestSender = new RequestSender< UserRequest, UserResponse >();
用于任何数量的不同类型。
因此,我不确定在这种情况下如何访问RequestSender。
答案 0 :(得分:1)
鉴于当前接口,在注入时必须指定泛型类型参数。假设您的请求和响应都是字符串,那么您的构造函数将如下所示:
public Session(IRequestSender<string, string> requestSender)
{
this.requestSender = requestSender;
}
如果您不想在创建/注入时指定参数,则必须对设计进行一些更改。我无法确定您提供的示例代码如何,但是可能可以从您的接口中删除泛型类型args,然后将它们放在方法上:
public interface IRequestSender
{
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
}
使用该定义,您将注入IRequestSender
,然后在调用时指定泛型类型参数。例如,
string myResponse = requestSender.Send<string, string>("my string");