private void EnsureValidRoot(ConstructionNode node, ConstructionNode root) { if (!Equals(node, root) && node.InstantiateAs != null) { throw new InvalidOperationException($"'InstantiateAs' has been set for the node {node}, but this feature is only valid for the root node."); } }
private ConstructionNode ProcessNode(XElement node, IPrefixAnnotator annotator) { var elementType = xmlTypeResolver.LocateType(node.Name); var directives = directiveExtractor.GetDirectives(node).ToList(); var type = GetFinalTypeAccordingToDirectives(elementType, directives); var rawAssigments = assignmentExtractor.GetAssignments(type, node, annotator).ToList(); var attributeBasedInstanceProperties = CombineDirectivesAndAssigments(type, directives, rawAssigments); var children = GetChildren(type, node, annotator).ToList(); var ctorArgs = GetCtorArgs(node, type); var constructionNode = new ConstructionNode(elementType) { Name = attributeBasedInstanceProperties.Name, Key = attributeBasedInstanceProperties.Key, PositionalParameters = ctorArgs, InstantiateAs = type == elementType ? null : type, }.WithAssignments(attributeBasedInstanceProperties.Assignments.ToList()) .WithChildren(children); AnnotatePrefixes(node, annotator, constructionNode); return(constructionNode); }
protected virtual void AssembleFromChildren(MemberAssignment a, ConstructionNode constructionNode, INodeToObjectBuilder nodeToObjectBuilder, BuilderContext context) { foreach (var node in a.Values) { Assemble(node, nodeToObjectBuilder, constructionNode, context); } }
private static bool CanBeCreated(ConstructionNode node) { var allAsignmentsCreated = node.Assignments.All(assignment => assignment.Values.All(c => c.IsCreated)); var allChildrenCreated = node.Children.All(c => c.IsCreated); return(allAsignmentsCreated && allChildrenCreated); }
protected void ApplyAssignments(ConstructionNode node, INodeToObjectBuilder builder, BuilderContext context) { foreach (var assignment in node.Assignments) { var nodeAssignment = new NodeAssignment(assignment, node.Instance); AssigmentApplier.ExecuteAssignment(nodeAssignment, builder, context); } }
private static void AttachChildren(ConstructionNode node) { var children = node.Children.Select(c => c.Instance).ToList(); foreach (var c in children) { Collection.UniversalAdd(node.Instance, c); } }
public static ConstructionNode WithAssignments(this ConstructionNode node, IEnumerable <MemberAssignment> assignment) { foreach (var ass in assignment) { node.Assignments.Add(ass); } return(node); }
protected void CreateInstance(ConstructionNode node) { var positionalParameters = from n in node.PositionalParameters select new PositionalParameter(n); var creationHints = new CreationHints(new List <InjectableMember>(), positionalParameters, new List <object>()); var instance = InstanceCreator.Create(node.ActualInstanceType, creationHints).Instance; node.Instance = instance; node.IsCreated = true; }
public object Inflate(ConstructionNode node, BuildContext buildContext, object instance = null) { if (buildContext.Root == null) { buildContext.Root = node; buildContext.PrefixedTypeResolver.Root = node; } return(InflateCore(node, buildContext, instance)); }
private void AssembleAssignment(MemberAssignment memberAssignment, ConstructionNode node, INodeToObjectBuilder nodeToObjectBuilder, BuilderContext context) { if (memberAssignment.SourceValue != null) { AssembleFromSourceValue(memberAssignment, node, context); } else { AssembleFromChildren(memberAssignment, node, nodeToObjectBuilder, context); } }
public Type GetTypeByPrefix(ConstructionNode node, string prefixedType) { var p = prefixedType.Dicotomize(':'); var prefix = p.Item2 == null ? "": p.Item1; var typeName = p.Item2 ?? p.Item1; var availablePrefixes = GetAvailableFrom(Root, node); var prefixRegistration = availablePrefixes.First(registration => registration.Prefix == prefix); return(typeDirectory.GetTypeByFullAddress(new Address(prefixRegistration.NamespaceName, typeName))); }
private void AssembleLeafNode(ConstructionNode node, BuilderContext context) { var tryConvert = Converter.Convert(node.SourceValue, node.ActualInstanceType, new ConvertContext() { Node = node }); node.Instance = tryConvert.Item2; node.IsCreated = tryConvert.Item1; node.SourceValue = node.SourceValue; node.InstanceType = node.ActualInstanceType; }
private void SetMember(Assignment assignment, ConstructionNode parentNode, INodeToObjectBuilder builder, BuilderContext context) { var mutableUnit = new MutablePipelineUnit(parentNode, assignment.Value); pipeline.Handle(assignment.Target.Instance, assignment.Member, mutableUnit, builder, context); if (mutableUnit.Handled) { return; } assignment.Member.SetValue(assignment.Target.Instance, mutableUnit.Value); }
private void EnsureValidNode(ConstructionNode node) { if (node.InstantiateAs != null) { Type instanceType = node.InstanceType; Type instantiateAsType = node.InstantiateAs; if (!instanceType.GetTypeInfo().IsAssignableFrom(instantiateAsType.GetTypeInfo())) { throw new InvalidOperationException($"A node of type {instanceType} cannot be instantiated as {instantiateAsType}. The node of type {instanceType} has been tried to inflate as {instantiateAsType}, but this is not possible since the {instantiateAsType} is not derived from {instanceType}"); } } }
protected override object CreateChildProperty(object parent, Member member, ConstructionNode nodeToBeCreated, BuildContext buildContext) { var metadata = ObjectBuilderContext.MetadataProvider.Get(parent.GetType()); var fragmentLoaderInfo = metadata.FragmentLoaderInfo; if (fragmentLoaderInfo != null && parent.GetType() == fragmentLoaderInfo.Type && member.MemberName == fragmentLoaderInfo.PropertyName) { return(fragmentLoaderInfo.Loader.Load(nodeToBeCreated, this, buildContext)); } else { return(base.CreateChildProperty(parent, member, nodeToBeCreated, buildContext)); } }
private void AssembleFromSourceValue(MemberAssignment a, ConstructionNode node, BuilderContext context) { var conversionResult = Converter.Convert(a.SourceValue, a.Member.MemberType, new ConvertContext() { Node = node, BuilderContext = context }); a.Values = new List <ConstructionNode> { new ConstructionNode(a.Member.MemberType) { Instance = conversionResult.Item2, IsCreated = conversionResult.Item1, Parent = node, } }; }
private object CreateInstance(ConstructionNode node, BuildContext buildContext) { EnsureValidNode(node); var instanceType = node.InstantiateAs ?? node.InstanceType; var instance = creator.Create(instanceType, buildContext, node.InjectableArguments.Select(s => new InjectableMember(s))); NotifyNewInstance(buildContext, instance); if (node.Name != null) { buildContext.NamescopeAnnotator.RegisterName(node.Name, instance); } return(instance); }
public void Assemble(ConstructionNode node, INodeToObjectBuilder nodeToObjectBuilder, ConstructionNode parent = null, BuilderContext context = null) { node.Parent = parent; if (node.IsCreated) { return; } if (node.SourceValue != null) { AssembleLeafNode(node, context); } else { CreateIntermediateNode(node, nodeToObjectBuilder, context); } }
private void CreateIntermediateNode(ConstructionNode node, INodeToObjectBuilder nodeToObjectBuilder, BuilderContext context) { foreach (var a in node.Assignments) { AssembleAssignment(a, node, nodeToObjectBuilder, context); } foreach (var c in node.Children) { Assemble(c, nodeToObjectBuilder, node, context); } if (CanBeCreated(node)) { CreateInstance(node); ApplyAssignments(node, nodeToObjectBuilder, context); AttachChildren(node); } }
private IEnumerable <PrefixDeclaration> GetAvailableFrom(ConstructionNode root, ConstructionNode lastToCheck) { var myPrefixes = annotator.GetPrefixesFor(root); IEnumerable <PrefixDeclaration> prefixesFromChildren; if (root != lastToCheck) { prefixesFromChildren = root.Assignments .SelectMany(assignment => assignment.Children) .Select(constructionNode => GetAvailableFrom(constructionNode, lastToCheck)) .SelectMany(enumerable => enumerable); } else { prefixesFromChildren = new PrefixDeclaration[0]; } return(myPrefixes.Concat(prefixesFromChildren).ToList()); }
private object InflateCore(ConstructionNode node, BuildContext buildContext, object instance = null) { EnsureValidRoot(node, buildContext.Root); buildContext.CurrentNode = node; if (instance == null) { instance = CreateInstance(node, buildContext); buildContext.InstanceLifecycleSignaler.OnBegin(instance); } else { NotifyNewInstance(buildContext, instance); } ApplyAssignments(instance, node.Assignments, buildContext); InflateChildren(node.Children, instance, buildContext); buildContext.InstanceLifecycleSignaler.OnEnd(instance); return(instance); }
public static ConstructionNode WithAssignment <TParent, TSub>(this ConstructionNode <TParent, TSub> node, Expression <Func <TSub, object> > selector, string value) where TSub : TParent { node.Assignments.Add(new MemberAssignment <TSub>(selector, value)); return(node); }
public static ConstructionNode WithAssignment <T>(this ConstructionNode <T> node, Expression <Func <T, object> > selector, string value) { node.Assignments.Add(new MemberAssignment <T>(selector, value)); return(node); }
public IEnumerable <PrefixDeclaration> GetPrefixesFor(ConstructionNode node) { IEnumerable <PrefixDeclaration> prefixes; return(mappedPrefixes.TryGetValue(node, out prefixes) ? prefixes : new List <PrefixDeclaration>()); }
public static ConstructionNode WithAssignment <TParent, TSub>(this ConstructionNode <TParent, TSub> node, string memberName, string value) where TSub : TParent { node.Assignments.Add(new MemberAssignment <TSub>(memberName, value)); return(node); }
protected virtual object CreateChildProperty(object parent, Member member, ConstructionNode nodeToBeCreated, BuildContext buildContext) { return(InflateCore(nodeToBeCreated, buildContext)); }
public static IEnumerable <ConstructionNode> GetAllChildren(this ConstructionNode node) { return(node.Children.Concat(node.Assignments.SelectMany(assignment => assignment.Values))); }
public static ConstructionNode WithAssignments <T>(this ConstructionNode <T> node, params (Expression <Func <T, object> >, string)[] assignments)
public void Annotate(ConstructionNode node, IEnumerable <PrefixDeclaration> prefixes) { mappedPrefixes.Add(node, prefixes); }
public static ConstructionNode WithAssignment <T>(this ConstructionNode <T> node, string memberName, string value) { node.Assignments.Add(new MemberAssignment <T>(memberName, value)); return(node); }