当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET MVC Model绑定(三)

ASP.NET MVC Model绑定(三)

2019年05月10日  | 移动技术网IT编程  | 我要评论

cx884航班,被拐11年后认亲,人寿保险理赔调查

asp.net mvc model绑定(三)

前言

看过前两篇的朋友想必对model绑定有个大概的了解,然而mvc框架给我们提供了更高的可扩展性的提供程序模式,也就是本篇的主题了,会讲解一下model绑定器提供程序的实现以及解决一下上篇遗留的问题。

第一个问题是modelbinderprovidercollection类型的执行过程?

还有个本篇的问题就是同样的向上下文中注册model绑定器和model绑定器提供程序,哪一个优先级更高?

model绑定

imodelbinder、自定义model绑定器简单实现model绑定器在mvc框架中的位置mvc中的默认model绑定器生成过程imodelbinderprovider的简单应用ivalueprovider在mvc框架中生成的位置以及过程ivalueprovider的应用场景ivalueprovider的实现之namevaluecollectionvalueprovider

imodelbinderprovider的简单应用

首先我们先看一下imodelbinderprovider类型的定义,代码1-1:。

代码1-1

    public interface imodelbinderprovider
    {
        // 摘要:
        //     返回指定类型的模型联编程序。
        //
        // 参数:
        //   modeltype:
        //     模型的类型。
        //
        // 返回结果:
        //     指定类型的模型联编程序。
        imodelbinder getbinder(type modeltype);
    }

在代码1-1中我们看出,其中的getbinder()方法是根据viewmodel的类型来做一些操作,最后返回model绑定器。现在我们自定义实现一个model绑定器提供程序代码1-2。

代码1-2

using system.web.mvc;
using consoleapplication2;

namespace mvcapplication.infrastructure
{
    public class mycustommodelbinderprovider : imodelbinderprovider
    {
        public imodelbinder getbinder(type modeltype)
        {
            if (modeltype == null)
            {
                throw new argumentnullexception("modeltype");
            }
            if (modeltype == typeof(customer))
            {
               //返回对应customer类型的model绑定器
            }
            return null;
        }
    }
}

在代码1-2中我们根据modeltype判断是否是customer类型,然后返回对应customer类型的model绑定器。为什么这里的实现是空的,因为我想把我们前面讲解过的ioc框架用起来,让model绑定器提供程序跟model绑定器解除耦合,想把ioc框架的应用定义在当前系统的上下文中,我们看一下代码实现,代码1-3。

代码1-3

using system.web.mvc;
using ninject;
using system.componentmodel;
using system.componentmodel.design;
using mvcapplication.infrastructure.ninjectcontrollerpart;


namespace mvcapplication
{
    public class mvcsystemcontext
    {
        private static mvcsystemcontext _mvcsystemcontext;

        public static mvcsystemcontext context
        {
            get
            {
                if (_mvcsystemcontext == null)
                {
                    _mvcsystemcontext = new mvcsystemcontext();
                }
                return _mvcsystemcontext;
            }
        }

        private servicecontainer _servicecontainer;

        private mvcsystemcontext()
        {
            _servicecontainer = new servicecontainer();
            _servicecontainer.addservice(typeof(ninjectcontroller),ninjectcontroller.instance);
        }

        public ninjectcontroller ninjectcontroller
        {
            get
            {
                return (ninjectcontroller)_servicecontainer.getservice(typeof(ninjectcontroller));
            }
        }


    }
}

代码1-3当中就是我定义的当前系统上下文了,只不过这个是给自己用的,上下文对象中想必是不会把所用到的所有数据或者是功能都添加在里面的,只是添加个引用而已,如代码1-3中的ninjectcontroller属性,ninjectcontroller属性对应的类型就是ninjectcontroller类型,ninjectcontroller类型的作用就是提供ioc框架的功能,我们看一下代码1-4中对于ninjectcontroller类型的定义。

代码1-4

using ninject;

namespace mvcapplication.infrastructure.ninjectcontrollerpart
{
    public class ninjectcontroller
    {
        private static ninjectcontroller _instance;

