从Ninject.MVC3移动到StructureMap

时间:2014-01-11 08:48:04

标签: c# asp.net asp.net-mvc asp.net-mvc-3 structuremap

我认为这是一个非常简单的问题,但我刚开始使用C#中的ASP.NET。 我在NinjectModule中有以下类,我需要你的帮助如何在StructureMap中做同样的事情。

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using SimpleBlog.Core.Objects;
using NHibernate;
using NHibernate.Cache;
using NHibernate.Tool.hbm2ddl;
using Ninject;
using Ninject.Modules;
using Ninject.Web.Common;

public class RepositoryModule : NinjectModule
{
    public override void Load()
    {
        Bind<ISessionFactory>()
            .ToMethod
            (
                e =>
                    Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>
                        c.FromConnectionStringWithKey("DefaultConnection")))
                    .Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>())
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Post>())
                    //.ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false))
                    .BuildConfiguration()
                    .BuildSessionFactory()
            )
            .InSingletonScope();

        Bind<ISession>()
            .ToMethod((ctx) => ctx.Kernel.Get<ISessionFactory>().OpenSession())
            .InRequestScope();
    }
}

1 个答案:

答案 0 :(得分:1)

如果我得到了正确的括号,这样的东西应该有效:

ObjectFactory.Initialize(config => {
    config.For<ISessionFactory>().Singleton().Use(() => 
      Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>
        c.FromConnectionStringWithKey("DefaultConnection")))
        .Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Post>())
        .BuildConfiguration()
        .BuildSessionFactory()));
    config.For<ISession>().HttpContextScoped()
      .Use(c => c.GetInstance<ISessionFactory>().OpenSession());
});