0

I have the following interface:

public interface IQueryStringHelper<T, in TQueryString> where T : class where TQueryString : class
{
    IQueryable<T> ApplyQueryStringToQuery(IQueryable<T> query, TQueryString queryString, out int count);
}

and its implementation in one of the classes looks like:

public class BookingQueryStringHelper : IQueryStringHelper<Booking, BookingsQueryString>
{
    public IQueryable<Booking> ApplyQueryStringToQuery(IQueryable<Booking> query, BookingsQueryString queryString, ICmsDbContext _cmsDbContext, out int count)
    {
          var cat....
    }
}

I have many different classes that use this generic interface and i want to bind it in ninject but having 0 success so far. I don't want one line per implementation(i know that works).

I've tried writing it something like this:

    kernel.Bind(x => x.FromThisAssembly()
          .SelectAllClasses()
          .InheritedFrom(typeof(IQueryStringHelper<,>))
          .BindSingleInterface());

and

        kernel.Bind(x =>
            x.FromThisAssembly()
            .SelectAllClasses()
            .InheritedFrom(typeof(IQueryStringHelper<,>))
            .BindToSelf());

but Im not able to get it to work

All help is very welcome!

*EDIT @Steven pointed me in the correct direction, so the bind statement is as following:

kernel.Bind(x =>
                x.FromAssemblyContaining(typeof(IQueryStringHelper<,>))
                .SelectAllClasses()
                .InheritedFrom(typeof(IQueryStringHelper <,>))
                .BindSingleInterface());

and now everything works as intended!

grimsan55
  • 265
  • 3
  • 4
  • 20
  • Are all `IQueryStringHelper<,>` implementations located in the same assembly as your registration code is? If not, `FromThisAssembly()` will not work. – Steven Oct 26 '16 at 14:07
  • @Steven , they are located within the same solution but in different projects – grimsan55 Oct 26 '16 at 14:14
  • kernel.Bind(x => x.FromAssemblyContaining(typeof(IQueryStringHelper<,>)) .SelectAllClasses() .InheritedFrom(typeof(IQueryStringHelper <,>)) .BindSingleInterface()); you guided me in the right direction @Steven. If you want to make an answer of your suggestion go right ahead and I'll accept it! – grimsan55 Oct 26 '16 at 14:26

0 Answers0