【问题标题】:AOP: Custom Model Binder Attribute using NinjectAOP:使用 Ninject 的自定义模型绑定器属性
【发布时间】:2011-04-13 22:54:37
【问题描述】:

简而言之:我正在尝试创建一个自定义模型绑定器,它将接受用户类型并获取他们的 id,然后使用服务类来检索强类型对象。

如果有更好的方法,请告诉我。

阐述:

我在我的 DomainService 层中设置了所有绑定的 ninject,3 个 web ui 连接到域服务层。每个 asp.net mvc 应用程序将绑定加载到内核中。

//我的自定义模型绑定器

public class UserModelBinder : IModelBinder
    {
        private IAuthenticationService auth;

        public UserModelBinder(IAuthenticationService _auth, EntityName type, 
        string loggedonuserid)
        {
            this.auth = _auth;
            CurrentUserType = type;
            CurrentUserId = loggedonuserid;
        }


        public EntityName CurrentUserType { get; private set; }
        private string CurrentUserId {  get; set; }

        public object BindModel(ControllerContext controllerContext, 
        ModelBindingContext bindingContext)
        {
            object loggedonuser = null;

            if (CurrentUserType == EntityName.Client)
                loggedonuser = GetLoggedOnClientUser(CurrentUserId);
            else if (CurrentUserType == EntityName.Shop)
                loggedonuser = GetLoggedOnShopUser(CurrentUserId);
            else
                throw new NotImplementedException();

            return loggedonuser;
        }

        public ClientUser GetLoggedOnClientUser(string loggedonuserid)
        {
            var user = _auth.GetLoggedOnClientUser(loggedonuserid);
            if (user == null)
                throw new NoAccessException();

            return user;
        }

        public ShopUser GetLoggedOnShopUser(string loggedonuserid)
        {
            var user = _auth.GetLoggedOnShopUser(loggedonuserid);
            if (user == null)
                throw new NoAccessException();

            return user;
        }

    }

我的 Global.aspx.cs

// using NInject to override application started
        protected override void OnApplicationStarted()
        {
            AreaRegistration.RegisterAllAreas();
            // hand over control to NInject to register all controllers
            RegisterRoutes(RouteTable.Routes);



 //how do I instantiate?
            ModelBinders.Binders.Add(typeof(object), new 
            UserModelBinder(null,EntityName.Client, User.Identity.Name));

        }

我的问题是 IAuthentication 是一项服务,它连接到存储库等其他东西,我该如何正确地实例化它?我应该创建一个新的 NinjectModule 吗?我对此感到非常困惑,因此非常感谢任何帮助。我试图通过 Container.Get(); - 但它是空的......

注意:我创建模型绑定器的原因 - 所有控制器都需要用户类型,因为我的服务层需要哪种类型的用户正在发出请求,我的服务层中的大多数方法都会有重载ShopUser 或 ClientUser 或系统中的任何其他用户的一件事......

编辑: 我可以很容易地在我的控制器中调用 IAuthenticationService 并获取用户类型并传递到我的域服务层以处理相关任务,但我只想知道如何使用 ModelBindings (以及这样做是否有意义)那样)。

Edit2:是否有使用带有 AOP 的自定义属性和自定义属性调用/绑定/获取 ISomethingService 实例的工作示例?

【问题讨论】:

    标签: c# asp.net-mvc-2 .net-3.5 aop ninject


    【解决方案1】:

    您可以在此处使用服务定位器模式。每次需要绑定东西时,将 Ninject Container (IKernel?) 传递给构造函数并解析 AuthenticationService。

    对此的改进可能是有一个构造函数参数 Func,您可以在其中传递函数来解析服务。这将更加明确并消除对 Ninject 的依赖。像这样的:

    public class MyModelBinder : IModelBinder
    {
        Func<IAuthenticationService> _resolveAuthService;
    
        public MyModelBinder(Func<IAuthenticationService> resolveAuthService)
        {
             _resolveAuthService = resolveAuthService;
        }
    
        public override object Bind(Context c)
        {
            var authService = _resolveAuthService();
    
            authService.GetSomething();
    
            // etc...
        }
    }
    

    【讨论】:

    • @rmac:服务定位器模式看起来不错,但是 AOP 方法可能会更好......我正在更改我的帖子标题以稍作修改,对不起!
    • 您是否也删除了 MVC 版本 2 的约束? MVC 3 对注入有更好的支持,我相信有办法让它在每个请求中解析一个新的模型绑定器实例。那将是最佳解决方案。
    • 我很难想象你想用 AOP 做什么。我认为你需要详细说明一下。
    • 不,我删除了 asp.net mvc2,但我为 asp.net 3.5 保留了标签-这基本上排除了 .net 4 运行时,我打算将 asp.net mvc 更改为 asp.net mvc2 以避免任何冲突......对不起!我知道 asp.net mvc3 有 IModelBinderProvider 可以很好地适用于上述场景,但我没有 asp.net2 的选项...
    • @rcmac 我将如何在 Global.asax 中实例化或注册这个类? ValueProviderFactories.Factories.Add(new MyModelBinder(???));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多