Beispiel #1
0
 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.");
     }
 }
Beispiel #2
0
        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);
        }
Beispiel #3
0
 protected virtual void AssembleFromChildren(MemberAssignment a, ConstructionNode constructionNode, INodeToObjectBuilder nodeToObjectBuilder, BuilderContext context)
 {
     foreach (var node in a.Values)
     {
         Assemble(node, nodeToObjectBuilder, constructionNode, context);
     }
 }
Beispiel #4
0
        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);
        }
Beispiel #5
0
 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);
     }
 }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
        public static ConstructionNode WithAssignments(this ConstructionNode node, IEnumerable <MemberAssignment> assignment)
        {
            foreach (var ass in assignment)
            {
                node.Assignments.Add(ass);
            }

            return(node);
        }
Beispiel #8
0
        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;
        }
Beispiel #9
0
        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));
        }
Beispiel #10
0
 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);
     }
 }
Beispiel #11
0
        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)));
        }
Beispiel #12
0
        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;
        }
Beispiel #13
0
        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);
        }
Beispiel #14
0
 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));
            }
        }
Beispiel #16
0
        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,
                }
            };
        }
Beispiel #17
0
        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);
        }
Beispiel #18
0
        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);
            }
        }
Beispiel #19
0
        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);
            }
        }
Beispiel #20
0
        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());
        }
Beispiel #21
0
        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);
        }
Beispiel #22
0
        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);
        }
Beispiel #23
0
        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);
        }
Beispiel #24
0
        public IEnumerable <PrefixDeclaration> GetPrefixesFor(ConstructionNode node)
        {
            IEnumerable <PrefixDeclaration> prefixes;

            return(mappedPrefixes.TryGetValue(node, out prefixes) ? prefixes : new List <PrefixDeclaration>());
        }
Beispiel #25
0
        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);
        }
Beispiel #26
0
 protected virtual object CreateChildProperty(object parent, Member member, ConstructionNode nodeToBeCreated,
                                              BuildContext buildContext)
 {
     return(InflateCore(nodeToBeCreated, buildContext));
 }
Beispiel #27
0
 public static IEnumerable <ConstructionNode> GetAllChildren(this ConstructionNode node)
 {
     return(node.Children.Concat(node.Assignments.SelectMany(assignment => assignment.Values)));
 }
Beispiel #28
0
 public static ConstructionNode WithAssignments <T>(this ConstructionNode <T> node, params (Expression <Func <T, object> >, string)[] assignments)
Beispiel #29
0
 public void Annotate(ConstructionNode node, IEnumerable <PrefixDeclaration> prefixes)
 {
     mappedPrefixes.Add(node, prefixes);
 }
Beispiel #30
0
        public static ConstructionNode WithAssignment <T>(this ConstructionNode <T> node, string memberName, string value)
        {
            node.Assignments.Add(new MemberAssignment <T>(memberName, value));

            return(node);
        }