private void SetSingleton(IBuilderContext context, Type t, object value)
        {
            SingletonLifetimePolicy policy = new SingletonLifetimePolicy();

            policy.SetValue(value);
            context.PersistentPolicies.Set <ILifetimePolicy>(policy, t);
        }
示例#2
0
        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");
            }
        }