private void SetSingleton(IBuilderContext context, Type t, object value) { SingletonLifetimePolicy policy = new SingletonLifetimePolicy(); policy.SetValue(value); context.PersistentPolicies.Set <ILifetimePolicy>(policy, t); }
public void TheSameInstanceIsReturnedEachTime() { var container = new SimpleContainer(); var policy = new SingletonLifetimePolicy(typeof(ExampleClass), container); var instance1 = policy.GetInstance(); var instance2 = policy.GetInstance(); Assert.AreEqual(instance1, instance2); Assert.IsTrue(instance1 is ExampleClass); Assert.IsTrue(instance2 is ExampleClass); }
private void RegisterType(bool singleton, Type type) { ILifetimePolicy policy = null; if (singleton) { policy = new SingletonLifetimePolicy(type, this); } else { policy = new TransientLifetimePolicy(type, this); } _resolvers[type] = new ConcreteTypeResolver(policy); }
public void CanResolveSimpleParameterTypes() { TestingBuilderContext context = GetContext(); SingletonLifetimePolicy lifetimePolicy = new SingletonLifetimePolicy(); lifetimePolicy.SetValue("C:\\Log.txt"); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, typeof(string)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(FileLogger)); context.BuildKey = typeof(FileLogger); plan.BuildUp(context); object result = context.Existing; FileLogger logger = result as FileLogger; Assert.IsNotNull(result); Assert.IsNotNull(logger); Assert.AreEqual("C:\\Log.txt", logger.LogFile); }
public void CanInjectProperties() { TestingBuilderContext context = GetContext(); object existingObject = new object(); SingletonLifetimePolicy lifetimePolicy = new SingletonLifetimePolicy(); lifetimePolicy.SetValue(existingObject); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, typeof(object)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(OnePropertyClass)); OnePropertyClass existing = new OnePropertyClass(); context.Existing = existing; context.BuildKey = typeof(OnePropertyClass); plan.BuildUp(context); Assert.IsNotNull(existing.Key); Assert.AreSame(existingObject, existing.Key); }
public void GetProperExceptionMessageWhenFailingToResolveMethodParameter() { TestingBuilderContext context = TestingBuilderContext.GetFullContext(); ILifetimePolicy intLifetime = new SingletonLifetimePolicy(); intLifetime.SetValue(42); context.PersistentPolicies.Set(intLifetime, typeof(int)); ObjectWithInjectionMethod existing = new ObjectWithInjectionMethod(); try { context.ExecuteBuildUp(typeof(ObjectWithInjectionMethod), existing); Assert.Fail(); } catch (BuildFailedException ex) { StringAssert.Contains(ex.Message, "DoSomething"); StringAssert.Contains(ex.Message, "stringParam"); } }