[TestMethod]
public void CanCreateInstances() // 测试是否能够创建实例
{
Builder builder = new Builder(); //对象建造器
Locator locator = CreateLocator(); //定位器
ConstructorPolicy policy = new ConstructorPolicy(); //建立一个构造策略
policy.AddParameter(new ValueParameter<int>(12)); //为策略加入参数
builder.Policies.Set<ICreationPolicy>(policy, typeof(SimpleObject), null); //将此策略加入建造器
SimpleObject m1 = builder.BuildUp<SimpleObject>(locator, null, null); //建造对象
SimpleObject m2 = builder.BuildUp<SimpleObject>(locator, null, null);
Assert.IsNotNull(m1);
Assert.IsNotNull(m2);
Assert.AreEqual(12, m1.IntParam);
Assert.AreEqual(12, m2.IntParam);
Assert.IsTrue(m1 != m2);
}
public void CanCreateInstances() // 测试是否能够创建实例
{
Builder builder = new Builder(); //对象建造器
Locator locator = CreateLocator(); //定位器
ConstructorPolicy policy = new ConstructorPolicy(); //建立一个构造策略
policy.AddParameter(new ValueParameter<int>(12)); //为策略加入参数
builder.Policies.Set<ICreationPolicy>(policy, typeof(SimpleObject), null); //将此策略加入建造器
SimpleObject m1 = builder.BuildUp<SimpleObject>(locator, null, null); //建造对象
SimpleObject m2 = builder.BuildUp<SimpleObject>(locator, null, null);
Assert.IsNotNull(m1);
Assert.IsNotNull(m2);
Assert.AreEqual(12, m1.IntParam);
Assert.AreEqual(12, m2.IntParam);
Assert.IsTrue(m1 != m2);
}
[TestMethod]
public void CanCreateSingleton()
{
Builder builder = new Builder();
Locator locator = CreateLocator();
ConstructorPolicy policy = new ConstructorPolicy();
policy.AddParameter(new ValueParameter<int>(12));
builder.Policies.Set<ICreationPolicy>(policy, typeof(SimpleObject), null);
//为SimpleObject添加单实例策略。
builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), null);
SimpleObject m1 = builder.BuildUp<SimpleObject>(locator, null, null);
SimpleObject m2 = builder.BuildUp<SimpleObject>(locator, null, null);
Assert.AreSame(m1, m2);
}
public void CanCreateSingleton()
{
Builder builder = new Builder();
Locator locator = CreateLocator();
ConstructorPolicy policy = new ConstructorPolicy();
policy.AddParameter(new ValueParameter<int>(12));
builder.Policies.Set<ICreationPolicy>(policy, typeof(SimpleObject), null);
//为SimpleObject添加单实例策略。
builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(SimpleObject), null);
SimpleObject m1 = builder.BuildUp<SimpleObject>(locator, null, null);
SimpleObject m2 = builder.BuildUp<SimpleObject>(locator, null, null);
Assert.AreSame(m1, m2);
}