Example #1
0
        protected ResourceMemberBase(ResourceClassBase resourceClass, string propertyName)
        {
            ResourceClass = resourceClass;
            PropertyName  = propertyName;

            _jsonPropertyName = new Lazy <string>(
                () =>
            {
                var jsonPropertyName =
                    ResourceClass.AllProperties.Any(x => x.PropertyName == PropertyName) ||
                    ResourceClass.MemberNamesInvolvedInJsonCollisions.Contains(PropertyName)
                            ? PropertyName.ToCamelCase()
                            : JsonNamingConvention.ProposeJsonPropertyName(ParentFullName.Name, PropertyName);

                return(UniqueIdSpecification.IsUSI(jsonPropertyName)
                        ? UniqueIdSpecification.GetUniqueIdPropertyName(jsonPropertyName)
                        : jsonPropertyName);
            });
        }
Example #2
0
        private void LazyInitializeDerivedCollections()
        {
            _allProperties = new Lazy <IReadOnlyList <ResourceProperty> >(
                () =>

                // Add locally defined identifying properties first
                Properties.Where(p => p.IsIdentifying)

                // Add reference properties, identifying references first, followed by required, and then optional
                .Concat(
                    References
                    .OrderByDescending(
                        r => (r.Association.IsIdentifying ? 100: 0)
                        + (r.IsRequired ? 10 : 0))
                    .SelectMany(r => r.Properties))

                // Add non-identifying properties
                .Concat(Properties.Where(p => !p.IsIdentifying))
                .Distinct(ModelComparers.ResourcePropertyNameOnly)
                .ToList());

            _allPropertyByName = new Lazy <IReadOnlyDictionary <string, ResourceProperty> >(
                () =>
                AllProperties.ToDictionary(x => x.PropertyName, x => x, StringComparer.InvariantCultureIgnoreCase));

            _propertyByName = new Lazy <IReadOnlyDictionary <string, ResourceProperty> >(
                () =>
                Properties
                .ToDictionary(x => x.PropertyName, x => x, StringComparer.InvariantCultureIgnoreCase));

            _embeddedObjectByName = new Lazy <IReadOnlyDictionary <string, EmbeddedObject> >(
                () =>
                EmbeddedObjects.ToDictionary(
                    x => x.PropertyName,
                    x => x,
                    StringComparer.InvariantCultureIgnoreCase));

            // Added lazy initialization of dictionary
            _extensionByName = new Lazy <IReadOnlyDictionary <string, Extension> >(
                () =>
                Extensions.ToDictionary(
                    x => x.PropertyName,
                    x => x,
                    StringComparer.InvariantCultureIgnoreCase));

            _collectionByName = new Lazy <IReadOnlyDictionary <string, Collection> >(
                () =>
                Collections.ToDictionary(x => x.PropertyName, x => x, StringComparer.InvariantCultureIgnoreCase));

            _referenceByName = new Lazy <IReadOnlyDictionary <string, Reference> >(
                () =>
                References.ToDictionary(x => x.PropertyName, x => x, StringComparer.InvariantCultureIgnoreCase));

            _allMembers = new Lazy <IReadOnlyList <ResourceMemberBase> >(
                () => LazyInitializeAllMembers()
                .ToList());

            _memberByName = new Lazy <IReadOnlyDictionary <string, ResourceMemberBase> >(
                () => AllMembers.ToDictionary(x => x.PropertyName, x => x, StringComparer.InvariantCultureIgnoreCase));

            _memberNamesInvolvedInJsonCollisions = new Lazy <IReadOnlyList <string> >(
                () =>
            {
                var allProposedNames =
                    AllMembers.Select(
                        p =>
                        Tuple.Create(
                            p.PropertyName,
                            JsonNamingConvention.ProposeJsonPropertyName(p.ParentFullName.Name, p.PropertyName)))
                    .ToList();

                var proposedJsonNames = new HashSet <string>();
                var rejectedJsonNames = new HashSet <string>();

                foreach (var nameAndProposedName in allProposedNames)
                {
                    if (!proposedJsonNames.Add(nameAndProposedName.Item2))
                    {
                        rejectedJsonNames.Add(nameAndProposedName.Item2);
                    }
                }

                return(allProposedNames
                       .Where(t => rejectedJsonNames.Contains(t.Item2))
                       .Select(t => t.Item1)
                       .ToList());
            }
                );
        }