        public static ninjectcontroller instance
        {
            get 
            {
                return _instance = new ninjectcontroller();
            }
        }

        private ikernel _ninjectkernel;

        private ninjectcontroller()
        {
            _ninjectkernel = new standardkernel();
        }

        public void addkernelbind()where u:t
        {
            _ninjectkernel.bind().to();
        }

        public t getvaluetype(type keytype)
        {
            var valuetype = _ninjectkernel.get(keytype);
            return (t)valuetype;
        }
    }
}

其中对于ninject这个ioc框架进行了一个最基础的功能封装,有的朋友可能会问为什么不公开个一个属性,何必这样多此一举,因为我对ninject的使用也不是很熟练,对于这部分的封装我只是让其简单的公开了两个功能,一个是绑定一个是获取值,这样让这部分内容还在我的可控范围内,如果是公开属性的话,其他人的胡乱使用导致错误的话是不可控的。

切回主题,这样基础定义好了过后,我们再修改1-2中的代码,把具体实现给加上,示例代码1-5所示。

代码1-5

if (modeltype == typeof(customer))
{
    //返回对应customer类型的model绑定器
    return mvcsystemcontext.context.ninjectcontroller.getvaluetype(typeof(imodelbinder));
}

可以看到代码1-5中,根据我们自定义上下文中的提供的ioc功能获取到绑定在ioc框架中的值,那么绑定又是在哪里呢?跟asp.net mvc model绑定(一)所演示的那样,还是在项目的global.asax文件中的mvcapplication类型的application_start()方法中添加如代码1-6。

代码1-6

mvcsystemcontext.context.ninjectcontroller.addkernelbind();
modelbinderproviders.binderproviders.add(new mycustommodelbinderprovider());

代码1-6分别做了两个操作,先是把对应customer类型的model绑定器注册到了我们自定义上下文的ioc中,然后再把针对处理customer类型的model绑定器提供程序注册到系统中。运行结果如图1.

图1

\

其中涉及到所有部分的代码和asp.net mvc model绑定(一)篇幅中的一样,所以这里就没有列举了。

在此我们根据上篇中最后图2所示的那样,可以判断出modelbinderprovidercollection类型的执行过程是根据当前parameterdescriptor类型所提供的model类型对比我们注册到或者是系统默认提供的model绑定器提供程序集合,如果有是针对parameterdescriptor类型所提供的model类型(上述示例中是customer类型)则会有model绑定器的返回,然后再根据model绑定器进行model绑定。

好了现在第一个问题解决了,来解决第二个问题。来看代码1-7所示。

代码1-7

    public class mycustommodelbinder:imodelbinder
    {
        public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext)
        {
            return new customer()
            {
                customerid = "010",
                name = "测试人员",
                registrationdate = datetime.now,
                address = new address()
                {
                    addressname = "天空之城"
                }
            };
        }
    }

    public class mycustommodelbinder_test : imodelbinder
    {
        public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext)
        {
            return new customer()
            {
                customerid = "010",
                name = "测试人员",
                registrationdate = datetime.now,
                address = new address()
                {
                    addressname = "这里是根据model绑定器绑定执行的model"
                }
            };
        }
    }

看到代码1-7中的mycustommodelbinder_test 类型内部customer类型实例内部的addressname值已经更改的和之前的不一样了。再看一下注册端的修改,示例代码1-8。

代码1-8

modelbinders.binders.add(typeof(customer), new binders.mycustommodelbinder_test());
mvcsystemcontext.context.ninjectcontroller.addkernelbind();
modelbinderproviders.binderproviders.add(new mycustommodelbinderprovider());

代码1-8中,我们把新定义的mycustommodelbinder_test 类型注册到了系统的model绑定器集合中,看一下究竟是哪一个级别更高一点。

来看运行结果图2

图2

\

看到图2这个结果,想必已经知道了是哪个级别更高一点了。




作者:金源

出处:https://blog.csdn.net/jinyuan0829

本文版权归作者和csdn共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网