Esempio n. 1
0
        private IStructureDefinition GetStructureDefinition(IMessage theMessage)
        {
            var messageType = theMessage.GetType();
            StructureDefinition retVal;

            if (structureDefinitions.TryGetValue(messageType, out var definitions))
            {
                if (definitions.TryGetValue(theMessage.GetStructureName(), out retVal))
                {
                    return(retVal);
                }
            }

            Log.Info($"Instantiating msg of class {messageType.FullName}");
            var constructor = messageType.GetConstructor(new[] { typeof(IModelClassFactory) });
            var message     = (IMessage)constructor.Invoke(new object[] { Factory });

            StructureDefinition previousLeaf = null;

            retVal = CreateStructureDefinition(message, ref previousLeaf);

            if (structureDefinitions.ContainsKey(messageType))
            {
                return(retVal);
            }

            var dictionary = new Dictionary <string, StructureDefinition>
            {
                { theMessage.GetStructureName(), retVal },
            };

            structureDefinitions.Add(messageType, dictionary);

            return(retVal);
        }
Esempio n. 2
0
        private IStructureDefinition GetStructureDefinition(IMessage theMessage)
        {
            var messageType = theMessage.GetType();
            StructureDefinition retVal;

            if (structureDefinitions.TryGetValue(messageType, out var definitions))
            {
                if (definitions.TryGetValue(theMessage.GetStructureName(), out retVal))
                {
                    return(retVal);
                }
            }

            // Double-Checked Locking
            lock (structureDefinitionLock)
            {
                if (structureDefinitions.TryGetValue(messageType, out var definitions2))
                {
                    if (definitions2.TryGetValue(theMessage.GetStructureName(), out retVal))
                    {
                        return(retVal);
                    }
                }

                Log.Info($"Instantiating msg of class {messageType.FullName}");
                var constructor = messageType.GetConstructor(new[] { typeof(IModelClassFactory) });
                var message     = (IMessage)constructor.Invoke(new object[] { Factory });

                StructureDefinition previousLeaf = null;
                retVal = CreateStructureDefinition(message, ref previousLeaf);

                if (structureDefinitions.ContainsKey(messageType))
                {
                    return(retVal);
                }
#if NET35
                var dictionary = new SynchronizedCache <string, StructureDefinition>
                {
                    { theMessage.GetStructureName(), retVal },
                };
#else
                var dictionary = new ConcurrentDictionary <string, StructureDefinition>
                {
                    [theMessage.GetStructureName()] = retVal,
                };
#endif

                structureDefinitions.TryAdd(messageType, dictionary);
            }

            return(retVal);
        }
Esempio n. 3
0
        private StructureDefinition CreateStructureDefinition(IStructure theStructure, ref StructureDefinition thePreviousLeaf)
        {
            var result = new StructureDefinition
            {
                Name = theStructure.GetStructureName(),
            };

            if (theStructure is IGroup)
            {
                result.IsSegment = false;
                var group      = (IGroup)theStructure;
                var index      = 0;
                var childNames = group.Names.ToList();

                foreach (var nextName in childNames)
                {
                    var nextChild           = group.GetStructure(nextName);
                    var structureDefinition = CreateStructureDefinition(nextChild, ref thePreviousLeaf);

                    structureDefinition.NameAsItAppearsInParent = nextName;
                    structureDefinition.IsRepeating             = group.IsRepeating(nextName);
                    structureDefinition.IsRequired      = group.IsRequired(nextName);
                    structureDefinition.IsChoiceElement = group.IsChoiceElement(nextName);
                    structureDefinition.Position        = index++;
                    structureDefinition.Parent          = result;

                    result.Children.Add(structureDefinition);
                }
            }
            else
            {
                if (thePreviousLeaf != null)
                {
                    thePreviousLeaf.NextLeaf = result;
                }

                thePreviousLeaf  = result;
                result.IsSegment = true;
            }

            return(result);
        }
Esempio n. 4
0
 protected bool Equals(StructureDefinition other)
 {
     return(Name == other?.Name && Position == other?.Position);
 }