使用Nhibernate的Servicestack UserAuth持久性(使用ServiceStack.Authentication.NHibernate)

时间:2014-02-05 08:25:36

标签: nhibernate fluent-nhibernate servicestack

我正在尝试为Nhibernate使用ServiceStack IUserAuthRepository实现。 我在我的服务IOC容器中注册了NHibernateUserAuthRepository,但我不知道如何告诉Nhibernate将AuthUser和角色映射到数据库表。

我的映射发生在容器实例化ISessionFactory时(使用FluentNhibernate)。

这是AppHost代码:

container.Register<ICacheClient>(new MemoryCacheClient());

container.Register<IDatabaseFactory>(c => new Oracle10DatabaseFactory(_DomainAssembly, _DomainAssemblyName,
                                                            c.Resolve<ConfigurationParameters>()));
// Register EventPublisher
        container.RegisterAutoWiredAs<EventPublisher, IEventPublisher>().ReusedWithin(ReuseScope.Request);
        container.RegisterAutoWiredAs<EventPublisherInterceptor, IInterceptor>().ReusedWithin(ReuseScope.Request);

// Register Session & UnitOfWork
container.Register<NHibernate.ISession>(item =>
container.Resolve<IDatabaseFactory>().SessionFactory.OpenSession(new EventPublisherInterceptor(container.Resolve<IEventPublisher>())))
            .ReusedWithin(ReuseScope.Request);

container.Register<IUnitOfWork>(item => new UnitOfWork(container.Resolve<NHibernate.ISession>())).ReusedWithin(ReuseScope.Request);

Plugins.Add(new AuthFeature(() => new AuthUserSession(),
                new IAuthProvider[] { new CredentialsAuthProvider() }));

这是Oracle10DatabaseFactory构造函数:

public Oracle10DatabaseFactory(Assembly assembly, string namespace, ConfigurationParameters parameters)
{
    var fileCache = new ConfigurationFileCache(assembly, parameters.PathToConfigurationFolder);
    var config = fileCache.LoadConfigurationFromFile();
    if (config == null)
    {                    
        var mapping = AutoMap.Assembly(assembly, new MappingConfiguration(new List<string>() { namespace }))
                    .Conventions.Add<ReferenceIndexConvention>()
                    .Conventions.Add<GuidIndexConvention>()
                    .UseOverridesFromAssemblyOf<MappingConfiguration>();


        this.sessionFactory =
                        Fluently.Configure()
                        .Database(OracleClientConfiguration.Oracle10.ConnectionString(c => c.FromConnectionStringWithKey("ZetesMobility_DataAccess_Connectionstring_Oracle"))
                            .UseReflectionOptimizer())


                        .Mappings(m => m.AutoMappings.Add(mapping)) //.Add(baseMapping)
                        .CurrentSessionContext("web")
                        .ExposeConfiguration(c => c.BuildSchema(NHibernateExtensions.RecreateSchema()))
                        .ExposeConfiguration(x => x.SetProperty("hbm2ddl.keywords", "auto-quote"))
                        .ExposeConfiguration(fileCache.SaveConfigurationToFile)
                        .BuildSessionFactory();

        config = fileCache.LoadConfigurationFromFile();
        this.sessionFactory = config.BuildSessionFactory();
    }
    else
    {
        this.sessionFactory = config.BuildSessionFactory();
    }
}

如何将身份验证类添加到映射配置?

非常感谢大家。

1 个答案:

答案 0 :(得分:1)

在你的Fluently.Config()中,部分映射(m - &gt; m ...,你需要告诉FluentNhibernate在包中包含映射,如下所示:

            .Mappings(m =>
                m.FluentMappings
                    .AddFromAssemblyOf<UserAuthPersistenceDto>())

你的应该最终看起来像

            .Mappings(m =>
            {
                m.AutoMappings.Add(mapping);
                m.FluentMappings
                    .AddFromAssemblyOf<UserAuthPersistenceDto>();
            })