Ninject Binding Generic InterfacesProper use of the IDisposable interfaceHow do I generate a random int number?Ninject : ninject.web - How to apply on a regular ASP.Net Web (!MVC)Ninject: Default & specific bindings for a Generic classNinject Bindings: same interface binded to different types. why works?Ninject - Managing Inconvariance of generic types?generic inheritance binding ninjectMVC5 Ninject binding and HttpContextHow do I bind interface to class using generics with Ninject?Ninject .ToFactory: Exception: “Exception thrown: 'Ninject.ActivationException' in Ninject.dll”
Why did the EU agree to delay the Brexit deadline?
What exact color does ozone gas have?
Why is so much work done on numerical verification of the Riemann Hypothesis?
How should I respond when I lied about my education and the company finds out through background check?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Recommended PCB layout understanding - ADM2572 datasheet
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
How does the math work for Perception checks?
Yosemite Fire Rings - What to Expect?
Does the Linux kernel need a file system to run?
When were female captains banned from Starfleet?
Keeping a ball lost forever
Biological Blimps: Propulsion
Need help understanding what a natural log transformation is actually doing and why specific transformations are required for linear regression
How can I write humor as character trait?
Why should universal income be universal?
Can disgust be a key component of horror?
How could a planet have erratic days?
It grows, but water kills it
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
What should you do if you miss a job interview (deliberately)?
Has any country ever had 2 former presidents in jail simultaneously?
Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?
Ninject Binding Generic Interfaces
Proper use of the IDisposable interfaceHow do I generate a random int number?Ninject : ninject.web - How to apply on a regular ASP.Net Web (!MVC)Ninject: Default & specific bindings for a Generic classNinject Bindings: same interface binded to different types. why works?Ninject - Managing Inconvariance of generic types?generic inheritance binding ninjectMVC5 Ninject binding and HttpContextHow do I bind interface to class using generics with Ninject?Ninject .ToFactory: Exception: “Exception thrown: 'Ninject.ActivationException' in Ninject.dll”
I'm trying to use DI to bind a different implementation of my networking class. I've been able to do this successfully using a none generic version of the class. My implementation is as follows:
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));
Class that uses my IRequestSender:
public class Session
IRequestSender requestSender;
[Inject]
public Session(IRequestSender requestSender)
this.requestSender = requestSender;
public void Authenticate()
Console.WriteLine(requestSender.Send("Hello There"));
The IRequestSender interface:
public interface IRequestSender
string Send(string request);
And the two different implementations:
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;
This is very straightforward and it works; however, what I need is for my IRequestSender to use Generic types rather than string for input output:
public interface IRequestSender<RequestT, ResponseT> where RequestT: class where ResponseT: class
RequestT Send(RequestT request);
And the impl's:
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();
I've come across several examples that address this issue and I've tried to base my implementation on them but I have failed. Here are the two problems that I'm running into:
1) Binding: this is the main problem. Here is what my binding looks like based on solutions I have seen online:
public class RealNetworkModule : NinjectModule
public override void Load()
Bind(typeof(IRequestSender<>)).To(typeof(RealRequestSender<>));
VSCode gives me the error:
Program.cs(29,29): Error CS0305: Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments (CS0305) (DI)
Based on this error and what I have read online it is still not clear to me what I need to do here.
2) Accessing IRequestSender: the solution to this might be clear once I know how to fix binding. In the original implementation I used [Inject] to get access to the IRequestSender I need in my Sessions class. However now in the generic version I imagine I will not be able to do this. If I were to use RequestSender without DI it would look like:
RequestSender <AuthRequest, AuthResponse> requestSender = new RequestSender<AuthRequest, AuthResponse>();
or
RequestSender <UserRequest, UserResponse> requestSender = new RequestSender< UserRequest, UserResponse >();
for any number of different types.
So I'm not sure how to go about accessing the RequestSender in this scenario.
c# ninject
add a comment |
I'm trying to use DI to bind a different implementation of my networking class. I've been able to do this successfully using a none generic version of the class. My implementation is as follows:
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));
Class that uses my IRequestSender:
public class Session
IRequestSender requestSender;
[Inject]
public Session(IRequestSender requestSender)
this.requestSender = requestSender;
public void Authenticate()
Console.WriteLine(requestSender.Send("Hello There"));
The IRequestSender interface:
public interface IRequestSender
string Send(string request);
And the two different implementations:
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;
This is very straightforward and it works; however, what I need is for my IRequestSender to use Generic types rather than string for input output:
public interface IRequestSender<RequestT, ResponseT> where RequestT: class where ResponseT: class
RequestT Send(RequestT request);
And the impl's:
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();
I've come across several examples that address this issue and I've tried to base my implementation on them but I have failed. Here are the two problems that I'm running into:
1) Binding: this is the main problem. Here is what my binding looks like based on solutions I have seen online:
public class RealNetworkModule : NinjectModule
public override void Load()
Bind(typeof(IRequestSender<>)).To(typeof(RealRequestSender<>));
VSCode gives me the error:
Program.cs(29,29): Error CS0305: Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments (CS0305) (DI)
Based on this error and what I have read online it is still not clear to me what I need to do here.
2) Accessing IRequestSender: the solution to this might be clear once I know how to fix binding. In the original implementation I used [Inject] to get access to the IRequestSender I need in my Sessions class. However now in the generic version I imagine I will not be able to do this. If I were to use RequestSender without DI it would look like:
RequestSender <AuthRequest, AuthResponse> requestSender = new RequestSender<AuthRequest, AuthResponse>();
or
RequestSender <UserRequest, UserResponse> requestSender = new RequestSender< UserRequest, UserResponse >();
for any number of different types.
So I'm not sure how to go about accessing the RequestSender in this scenario.
c# ninject
I'm not sure if this is your entire problem, but the messageUsing the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding toBind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).
– xander
Mar 8 at 2:45
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48
add a comment |
I'm trying to use DI to bind a different implementation of my networking class. I've been able to do this successfully using a none generic version of the class. My implementation is as follows:
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));
Class that uses my IRequestSender:
public class Session
IRequestSender requestSender;
[Inject]
public Session(IRequestSender requestSender)
this.requestSender = requestSender;
public void Authenticate()
Console.WriteLine(requestSender.Send("Hello There"));
The IRequestSender interface:
public interface IRequestSender
string Send(string request);
And the two different implementations:
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;
This is very straightforward and it works; however, what I need is for my IRequestSender to use Generic types rather than string for input output:
public interface IRequestSender<RequestT, ResponseT> where RequestT: class where ResponseT: class
RequestT Send(RequestT request);
And the impl's:
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();
I've come across several examples that address this issue and I've tried to base my implementation on them but I have failed. Here are the two problems that I'm running into:
1) Binding: this is the main problem. Here is what my binding looks like based on solutions I have seen online:
public class RealNetworkModule : NinjectModule
public override void Load()
Bind(typeof(IRequestSender<>)).To(typeof(RealRequestSender<>));
VSCode gives me the error:
Program.cs(29,29): Error CS0305: Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments (CS0305) (DI)
Based on this error and what I have read online it is still not clear to me what I need to do here.
2) Accessing IRequestSender: the solution to this might be clear once I know how to fix binding. In the original implementation I used [Inject] to get access to the IRequestSender I need in my Sessions class. However now in the generic version I imagine I will not be able to do this. If I were to use RequestSender without DI it would look like:
RequestSender <AuthRequest, AuthResponse> requestSender = new RequestSender<AuthRequest, AuthResponse>();
or
RequestSender <UserRequest, UserResponse> requestSender = new RequestSender< UserRequest, UserResponse >();
for any number of different types.
So I'm not sure how to go about accessing the RequestSender in this scenario.
c# ninject
I'm trying to use DI to bind a different implementation of my networking class. I've been able to do this successfully using a none generic version of the class. My implementation is as follows:
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));
Class that uses my IRequestSender:
public class Session
IRequestSender requestSender;
[Inject]
public Session(IRequestSender requestSender)
this.requestSender = requestSender;
public void Authenticate()
Console.WriteLine(requestSender.Send("Hello There"));
The IRequestSender interface:
public interface IRequestSender
string Send(string request);
And the two different implementations:
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;
This is very straightforward and it works; however, what I need is for my IRequestSender to use Generic types rather than string for input output:
public interface IRequestSender<RequestT, ResponseT> where RequestT: class where ResponseT: class
RequestT Send(RequestT request);
And the impl's:
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();
I've come across several examples that address this issue and I've tried to base my implementation on them but I have failed. Here are the two problems that I'm running into:
1) Binding: this is the main problem. Here is what my binding looks like based on solutions I have seen online:
public class RealNetworkModule : NinjectModule
public override void Load()
Bind(typeof(IRequestSender<>)).To(typeof(RealRequestSender<>));
VSCode gives me the error:
Program.cs(29,29): Error CS0305: Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments (CS0305) (DI)
Based on this error and what I have read online it is still not clear to me what I need to do here.
2) Accessing IRequestSender: the solution to this might be clear once I know how to fix binding. In the original implementation I used [Inject] to get access to the IRequestSender I need in my Sessions class. However now in the generic version I imagine I will not be able to do this. If I were to use RequestSender without DI it would look like:
RequestSender <AuthRequest, AuthResponse> requestSender = new RequestSender<AuthRequest, AuthResponse>();
or
RequestSender <UserRequest, UserResponse> requestSender = new RequestSender< UserRequest, UserResponse >();
for any number of different types.
So I'm not sure how to go about accessing the RequestSender in this scenario.
c# ninject
c# ninject
asked Mar 8 at 2:18
Przemek LachPrzemek Lach
1751621
1751621
I'm not sure if this is your entire problem, but the messageUsing the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding toBind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).
– xander
Mar 8 at 2:45
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48
add a comment |
I'm not sure if this is your entire problem, but the messageUsing the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding toBind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).
– xander
Mar 8 at 2:45
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48
I'm not sure if this is your entire problem, but the message
Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding to Bind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).– xander
Mar 8 at 2:45
I'm not sure if this is your entire problem, but the message
Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding to Bind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).– xander
Mar 8 at 2:45
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48
add a comment |
1 Answer
1
active
oldest
votes
Given your current interface, you'll have to specify the generic type arguments when injecting. Assuming your request and response are both strings, your constructor would look like:
public Session(IRequestSender<string, string> requestSender)
this.requestSender = requestSender;
If you don't want to specify the arguments at creation/injection time, you'll have to change the design a bit. I can't say for certain with the sample code you provided, but it might be possible to remove the generic type args from your interface and place them on the method instead:
public interface IRequestSender
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
With that definition, you'd inject IRequestSender
, and then specify the generic type parameters when calling. For example,
string myResponse = requestSender.Send<string, string>("my string");
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055796%2fninject-binding-generic-interfaces%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Given your current interface, you'll have to specify the generic type arguments when injecting. Assuming your request and response are both strings, your constructor would look like:
public Session(IRequestSender<string, string> requestSender)
this.requestSender = requestSender;
If you don't want to specify the arguments at creation/injection time, you'll have to change the design a bit. I can't say for certain with the sample code you provided, but it might be possible to remove the generic type args from your interface and place them on the method instead:
public interface IRequestSender
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
With that definition, you'd inject IRequestSender
, and then specify the generic type parameters when calling. For example,
string myResponse = requestSender.Send<string, string>("my string");
add a comment |
Given your current interface, you'll have to specify the generic type arguments when injecting. Assuming your request and response are both strings, your constructor would look like:
public Session(IRequestSender<string, string> requestSender)
this.requestSender = requestSender;
If you don't want to specify the arguments at creation/injection time, you'll have to change the design a bit. I can't say for certain with the sample code you provided, but it might be possible to remove the generic type args from your interface and place them on the method instead:
public interface IRequestSender
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
With that definition, you'd inject IRequestSender
, and then specify the generic type parameters when calling. For example,
string myResponse = requestSender.Send<string, string>("my string");
add a comment |
Given your current interface, you'll have to specify the generic type arguments when injecting. Assuming your request and response are both strings, your constructor would look like:
public Session(IRequestSender<string, string> requestSender)
this.requestSender = requestSender;
If you don't want to specify the arguments at creation/injection time, you'll have to change the design a bit. I can't say for certain with the sample code you provided, but it might be possible to remove the generic type args from your interface and place them on the method instead:
public interface IRequestSender
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
With that definition, you'd inject IRequestSender
, and then specify the generic type parameters when calling. For example,
string myResponse = requestSender.Send<string, string>("my string");
Given your current interface, you'll have to specify the generic type arguments when injecting. Assuming your request and response are both strings, your constructor would look like:
public Session(IRequestSender<string, string> requestSender)
this.requestSender = requestSender;
If you don't want to specify the arguments at creation/injection time, you'll have to change the design a bit. I can't say for certain with the sample code you provided, but it might be possible to remove the generic type args from your interface and place them on the method instead:
public interface IRequestSender
RequestT Send<RequestT, ResponseT>(RequestT request)
where RequestT: class
where ResponseT: class;
With that definition, you'd inject IRequestSender
, and then specify the generic type parameters when calling. For example,
string myResponse = requestSender.Send<string, string>("my string");
answered Mar 8 at 3:05
xanderxander
632516
632516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55055796%2fninject-binding-generic-interfaces%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not sure if this is your entire problem, but the message
Using the generic type 'IRequestSender<RequestT, ResponseT>' requires 2 type arguments
is significant... try changing your binding toBind(typeof(IRequestSender<,>)).To(typeof(RealRequestSender<,>));
(note the commas).– xander
Mar 8 at 2:45
Ya that was it. Dumb mistake. Ok that fixes problem 1. Great ty!
– Przemek Lach
Mar 8 at 2:48