示例#1
0
        internal static EntityDefinition GetEntityDefinition(INamedTypeSymbol symbol)
        {
            if (!Cache.ContainsKey(symbol))
            {
                lock (SyncRoot)
                {
                    if (!Cache.ContainsKey(symbol))
                    {
                        Cache[symbol] = new EntityDefinition(symbol);
                    }
                }
            }

            return(Cache[symbol]);
        }
示例#2
0
        private ModelDefinition(INamedTypeSymbol symbol, AttributeData crmEntityAttribute) : base(symbol)
        {
            ParentModel = GetModelDefinition(symbol.BaseType);

            var entityDefinitionSymbol = (INamedTypeSymbol)crmEntityAttribute.ConstructorArguments.Single().Value;

            EntityDefinition = EntityDefinition.GetEntityDefinition(entityDefinitionSymbol);

            foreach (var memberName in symbol.MemberNames)
            {
                var members = symbol.GetMembers(memberName);

                if (!members.OfType <IPropertySymbol>().Any())
                {
                    continue;
                }

                var property = members.OfType <IPropertySymbol>().First();

                if (ModelAttributeDefinition.TryGetDefinition(this, property, out var modelAttributeDefinition))
                {
                    _attributes.Add(modelAttributeDefinition);
                }
            }

            foreach (var interfaceSymbol in symbol.Interfaces)
            {
                foreach (var memberName in interfaceSymbol.MemberNames)
                {
                    var members = interfaceSymbol.GetMembers(memberName);

                    if (!members.OfType <IPropertySymbol>().Any())
                    {
                        continue;
                    }

                    var property = members.OfType <IPropertySymbol>().First();

                    if (ModelAttributeDefinition.TryGetDefinition(this, property, out var modelAttributeDefinition))
                    {
                        _attributes.Add(modelAttributeDefinition);
                    }
                }
            }
        }
        private ModelAttributeDefinition(ModelDefinition containingModel, IPropertySymbol property, ImmutableArray <AttributeData> attributes) : base(property)
        {
            ContainingModel = containingModel;

            var crmMappingAttribute = attributes.FirstOrDefault(a => a.AttributeClass.Name == "CrmMappingAttribute");

            if (crmMappingAttribute != null)
            {
                var targetAttributeName = (string)crmMappingAttribute.ConstructorArguments.First().Value;
                AttributeDefinition = ContainingModel.EntityDefinition[targetAttributeName];

                if (crmMappingAttribute.NamedArguments.Any(kvp => kvp.Key == "IsValidForUpdate"))
                {
                    IsValidForUpdate = (bool)crmMappingAttribute.NamedArguments.First(kvp => kvp.Key == "IsValidForUpdate").Value.Value;
                }
                if (crmMappingAttribute.NamedArguments.Any(kvp => kvp.Key == "FollowLink"))
                {
                    FollowLink = (bool)crmMappingAttribute.NamedArguments.First(kvp => kvp.Key == "FollowLink").Value.Value;
                }

                if (attributes.Any(a => a.AttributeClass.Name == "CrmLookupAttribute"))
                {
                    var lookupAttribute  = attributes.FirstOrDefault(a => a.AttributeClass.Name == "CrmLookupAttribute");
                    var targetDefinition = EntityDefinition.GetEntityDefinition((INamedTypeSymbol)lookupAttribute.ConstructorArguments.First().Value);
                    ExplicitLinkedAttributeDefinition = targetDefinition[(string)lookupAttribute.ConstructorArguments.Skip(1).First().Value];
                }

                if (attributes.Any(a => a.AttributeClass.Name == "TypeConverterAttribute" && a.AttributeClass.ContainingNamespace.ToDisplayString() == "System.ComponentModel"))
                {
                    var typeConverterAttribute = attributes.FirstOrDefault(a => a.AttributeClass.Name == "TypeConverterAttribute" && a.AttributeClass.ContainingNamespace.ToDisplayString() == "System.ComponentModel");
                    if (!(typeConverterAttribute.AttributeClass is IErrorTypeSymbol))
                    {
                        TypeConverter = (INamedTypeSymbol)typeConverterAttribute.ConstructorArguments.First().Value;
                    }
                }
            }

            IsModelExtension = attributes.Any(a => a.AttributeClass.Name == "ExtendBindingModelAttribute");
        }
        internal AttributeDefinition(EntityDefinition containingDefinition, IFieldSymbol attributeSymbol) : base(attributeSymbol)
        {
            ContainingDefinition = containingDefinition;
            LogicalName          = (string)attributeSymbol.ConstantValue;

            var attributes = attributeSymbol.GetAttributes();

            var metadata = attributes.Single(a => a.AttributeClass.Name == "AttributeMetadataAttribute").ConstructorArguments.Single();

            Type = (AttributeTypeCode)Enum.ToObject(typeof(AttributeTypeCode), metadata.Value);

            if (attributes.Any(a => a.AttributeClass.Name == "PrimaryAttributeAttribute"))
            {
                metadata     = attributes.FirstOrDefault(a => a.AttributeClass.Name == "PrimaryAttributeAttribute").ConstructorArguments.Single();
                _primaryType = metadata.IsNull ? null : (PrimaryAttributeType?)Enum.ToObject(typeof(PrimaryAttributeType), metadata.Value);
            }

            foreach (var relationMetadata in attributes.Where(a => a.AttributeClass.Name == "CrmLookupAttribute"))
            {
                if (relationMetadata.ConstructorArguments.First().Kind == TypedConstantKind.Primitive)
                {
                }
                else
                {
                    var targetSymbol = (INamedTypeSymbol)relationMetadata.ConstructorArguments.First().Value;

                    var relationship = new RelationshipMetadata
                    {
                        TargetNamedTypeSymbol = targetSymbol,
                        TargetAttributeName   = (string)relationMetadata.ConstructorArguments[1].Value,
                        SchemaName            = (string)relationMetadata.NamedArguments[0].Value.Value
                    };
                    Relationships.Add(relationship);
                }
            }
        }