开发者

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

开发者 https://www.devze.com 2023-03-26 02:13 出处:网络
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)

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<ISes开发者_开发技巧sionFactory>().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?


Use the Open Generics support:-

Bind(typeof(IIntKeyedRepository<>)).To(typeof(Repository<>)).InRequestScope();
0

精彩评论

暂无评论...
验证码 换一张
取 消