ObjectBuilder已经默认制定了很多创建策略,可能看到这么多策略加上整个地方架构满天飞,反而搞不清楚如何去应用ObjectBuilder创建我们需要的对象了。研究它的原理只是为了更好的发挥它的功效,本系列将详细解析ObjectBuilder中的所有默认创建策略的原理以及应用:
ObjectBuilder之创建策略祥解(一)
-Written by 浪子 [walkingboy.cnblogs.com]
前言:
ObjectBuilder已经默认制定了很多创建策略,可能看到这么多策略加上整个地方架构满天飞,反而搞不清楚如何去应用ObjectBuilder创建我们需要的对象了。研究它的原理只是为了更好的发挥它的功效,本系列将详细解析ObjectBuilder中的所有默认创建策略的原理以及应用:
1、类型映射策略(TypeMappingStrategy):
上文 讲过,策略的具体行为都是由他们各自所对应的方针来决定的。类型映射策略也有其映射方针:
![](/images/cnblogs_com/walkingboy/55704/r_TypeMappingPolicy.png)
方针规范(ITypeMappingPolicy):
public interface ITypeMappingPolicy : IBuilderPolicy
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Maps one Type/ID pair to another.
/// </summary>
/// <param name="incomingTypeIDPair">The incoming Type/ID pair.</param>
/// <returns>The new Type/ID pair.返回映射后的对象类型&标识</returns>
DependencyResolutionLocatorKey Map(DependencyResolutionLocatorKey incomingTypeIDPair);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
具体方针(TypeMappingPolicy):
public class TypeMappingPolicy : ITypeMappingPolicy
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private DependencyResolutionLocatorKey pair;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Initializes a new instance of the <see cref="TypeMappingPolicy"/> class using
/// the provided type and ID.
/// </summary>
/// <param name="type">要翻译的对象类型</param>
/// <param name="id">The new ID to be returned during Map.</param>
public TypeMappingPolicy(Type type, string id)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pair = new DependencyResolutionLocatorKey(type, id);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// See <see cref="ITypeMappingPolicy.Map"/> for more information.
/// </summary>
public DependencyResolutionLocatorKey Map(DependencyResolutionLocatorKey incomingTypeIDPair)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return pair;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
创建策略中的类型映射应用:
public override object BuildUp(IBuilderContext context, Type t, object existing, string id)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//要创建的对象类型DependencyResolutionLocatorKey
DependencyResolutionLocatorKey result = new DependencyResolutionLocatorKey(t, id);
![](/Images/OutliningIndicators/InBlock.gif)
//按照创建的对象类型和标识查询上下文中的映射方针
ITypeMappingPolicy policy = context.Policies.Get<ITypeMappingPolicy>(t, id);
![](/Images/OutliningIndicators/InBlock.gif)
if (policy != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//将方针中的翻译对象映射给result
result = policy.Map(result);
![](/Images/OutliningIndicators/InBlock.gif)
//记录BuildUp日志
TraceBuildUp(context, t, id, Properties.Resources.TypeMapped, result.Type, result.ID ?? "(null)");
![](/Images/OutliningIndicators/InBlock.gif)
//检测是否能正常转换这两个类型
Guard.TypeIsAssignableFromType(t, result.Type, t);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
//用方针中的具体翻译类型result.Type替换原来要创建的对象类型t,将修改后的创建责任推给下一个节点
![](/Images/OutliningIndicators/InBlock.gif)
return base.BuildUp(context, result.Type, existing, result.ID);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
应用举例:
class Program
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//创建builder 实例
Builder builder = new Builder();
![](/Images/OutliningIndicators/InBlock.gif)
PolicyList policyList = new PolicyList();
![](/Images/OutliningIndicators/InBlock.gif)
//创建自己的映射方针,目标类型typeof(MyConcreteClass),被翻译类型typeof(MyAbstractClass),标识"myclass"
![](/Images/OutliningIndicators/InBlock.gif)
policyList.Set<ITypeMappingPolicy>(new TypeMappingPolicy(typeof(MyConcreteClass), null), typeof(MyAbstractClass), "myclass");
![](/Images/OutliningIndicators/InBlock.gif)
MyAbstractClass myclass = builder.BuildUp<MyAbstractClass>(null, "myclass", null, policyList);
![](/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(myclass.GetType().ToString());
![](/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
//抽象基类
public abstract class MyAbstractClass
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//抽象类具体定义
}
![](/Images/OutliningIndicators/None.gif)
//具体实现类
public class MyConcreteClass : MyAbstractClass
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
//具体类个性化的定义
private string test;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
我们可以看到结果会输出:
MyConcreteClass
可见我们通过指定方针就可以轻松的将抽象类或者接口类映射成为具体的实例类,而且这种转换是安全的
//检测是否能正常转换这两个类型
Guard.TypeIsAssignableFromType(t, result.Type, t);
![](/Images/OutliningIndicators/None.gif)