public void BuildUp(IBuilderContext context) { Guard.ArgumentNotNull(context, "context"); if (context.Existing == null) { var currentContainer = context.Container ?? 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); } }
private static void BuildResolveEnumerable <T>(IBuilderContext context) { if (null == context.Existing) { var key = context.BuildKey; var type = key.Type; var itemType = type.GetTypeInfo().GenericTypeArguments[0]; var itemTypeInfo = itemType.GetTypeInfo(); var container = context.Container ?? context.NewBuildUp <IUnityContainer>(); if (itemTypeInfo.IsGenericTypeDefinition) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.MustHaveOpenGenericType, itemType.GetTypeInfo().Name)); } var generic = new Lazy <Type>(() => itemType.GetGenericTypeDefinition()); IEnumerable <object> enumerable = container.Registrations .Where(r => r.RegisteredType == itemType || (itemTypeInfo.IsGenericType && r.RegisteredType.GetTypeInfo().IsGenericTypeDefinition&& r.RegisteredType == generic.Value)) .Select(r => context.NewBuildUp(new NamedTypeBuildKey(itemType, r.Name))); context.Existing = CastMethod.MakeGenericMethod(itemType).Invoke(null, new object[] { enumerable }); context.BuildComplete = true; } // match the behavior of DynamicMethodConstructorStrategy DynamicMethodConstructorStrategy.SetPerBuildSingleton(context); }
private static void BuildResolveAllLazy <T>(IBuilderContext context) { if (context.Existing == null) { var container = context.Container ?? context.NewBuildUp <IUnityContainer>(); context.Existing = new Lazy <IEnumerable <T> >(() => container.ResolveAll <T>()); } // match the behavior of DynamicMethodConstructorStrategy 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) { Unity.Utility.Guard.ArgumentNotNull(context, "context"); if (context.Existing == null) { var currentContainer = context.Container ?? context.NewBuildUp <IUnityContainer>(); context.Existing = factory(currentContainer, context.BuildKey.Type, context.BuildKey.Name); DynamicMethodConstructorStrategy.SetPerBuildSingleton(context); } }
private static void BuildResolveLazy <T>(IBuilderContext context) { if (context.Existing == null) { var name = context.BuildKey.Name; var container = context.NewBuildUp <IUnityContainer>(); context.Existing = new Lazy <T>(() => container.Resolve <T>(name)); } // match the behavior of DynamicMethodConstructorStrategy DynamicMethodConstructorStrategy.SetPerBuildSingleton(context); }
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))); }