Exemplo n.º 1
0
        public void BuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");

            if (context.Existing == null)
            {
                var currentContainer = context.NewBuildUp <IUnityContainer>();

                Type   typeToBuild = GetTypeToBuild(context.BuildKey.Type);
                string nameToBuild = context.BuildKey.Name;

                Delegate resolveMethod;

                if (IsResolvingIEnumerable(typeToBuild))
                {
                    resolveMethod = CreateResolveAllResolver(currentContainer, typeToBuild);
                }
                else
                {
                    resolveMethod = CreateResolver(currentContainer, typeToBuild, nameToBuild);
                }

                context.Existing = resolveMethod;

                DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
            }
        }
        /// <summary>
        /// Creates an instance of this build plan's type, or fills
        /// in the existing type if passed in.
        /// </summary>
        /// <param name="context">Context used to build up the object.</param>
        public void BuildUp(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                var currentContainer = context.NewBuildUp <IUnityContainer>();
                context.Existing = factory(currentContainer, context.BuildKey.Type, context.BuildKey.Name);

                DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
            }
        }
        private static void BuildResolveAllLazy <T>(IBuilderContext context)
        {
            if (context.Existing == null)
            {
                var container = context.NewBuildUp <IUnityContainer>();
                context.Existing = new Lazy <IEnumerable <T> >(() => container.ResolveAll <T>());
            }

            // match the behavior of DynamicMethodConstructorStrategy
            DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
        }
Exemplo n.º 4
0
        public void BuildUp(IBuilderContext context)
        {
            Microsoft.Practices.Unity.Utility.Guard.ArgumentNotNull(context, "context");

            if (context.Existing == null)
            {
                var currentContainer = context.NewBuildUp <IUnityContainer>();
                context.Existing = factory(currentContainer, context.BuildKey.Type, context.BuildKey.Name);

                DynamicMethodConstructorStrategy.SetPerBuildSingleton(context);
            }
        }
Exemplo n.º 5
0
        internal Expression CreateInstanceBuildupExpression(DynamicBuildPlanGenerationContext buildContext, IBuilderContext context)
        {
            var targetTypeInfo = context.BuildKey.Type.GetTypeInfo();

            if (targetTypeInfo.IsInterface)
            {
                return(CreateThrowWithContext(buildContext, ThrowForAttemptingToConstructInterfaceMethod));
            }

            if (targetTypeInfo.IsAbstract)
            {
                return(CreateThrowWithContext(buildContext, ThrowForAttemptingToConstructAbstractClassMethod));
            }

            if (targetTypeInfo.IsSubclassOf(typeof(Delegate)))
            {
                return(CreateThrowWithContext(buildContext, ThrowForAttemptingToConstructDelegateMethod));
            }

            IPolicyList resolverPolicyDestination;
            IConstructorSelectorPolicy selector =
                context.Policies.Get <IConstructorSelectorPolicy>(context.BuildKey, out resolverPolicyDestination);

            SelectedConstructor selectedConstructor = selector.SelectConstructor(context, resolverPolicyDestination);

            if (selectedConstructor == null)
            {
                return(CreateThrowWithContext(buildContext, ThrowForNullExistingObjectMethod));
            }

            string signature = DynamicMethodConstructorStrategy.CreateSignatureString(selectedConstructor.Constructor);

            if (IsInvalidConstructor(selectedConstructor))
            {
                return(CreateThrowForNullExistingObjectWithInvalidConstructor(buildContext, signature));
            }

            // psuedo-code:
            // throw if attempting interface
            // if (context.Existing == null) {
            //   collect parameters
            //   set operation to invoking constructor
            //   context.Existing = new {objectType}({constructorparameter}...)
            //   clear current operation
            // }
            return(Expression.Block(this.CreateNewBuildupSequence(buildContext, selectedConstructor, signature)));
        }