Can the Func<string,T> delegate injection also be done using open generics

12 views
Skip to first unread message

Mrinal Kamboj

unread,
Jan 21, 2019, 4:21:40 AM1/21/19
to ninject
Following is my interface design:

    public interface IDomainRepository<in TRequest,out TResponse>
    {
        Task<IEnumerable<TResponse>> QueryAsync(TRequest inputParameters);
    }

Since I have lot of entities, so was keen to use open generic binding as follows, thus it takes care of all the bindings:

kernel.Bind(typeof(IDomainRepository<,>)).To(typeof(CommonDomainRepository<,>)).Named("Common");

Challenge is in using Func delegate based injection, so that name binding can be passed at the run-time, something like:

  kernel.Bind<Func<string, IDomainRepository<ManualInput, ManualEntity>>>()
       .ToMethod(ctx => name => kernel.Get<IDomainRepository<ManualInput, ManualEntity>>(name));

But for the Func injection, I am unable to use the open generics, due to which I have create closed generic based binding, which is leading to lots and lots of binding code (as there are 300+ entities), is there way to:

  • Use the Func delegate with open generics ?
  • Any replacement of Func delegate, since here we pass the name for named binding at run-time, which is why Interface allowing open generic cannot be directly injected

Thanks,

Mrinal
Reply all
Reply to author
Forward
0 new messages