|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_SingletonTest
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new SingletonStrategy());
context.InnerChain.Add(new CreationStrategy());
context.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(TestObject), null);
context.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
TestObject obj1 = (TestObject)context.HeadOfChain.BuildUp(context,
typeof(TestObject), null, null);
TestObject obj2 = (TestObject)context.HeadOfChain.BuildUp(context,
typeof(TestObject), null, null);
if (obj1 == obj2)
Console.WriteLine("Singleton");
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public class TestObject
{
}
}
|
|
public SingletonPolicy(bool isSingleton);
|
|
private void RegisterObject(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
{
if (context.Locator != null)
{
ILifetimeContainer lifetime = context.Locator.Get<ILifetimeContainer>(
typeof(ILifetimeContainer), SearchMode.Local);
if (lifetime != null)
{
ISingletonPolicy singletonPolicy = context.Policies.Get<ISingletonPolicy>(
typeToBuild, idToBuild);
if (singletonPolicy != null && singletonPolicy.IsSingleton)
{
context.Locator.Add(new DependencyResolutionLocatorKey(
typeToBuild, idToBuild), existing);
lifetime.Add(existing);
......
}
}
}
}
|
|
public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
{
DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(
typeToBuild, idToBuild);
if (context.Locator != null && context.Locator.Contains(key, SearchMode.Local))
{
TraceBuildUp(context, typeToBuild, idToBuild, "");
return context.Locator.Get(key);
}
return base.BuildUp(context, typeToBuild, existing, idToBuild);
}
|
|
PS:注意,SingletonStrategy在該物件已經存在於Locator中時,是直接回傳,並不會呼叫後面如MethodExecutionStrategy、PropertySetterStrategy等Strategy。
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_TypeMappingTest
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new TypeMappingStrategy());
context.InnerChain.Add(new CreationStrategy());
ITypeMappingPolicy policy = new TypeMappingPolicy(typeof(TestObject),null);
context.Policies.Set<ITypeMappingPolicy>(policy, typeof(ITestInterface), null);
context.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
ITestInterface obj1 = (ITestInterface)context.HeadOfChain.BuildUp(
context, typeof(ITestInterface), null, null);
obj1.SayHello();
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public interface ITestInterface
{
void SayHello();
}
public class TestObject : ITestInterface
{
public void SayHello()
{
Console.WriteLine("TEST");
}
}
}
|
|
public TypeMappingPolicy(Type type, string id)
|
|
context.Policies.Set<ITypeMappingPolicy>(policy, typeof(ITestInterface), null)
|
|
PS:注意,Type Mapping型別必須相容,如介面->實作、基礎類別->衍生類別。
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_BuildAwareTest
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new CreationStrategy());
context.InnerChain.Add(new BuilderAwareStrategy());
context.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
TestObject obj = (TestObject)context.HeadOfChain.BuildUp(context,
typeof(TestObject), null, null);
context.HeadOfChain.TearDown(context, obj);
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public class TestObject : IBuilderAware
{
#region IBuilderAware Members
public void OnBuiltUp(string id)
{
Console.WriteLine("Object is build up");
}
public void OnTearingDown()
{
Console.WriteLine("Object is TearDown");
}
#endregion
}
}
|
|
object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild);
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_ExistingTest
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new CreationStrategy());
ConstructorPolicy policy = new ConstructorPolicy(new ValueParameter(typeof(string),"id"));
context.Policies.Set<ICreationPolicy>(policy, typeof(TestObject), null);
TestObject obj = (TestObject)context.HeadOfChain.BuildUp(context,
typeof(TestObject), null, null);
TestObject obj2 = (TestObject)context.HeadOfChain.BuildUp(context,
typeof(TestObject), obj, null);
if (obj == obj2)
Console.WriteLine("is same object.");
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public class TestObject
{
private string _id;
public string ID
{
get
{
return _id;
}
}
public TestObject(string id)
{
_id = id;
}
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_IDTesting
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new CreationStrategy());
context.InnerChain.Add(new PropertySetterStrategy());
context.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
PropertySetterInfo pi1 = new PropertySetterInfo("ID", new ValueParameter<string>("ID1"));
PropertySetterPolicy pp1 = new PropertySetterPolicy();
pp1.Properties.Add("ID", pi1);
context.Policies.Set<IPropertySetterPolicy>(pp1, typeof(TestObject), "TO1");
PropertySetterInfo pi2 = new PropertySetterInfo("ID", new ValueParameter<string>("ID2"));
PropertySetterPolicy pp2 = new PropertySetterPolicy();
pp2.Properties.Add("ID", pi2);
context.Policies.Set<IPropertySetterPolicy>(pp2, typeof(TestObject), "TO2");
TestObject obj1 = (TestObject)context.HeadOfChain.BuildUp(context, typeof(TestObject),
null, "TO1");
TestObject obj2 = (TestObject)context.HeadOfChain.BuildUp(context, typeof(TestObject),
null, "TO2");
Console.WriteLine(obj1.ID);
Console.WriteLine(obj2.ID);
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public class TestObject
{
public string _id;
public string ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
}
}
|
|
private object BuildUpNewObject(IBuilderContext context, Type typeToBuild, object existing,
string idToBuild)
{
ICreationPolicy policy = context.Policies.Get<ICreationPolicy>(typeToBuild, idToBuild);
..................
|
|
public DependencyResolutionLocatorKey(Type type, string id)
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_SingletonTwoTest
{
class Program
{
static void Main(string[] args)
{
MyBuilderContext context = new MyBuilderContext();
context.InnerChain.Add(new SingletonStrategy());
context.InnerChain.Add(new CreationStrategy());
context.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(TestObject), "ID1");
context.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(TestObject), "ID2");
context.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
TestObject obj1 = (TestObject)context.HeadOfChain.BuildUp(context, typeof(TestObject),
null, "ID1");
TestObject obj2 = (TestObject)context.HeadOfChain.BuildUp(context, typeof(TestObject),
null, "ID2");
if (obj1 == obj2)
Console.WriteLine("Singleton");
Console.Read();
}
}
internal class MyBuilderContext : BuilderContext
{
public IReadWriteLocator InnerLocator;
public BuilderStrategyChain InnerChain = new BuilderStrategyChain();
public PolicyList InnerPolicies = new PolicyList();
public LifetimeContainer lifetimeContainer = new LifetimeContainer();
public MyBuilderContext()
: this(new Locator())
{
}
public MyBuilderContext(IReadWriteLocator locator)
{
InnerLocator = locator;
SetLocator(InnerLocator);
StrategyChain = InnerChain;
SetPolicies(InnerPolicies);
if (!Locator.Contains(typeof(ILifetimeContainer)))
Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
}
}
public class TestObject
{
}
}
|
|
public enum BuilderStage
{
PreCreation,
Creation,
Initialization,
PostInitialization
}
|
|
public Builder(IBuilderConfigurator<BuilderStage> configurator)
{
Strategies.AddNew<TypeMappingStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<SingletonStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<ConstructorReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<PropertyReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<MethodReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<CreationStrategy>(BuilderStage.Creation);
Strategies.AddNew<PropertySetterStrategy>(BuilderStage.Initialization);
Strategies.AddNew<MethodExecutionStrategy>(BuilderStage.Initialization);
Strategies.AddNew<BuilderAwareStrategy>(BuilderStage.PostInitialization);
Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
if (configurator != null)
configurator.ApplyConfiguration(this);
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_StrategyListTest
{
class Program
{
static void Main(string[] args)
{
MyBuilder builder = new MyBuilder();
ITypeMappingPolicy policy = new TypeMappingPolicy(typeof(TestObject), null);
builder.Policies.Set<ITypeMappingPolicy>(policy, typeof(ITestInterface), null);
ITestInterface obj1 = builder.BuildUp<ITestInterface>(new Locator(), null, null);
Console.Read();
}
}
public class MyBuilder : BuilderBase<BuilderStage>
{
public MyBuilder()
: this(null)
{
}
public MyBuilder(IBuilderConfigurator<BuilderStage> configurator)
{
Strategies.AddNew<CreationStrategy>(BuilderStage.Creation);
Strategies.AddNew<TypeMappingStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<SingletonStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<ConstructorReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<PropertyReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<MethodReflectionStrategy>(BuilderStage.PreCreation);
Strategies.AddNew<PropertySetterStrategy>(BuilderStage.Initialization);
Strategies.AddNew<MethodExecutionStrategy>(BuilderStage.Initialization);
Strategies.AddNew<BuilderAwareStrategy>(BuilderStage.PostInitialization);
Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
if (configurator != null)
configurator.ApplyConfiguration(this);
}
}
public interface ITestInterface
{
}
public class TestObject : ITestInterface
{
}
}
|
|
public BuilderPolicyKey(Type policyType, Type typePolicyAppliesTo, string idPolicyAppliesTo)
|
|
public void Set(Type policyInterface, IBuilderPolicy policy, Type typePolicyAppliesTo,
string idPolicyAppliesTo)
{
BuilderPolicyKey key = new BuilderPolicyKey(policyInterface,
typePolicyAppliesTo, idPolicyAppliesTo);
lock (lockObject)
{
policies[key] = policy;
}
}
|
|
public void Set<TPolicyInterface>(TPolicyInterface policy,
Type typePolicyAppliesTo, string idPolicyAppliesTo)
where TPolicyInterface : IBuilderPolicy
{
Set(typeof(TPolicyInterface), policy, typePolicyAppliesTo, idPolicyAppliesTo);
}
|
|
public TPolicyInterface Get<TPolicyInterface>(Type typePolicyAppliesTo, string idPolicyAppliesTo)
where TPolicyInterface : IBuilderPolicy
{
return (TPolicyInterface)Get(typeof(TPolicyInterface), typePolicyAppliesTo, idPolicyAppliesTo);
}
public IBuilderPolicy Get(Type policyInterface, Type typePolicyAppliesTo, string idPolicyAppliesTo)
{
BuilderPolicyKey key = new BuilderPolicyKey(policyInterface,
typePolicyAppliesTo, idPolicyAppliesTo);
lock (lockObject)
{
IBuilderPolicy policy;
if (policies.TryGetValue(key, out policy))
return policy;
BuilderPolicyKey defaultKey = new BuilderPolicyKey(policyInterface, null, null);
if (policies.TryGetValue(defaultKey, out policy))
return policy;
return null;
}
}
|

|
public interface IEventSetterInfo
{
object GetValue(IBuilderContext context, Type type, string id, EventInfo propInfo);
EventInfo SelectEvent(IBuilderContext context, Type type, string id);
}
|
|
public sealed class EventSetterInfo : IEventSetterInfo
{
private string _name = null;
private IParameter _value = null;
#region IEventSetterInfo Members
public object GetValue(IBuilderContext context, Type type, string id, EventInfo propInfo)
{
return _value.GetValue(context);
}
public EventInfo SelectEvent(IBuilderContext context, Type type, string id)
{
return type.GetEvent(_name);
}
#endregion
public EventSetterInfo(string name,IParameter value)
{
_name = name;
_value = value;
}
}
|
|
public interface IEventSetterPolicy : IBuilderPolicy
{
Dictionary<string, IEventSetterInfo> Events { get;}
}
|
|
public sealed class EventSetterPolicy : IEventSetterPolicy
{
private Dictionary<string, IEventSetterInfo> _events = new Dictionary<string, IEventSetterInfo>();
#region IEventPolicy Members
public Dictionary<string, IEventSetterInfo> Events
{
get
{
return _events;
}
}
#endregion
}
|
|
public class EventSetterStrategy : BuilderStrategy
{
public override object BuildUp(IBuilderContext context, Type typeToBuild,
object existing, string idToBuild)
{
if (existing != null)
InjectEvents(context, existing, idToBuild);
return base.BuildUp(context, typeToBuild, existing, idToBuild);
}
private void InjectEvents(IBuilderContext context, object obj, string id)
{
if (obj == null)
return;
Type type = obj.GetType();
IEventSetterPolicy policy = context.Policies.Get<IEventSetterPolicy>(type, id);
if (policy == null)
return;
foreach (IEventSetterInfo eventSetterInfo in policy.Events.Values)
{
EventInfo eventInfo = eventSetterInfo.SelectEvent(context, type, id);
if (eventInfo != null)
{
if (TraceEnabled(context))
TraceBuildUp(context, type, id, "Event Setter", eventInfo.Name);
eventInfo.AddEventHandler(obj,
eventSetterInfo.GetValue(context, type, id, eventInfo) as Delegate);
}
}
}
}
|
|
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace EventSetterTest
{
class Program
{
static void Main(string[] args)
{
Builder builder = new Builder();
builder.Strategies.AddNew<EventSetterStrategy>(BuilderStage.Initialization);
IEventSetterPolicy policy = new EventSetterPolicy();
EventHandler handler = new EventHandler(CallHandler);
policy.Events.Add("Call", new EventSetterInfo("Call",
new ValueParameter(typeof(EventHandler), handler)));
builder.Policies.Set<IEventSetterPolicy>(policy, typeof(TestObject), null);
TestObject obj = builder.BuildUp<TestObject>(new Locator(), null, null);
obj.RaiseCall();
Console.ReadLine();
}
static void CallHandler(object sender, EventArgs args)
{
Console.WriteLine("Called");
}
}
public class TestObject
{
private EventHandlerList _events = new EventHandlerList();
private static object _onCall = new object();
public event EventHandler Call
{
add
{
_events.AddHandler(_onCall, value);
}
remove
{
_events.RemoveHandler(_onCall, value);
}
}
protected virtual void OnCall(EventArgs args)
{
EventHandler handler = (EventHandler)_events[_onCall];
if (handler != null)
handler(this, args);
}
public void RaiseCall()
{
OnCall(EventArgs.Empty);
}
}
}
|

|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace Orphean.WinFormHelper.Framework.Factorys
{
///<summary>
/// a interface to be implement by Object Factory,
/// DAL use object factory to speed object constructing.
///</summary>
public interface IObjectFactory
{
///<summary>
/// acquire a object.
///</summary>
///<param name="type">object Type</param>
///<returns>object</returns>
object AcquireObject(Type type);
///<summary>
/// release a object.
///</summary>
///<param name="obj">a object to releasing</param>
void ReleaseObject(object obj);
}
public sealed class PoolObjectFactory : IObjectFactory, IDisposable
{
class PoolData
{
public bool InUse = false;
public object obj;
}
private IList _storage;
private int _max = 100;
private bool _limit = false;
private IBuilderContext _context = null;
public PoolObjectFactory(IBuilderContext context,int max, bool limit, IList storage):this(context)
{
_max = max;
_limit = limit;
_storage = storage;
}
public PoolObjectFactory(IBuilderContext context)
{
_context = context;
}
private PoolData GetPoolData(object obj)
{
lock (_storage.SyncRoot)
{
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (p.obj == obj)
return p;
}
}
return null;
}
private object GetObject(Type type)
{
lock (_storage.SyncRoot)
{
if (_storage.Count > 0)
{
if (((PoolData)_storage[0]).obj.GetType() != type)
throw new Exception(
string.Format("the Pool Factory only for Type :{0}",
_storage[0].GetType().Name));
}
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (!p.InUse)
{
p.InUse = true;
return p.obj;
}
}
if (_storage.Count > _max && _limit)
throw new Exception("max limit is arrived.");
object obj = _context.HeadOfChain.BuildUp(_context, type, null, null);
PoolData p1 = new PoolData();
p1.InUse = true;
p1.obj = obj;
_storage.Add(p1);
return obj;
}
}
private void PutObject(object obj)
{
PoolData p = GetPoolData(obj);
if (p != null)
p.InUse = false;
}
#region IObjectFactory Members
public object AcquireObject(Type type)
{
return GetObject(type);
}
public void ReleaseObject(object obj)
{
if (_storage.Count > _max)
{
if (obj is IDisposable)
((IDisposable)obj).Dispose();
PoolData p = GetPoolData(obj);
lock (_storage.SyncRoot)
_storage.Remove(p);
return;
}
PutObject(obj);
}
#endregion
#region IDisposable Members
public void Dispose()
{
lock (_storage.SyncRoot)
{
for (int i = 0; i < _storage.Count; i++)
{
PoolData p = (PoolData)_storage[i];
if (p.obj is IDisposable)
((IDisposable)p.obj).Dispose();
}
}
}
#endregion
}
}
|
|
public interface IPoolPolicy : IBuilderPolicy
{
bool IsPool { get;}
}
|
|
public class PoolPolicy : IPoolPolicy
{
private bool _isPool = false;
#region IPoolPolicy Members
public bool IsPool
{
get
{
return _isPool;
}
}
#endregion
public PoolPolicy(bool isPool)
{
_isPool = isPool;
}
}
|
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Orphean.WinFormHelper.Framework.Factorys;
namespace OB_PoolStrategy
{
public class PoolStrategy:BuilderStrategy
{
private WeakRefDictionary<object, object> _factoryMap =
new WeakRefDictionary<object, object>();
private bool _poolObjectCreating = false;
public override object BuildUp(IBuilderContext context, Type typeToBuild,
object existing, string idToBuild)
{
if (!_poolObjectCreating)
{
IPoolPolicy policy = context.Policies.Get<IPoolPolicy>(typeToBuild, idToBuild);
if (policy != null && policy.IsPool)
{
PoolLocatorKey key = new PoolLocatorKey(typeToBuild, idToBuild);
PoolObjectFactory factory = null;
if (context.Locator.Contains(key))
{
factory = context.Locator.Get<PoolObjectFactory>(key);
lock (this)
{
_poolObjectCreating = true;
try
{
existing = factory.AcquireObject(typeToBuild);
}
finally
{
_poolObjectCreating = false;
}
}
}
else
{
factory = new PoolObjectFactory(context, 15, false, new ArrayList());
_poolObjectCreating = true;
try
{
existing = factory.AcquireObject(typeToBuild);
}
finally
{
_poolObjectCreating = false;
}
context.Locator.Add(key, factory);
}
if (!_factoryMap.ContainsKey(existing))
_factoryMap.Add(existing, factory);
}
}
return base.BuildUp(context,typeToBuild,existing,idToBuild);
}
public override object TearDown(IBuilderContext context, object item)
{
if(_factoryMap.ContainsKey(item))
{
PoolObjectFactory factory = _factoryMap[item] as PoolObjectFactory;
if(factory != null)
factory.ReleaseObject(item);
_factoryMap.Remove(item);
}
return base.TearDown(context,item);
}
}
public sealed class PoolLocatorKey
{
private Type type;
private string id;
public PoolLocatorKey()
: this(null, null)
{
}
public PoolLocatorKey(Type type, string id)
{
this.type = type;
this.id = id;
}
public string ID
{
get { return id; }
}
public Type Type
{
get { return type; }
}
public override bool Equals(object obj)
{
PoolLocatorKey other = obj as PoolLocatorKey;
if (other == null)
return false;
return (Equals(type, other.type) && Equals(id, other.id));
}
public override int GetHashCode()
{
int hashForType = type == null ? 0 : type.GetHashCode();
int hashForID = id == null ? 0 : id.GetHashCode();
return hashForType ^ hashForID;
}
}
}
|
|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
namespace OB_PoolStrategy
{
class Program
{
static void Main(string[] args)
{
Builder builder = new Builder();
builder.Strategies.AddNew<PoolStrategy>(BuilderStage.PreCreation);
IPoolPolicy policy = new PoolPolicy(true);
builder.Policies.Set<IPoolPolicy>(policy, typeof(TestObject), null);
Locator locator = new Locator();
TestObject obj1 = builder.BuildUp<TestObject>(locator, null, null);
TestObject obj2 = builder.BuildUp<TestObject>(locator, null, null);
builder.TearDown<TestObject>(locator, obj1);
builder.TearDown<TestObject>(locator, obj2);
TestObject obj3 = builder.BuildUp<TestObject>(locator, null, null);
if (obj3 == obj1 || obj3 == obj2)
Console.WriteLine("Pooled");
Console.ReadLine();
}
}
public class TestObject
{
}
}
|

366



被折叠的 条评论
为什么被折叠?



