zoukankan      html  css  js  c++  java
  • Web Client Developer Guidance研究1

    Web Client Guidance是什么?自己去看吧:

    http://webclientguidance.codeplex.com/


    今天对其进行了一些研究,如下:

    1 Microsoft.Practices.Unity中提供了一个接口IUnityContainer,它定义了统一的依赖注入容器的行为,可以通过它来 进行依赖注入

    private readonly IUnityContainer container;

    public ApplicationServicesSupplier(IUnityContainer container)
    {
    this.container = container;
    }

    public void ProfferServices()
    {
    this.container.RegisterType<IRatingsAndReviewsRepository, RatingsAndReviewsRepository>(new

    ContainerControlledLifetimeManager());
    this.container.RegisterType<ISongsRepository, SongsRepository>(new ContainerControlledLifetimeManager());
    this.container.RegisterType<IRatingService, RatingService>(new ContainerControlledLifetimeManager());
    this.container.RegisterType<IAuthenticationInfo, AuthenticationInfo>(new ContainerControlledLifetimeManager());
    this.container.RegisterType<INavigationManager, NavigationManager>(new ContainerControlledLifetimeManager());
    }


    IUnityContaine也可以采用配置文件的方式,可以看出,跟Castle有很多相似的地方,毕竟都是IoC容器,原理都是一样的


    2 用户验证,使用IAuthenticationInfo接口和AuthenticationInfo类


    3 BaseRegistrar中有个CreateInstance方法,在其代码中对类的构造函数个数进行了约束,要求构造函数个数只能是一个

    ConstructorInfo[] constructors = typeToCreate.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
    if (constructors.Length != 1)
    {
    throw new InvalidOperationException();
    }

    因为后面会用到Activator.CreateInstance这个方法,MSDN里的解释是“使用与指定参数匹配程度最高的构造函数创建指定类型的实例”

    return Activator.CreateInstance(typeToCreate, parameterValues.ToArray());


    4 TypesProvider相关

    IsConcreteInstanceOfType方法,看方法的源码

    public static bool IsConcreteInstanceOfType<T>(Type type)
    {
    return type != null && !type.IsAbstract && typeof(T).IsAssignableFrom(type);
    }

    要求type不能为空,不能是抽象类,并且“确定当前的Type的实例是否可以从指定Type的实例分配”;自己编写代码测试后,得到如下结论:“引用类 型间要满足当前的Type必须是指定Type的父类或同一个类;值类型间不满足;数组类型间:如果是值类型要满足统一的数据类型且维数一致,如果是引用类 型要满足当前的Type必须是指定Type的父类或同一个类且维数一致;泛型与非泛型间不满足;泛型与泛型间需要统一的类型”

    HasDefaultConstructor方法,用来判断类是否有默认的构造函数,源码如下

    public static bool HasDefaultConstructor(Type type)
    {
    return type != null && type.GetConstructor(Type.EmptyTypes) != null;
    }


    5 通用的扩展,包括UnityServiceLocator、UnityBootstrapper、UnityControllerFactory和 UnityMvcBootstrapper;其中UnityControllerFactory继承自 System.Web.Mvc.DefaultControllerFactory;UnityMvcBootstrapper继承自 UnityBootstrapper,而UnityBootstrapper又继承自ApplicationBootstrapper,下面列出一些重要 的代码,不做分析了,有兴趣的可以自己去看看

    ApplicationBootstrapper中的两个方法:

    public virtual IEnumerable<Type> GetRoutesSuppliers()
    {
    return this.typesProvider.FilterTypesInAssemblies(TypesProvider.IsConcreteInstanceOfType<IRoutesSupplier>);
    }

    public virtual IEnumerable<Type> GetServicesSuppliers()
    {
    return this.typesProvider.FilterTypesInAssemblies(TypesProvider.IsConcreteInstanceOfType<IServicesSupplier>);
    }

    UnityBootstrapper中的构造函数

    public UnityBootstrapper(IUnityContainer container)
    : base(new UnityServiceLocator(container))
    {
    this.UnityContainer = container;

    Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => this.ServiceLocator);
    }

    UnityMvcBootstrapper中的构造函数

    public UnityMvcBootstrapper(IUnityContainer container)
    : base(container)
    {
    IControllerFactory controllerFactory = new UnityControllerFactory(container);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }

    作者:Lucifer Xue
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    nodejs async waterfull 小白向
    nodejs async series 小白向
    MySQL 分区介绍总结
    cocos2d-x 一些实用的函数
    LeetCode(61)-Valid Palindrome
    ganglia错误解决
    (6)uboot具体解释——关闭缓存和mmu
    Linux下设置MySQL不区分大写和小写
    火狐与IE的7个JavaScript差异
    商业研究(8):汽车交通
  • 原文地址:https://www.cnblogs.com/SevenXue/p/1738582.html
Copyright © 2011-2022 走看看