NHIbernate&除非我调用session.Flush(),否则Rhino-Security不会保存

时间:2010-08-26 00:13:01

标签: c# nhibernate rhino-commons

我在使用nHibernate和Rhino.Security时遇到了问题。经过几个小时的努力才能获得正确的配置设置,我终于让代码运行没有任何错误。但是,除非我调用session.Flush()。

,否则不会将任何条目保存到数据库

查看网上的各种例子;我不应该打电话给。

这是我的配置代码:

var cfg = new Configuration()
             .SetProperty(Environment.ConnectionDriver, typeof(SqlClientDriver).AssemblyQualifiedName)
             .SetProperty(Environment.Dialect, typeof(MsSql2008Dialect).AssemblyQualifiedName)
             .SetProperty(Environment.ConnectionString, "………")
             .SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName)
             .SetProperty(Environment.ReleaseConnections, "on_close")
             .SetProperty(Environment.UseSecondLevelCache, "true")
             .SetProperty(Environment.UseQueryCache, "true")
             .SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName)
             .AddAssembly("GA.CAP.Website")
        ;

        Security.Configure<AspNetUser>(cfg, SecurityTableStructure.Prefix);

        var factory = cfg.BuildSessionFactory();

        var session = factory.OpenSession();


        var authorizationRepository = new AuthorizationRepository(session);
        IoC.Container.RegisterInstance<IAuthorizationRepository>(authorizationRepository);
        var permissionBuilderService = new PermissionsBuilderService(session, authorizationRepository);
        IoC.Container.RegisterInstance<IPermissionsBuilderService>(permissionBuilderService);
        var permissionService = new PermissionsService(authorizationRepository, session);
        IoC.Container.RegisterInstance<IPermissionsService>(permissionService);
        var authService = new AuthorizationService(permissionService, authorizationRepository);
        IoC.Container.RegisterInstance<IAuthorizationService>(authService);

测试代码:

    authorizationRepository.CreateUsersGroup("GAAdmins");
    var group = authorizationRepository.GetUsersGroupByName("GAAdmins");

GetUsersGroupByName调用返回null。如果我在两次调用之间添加一个session.Flush调用,它可以正常工作并返回该组。

基于各种博客中的示例,例如this one,我不应该调用flush。此外,Rhino.Security代码中包含的测试用例不会进行任何刷新,如下所示:

直接来自Rhino.Security的测试用例夹具:

    // on first deploy
    Operation operation = authorizationRepository.CreateOperation("/Account/View");
    // when creating account
    UsersGroup group = authorizationRepository.CreateUsersGroup("Belongs to " + account.Name);

    // setting permission so only associated users can view
    permissionsBuilderService
        .Allow(operation)
        .For(group)
        .On(account)
        .DefaultLevel()
        .Save();

    // when adding user to account
    authorizationRepository.AssociateUserWith(user, group);

    bool allowed = authorizationService.IsAllowed(user, account, "/Account/View");
    Assert.True(allowed);

我在某处缺少某些设置吗?

谢谢,

瑞克

1 个答案:

答案 0 :(得分:3)

这是预期的,RS使用与您的代码相同的会话,并在内部调用Flush可能会导致意外后果。 提交您的交易或致电Flush