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”

Fully-Firstable Anagram Sets

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Can a Cauchy sequence converge for one metric while not converging for another?

Watching something be written to a file live with tail

What does the "remote control" for a QF-4 look like?

How to efficiently unroll a matrix by value with numpy?

How to draw a waving flag in TikZ

Why is Minecraft giving an OpenGL error?

Can I make popcorn with any corn?

Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

Is it possible to do 50 km distance without any previous training?

Perform and show arithmetic with LuaLaTeX

How does quantile regression compare to logistic regression with the variable split at the quantile?

Do infinite dimensional systems make sense?

Maximum likelihood parameters deviate from posterior distributions

Definite integral giving negative value as a result?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

How can I make my BBEG immortal short of making them a Lich or Vampire?

What typically incentivizes a professor to change jobs to a lower ranking university?

Is it unprofessional to ask if a job posting on GlassDoor is real?

High voltage LED indicator 40-1000 VDC without additional power supply

Why is 150k or 200k jobs considered good when there's 300k+ births a month?



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”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















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.










share|improve this question






















  • 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


















0















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.










share|improve this question






















  • 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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 2:18









Przemek LachPrzemek Lach

1801621




1801621












  • 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


















  • 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

















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













1 Answer
1






active

oldest

votes


















1














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");





share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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");





    share|improve this answer



























      1














      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");





      share|improve this answer

























        1












        1








        1







        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");





        share|improve this answer













        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");






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 3:05









        xanderxander

        637516




        637516





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко