Think Like a Computer Scientist.

Welcome to the future.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Web Client Developer Guidance研究1

Posted on 2010-05-18 19:40  TerabyteX  阅读(329)  评论(0编辑  收藏  举报

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);
}