Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I really add this line for each class in my model using ninject and NHibernate?

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?

like image 936
leora Avatar asked Aug 06 '11 00:08

leora


1 Answers

Use the Open Generics support:-

Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope();
like image 63
Remo Gloor Avatar answered Nov 15 '22 05:11

Remo Gloor