Rhino.Security and IEntityInformationExtractor

强颜欢笑 提交于 2019-12-06 08:00:45

At the end I couldn't manage to make it work with Ninject so I switched to StructureMap.
Rhino.Security works with MS ServiceLocator to resolve IEntityInformationExtractor. I've found a ServiceLocator adapter for StructureMap

So I've referenced in my project the following assemblies:

Microsoft.Practices.ServiceLocation
StructureMap
StructureMapAdapter

changed my code to use StructureMap:

public static class Bootstrapper
    {
        public static void Initialize()
        {
            ObjectFactory.Initialize(cfg =>
            {
                cfg.AddRegistry<StructureMapRegistry>();
            });
            ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(ObjectFactory.Container));
        }
    }

and my StructureMapRegistry class:

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        string ConnDb = "Data Source=(local); Initial Catalog=RhinoSecurity_Test; Trusted_Connection=true;";

        For<ISessionFactory>()
            .Singleton()
            .TheDefault.Is.ConstructedBy(() => new NHSessionFactory(ConnDb, false).SessionFactory);
        For<ISession>()
            .Singleton()
            .TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());
        For<IAuthorizationRepository>()
             .Use<AuthorizationRepository>();
        For<IPermissionsService>()
            .Use<PermissionsService>();
        For<IAuthorizationService>()
            .Use<AuthorizationService>();
        For<IPermissionsBuilderService>()
            .Use<PermissionsBuilderService>();
        For<IEntityInformationExtractor<Model.Account>>()
            .Use(p =>
                {
                return (new AccountInfromationExtractor(p.GetInstance<ISession>()));
                });
    }
}

I hope this might help someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!