In meinem MVC-Projekt, ich habe mein Setup MvcApplication_start():Problem Verständnis Ninject Bindung/Injection
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
Und gebunden haben erfolgreich eine .Um in Bezug auf meine IProductsRepository zu MySqlProductsRepository:
public class NinjectControllerFactory : DefaultControllerFactory
{
private readonly IKernel _kernel = new StandardKernel(new MyServices());
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return null;
return (IController) _kernel.Get(controllerType);
}
public class MyServices: NinjectModule
{
public override void Load()
{
Bind<IProductsRepository>().To<MySqlProductsRepository>();
}
}
}
Aber ich verwende NHibernate und habe eine separate Session Factory-Klasse mit einer GetSession() -Methode, die eine ISession zurückgibt.
public static ISessionFactory SessionFactory = CreateSessionFactory();
private static ISessionFactory CreateSessionFactory()
{
var cfg = new Configuration().Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nhibernate.config"));
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName, System.Environment.MachineName);
NHibernateProfiler.Initialize();
return cfg.BuildSessionFactory();
}
public static ISession GetSession()
{
return SessionFactory.GetCurrentSession();
}
Ich wollte es so einrichten, dass mein MySqlProductsRepository weitergegeben würde und ISession Objekt von Ninject, wenn es erstellt wurde:
public class MySqlProductsRepository : IProductsRepository
{
private readonly ISession _session;
public MySqlProductsRepository(ISession session)
{
_session = session;
}
Und mein Controller würde eine IProductsRepository Instanz übergeben werden:
public class AdminController : Controller
{
private readonly IProductsRepository _productsRepository;
public AdminController(IProductsRepository productsRepository)
{
_productsRepository = productsRepository;
}
MEIN PROBLEM:
ich kann nicht in m scheinen, um herauszufinden y IoC-Container, in dem ich mein IProductsRepository an mein Repository anbinde, wie man eine ISession registriert, wie man eine ISession an mein MyProductsRepository-Objekt abgibt, wenn es erstellt wird, und ein MyProductsRepository-Objekt an meinen Controller übergibt?
+1 fantastische Sachen - füllt eine Lücke auf dem Markt - das verdient eine FAQ Antwort auf diese Art von Frage hier zu werden! –
Danke Bob, perfekt, werde beginnen, sie jetzt zu lesen, sollte eine große Hilfe sein. – pghtech