5

I am using NHibernate and ninject in ASP.Net MVC, using this page as a guide. One thing I think is weird is that, in this code (half way down the page)

public class RepositoryModule : NinjectModule
{
     public override void Load()
     {
        const string connectionString = @"Server=localhost; Port=3306; Database=trucktracker; Uid=root; Pwd='your_own_password';";

        NHibernateHelper helper = new NHibernateHelper(connectionString);
        Bind<ISessionFactory>().ToConstant(helper.SessionFactory).InSingletonScope();

        Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
        Bind<ISession>().ToProvider(new SessionProvider()).InRequestScope();
        Bind<IIntKeyedRepository<Truck>>().To<Repository<Truck>>().InRequestScope();
    }
}

I think it's odd that you need to have this line per model:

Bind<IIntKeyedRepository<Truck>>().To<Repository<Truck>>().InRequestScope();

If I have 100 different tables (and thus models) do I really need to add this line in for every class that I have? Is there not a better way where I can just declare this once and use inheritance to pass in the Type in my controller?

ckittel
  • 6,478
  • 3
  • 41
  • 71
leora
  • 188,729
  • 360
  • 878
  • 1,366

1 Answers1

6

Use the Open Generics support:-

Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope();
jim tollan
  • 22,305
  • 4
  • 49
  • 63
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98