2010-11-20 14 views
0

NHibernate kullanıyorum ve bir İşlevi döndüren bir yöntemi olan bir BootStrapper sınıfım var. Veri Havuzu sınıfımda bir İşlevi Nesnesini geçiriyorum. Denetleyicimde, kurucusunu bir İfade edici nesneden geçiriyorum.Sınıfı Oluşturucu'ya kaydetme ve bu sınıfı Oluşturucu'ya enjekte etme

İkincil Depomu Depo sınıfına bağlamada başarılı oldum, ancak Repository sınıfımı nasıl bağlayacağımı/kaydettireceğimi anlayamıyorum. Böylece, ActiveStrapper sınıfından bir ISession nesnesi alındığında ve denetleyicime bağlandığında Oluşturulduğu zaman bir İfade edici nesne elde etmek için.

Benim Kod:

public interface IProductsRepository 
{ 
    IQueryable<Product> Products { get; } 
    void SaveProduct(Product product); 
} 


public class MySqlProductsRepository : IProductsRepository 
{ 
    private readonly ISession _session; 

    public MySqlProductsRepository(ISession session) 
    { 
     _session = session; 
    } 

    public IQueryable<Product> Products 
    { 
     get 
     { 
      return _session.CreateCriteria(typeof (Product)).List<Product>().AsQueryable(); 
     } 
    } 


public class BootStrapper 
{ 

    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(); 
    } 
} 


public class AdminController : Controller 
{ 
    private readonly IProductsRepository _productsRepository; 

    public AdminController(IProductsRepository productsRepository) 
    { 
     _productsRepository = productsRepository; 
    } 
} 

public class NinjectControllerFactory : DefaultControllerFactory 
{ 
    private readonly IKernel _kernel = new StandardKernel(new DaisyBlossomsServices()); 

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) 
    { 
     if (controllerType == null) 
      return null; 
     return (IController) _kernel.Get(controllerType); 
    } 

    public class DaisyBlossomsServices : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<IProductsRepository>().To<MySqlProductsRepository>(); 

     } 
    } 
} 

nasıl ISession onun yapıcısı için bir ISession nesnesi alır böylece MySqlProductsRepository bağlanan yoktur ve nasıl bu kadar benim denetleyicisi yapıcısına bir IProductsRepository aldığı bağlama mı?

cevap

2

ISession'ı bir Sağlayıcıya bağlayabilirsiniz. Bu durumda, Provider'dan devralmak ve CreateInstance yöntemini uygulamak için BootStrapper sınıfını değiştirebilirsiniz. Bundan sonra

public class BootStrapper : Provider<ISession> 
    { 
     ..... 
     protected override ISession CreateInstance(IContext context) 
     { 
      return GetSession(); 
     } 
     .... 
    } 

sadece modülüne bağlanarak ekleyin:

public class DaisyBlossomsServices : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<IProductsRepository>().To<MySqlProductsRepository>(); 
      Bind<ISession>().ToProvider<BootStrapper>(); 
     } 
    } 

yapmalıyım Bu o şekilde görünecektir

.

İlgili konular