Ninject has issues dealing with constructors

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
28,050
Reaction score
17,804
hi guys. I have a problem with an old legacy project that has landed across my desk. It is in .NET 5.6.

In trying to fix their dependency hell I have created a fresh project to try to understand how Ninject works. I have a simple SQLDBClass, a GenericRepository and a Service. This is the service constructor.

Code:
    public class ReservationService : IReservationService
    {
        private readonly IRepository<ReservationType> _context;
        public ReservationService(IRepository<ReservationType> context)
        {
            _context = context;
        }
    }


On the ninject side in the NinjectWebCommon class, my respository binds perfectly

Code:
kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>));
kernel.Bind(typeof(IRepository<ReservationType>)).To(typeof(ReservationType));

But come time to the service in say a HomeController it all goes to pieces. I think it has something to do with the constructor in ReservationService.

kernel.Bind(typeof(lReservationService)).To(typeof(ReservationService));

I get this error: : 'Error activating IRepository{ReservationType} No matching bindings are available, and the type is not self-bindable. Activation path:

I've been staring at this all weekend to no avail. If anyone has any ideas on this please share them!
 
Never used ninject, but shouldn’t

Code:
kernel.Bind(typeof(IRepository<ReservationType>)).To(typeof(ReservationType));

Be

Code:
kernel.Bind(typeof(IRepository<ReservationType>)).To(typeof(Repository<ReservationType>));
 
I'll try that ty. If there is any alternative to Ninject for .net 4.6 I'm all ears. I'm finding it clunky and really unfriendly!
 
Unity is pretty similar to how it works in .net core

I’ve not done much .net framework, but have used unity and autofac and preferred unity in my limited experience.
 
Thanks @_kabal_ I appreciate your help. I don't have time to learn the deeper mechanics of Ninject as it seems that this rabbit hole goes quite deep. I'll try the other frameworks.
 
Quick question for anyone. Patch updates in an API. Possible without Entity Framework? I'm still on this project using a generic repository.
 
Top
Sign up to the MyBroadband newsletter
X