Example #1
0
        private GraphNode CreateNewMember(PropertyInfo accessor)
        {
            GraphNode newMember;

            switch (_currentMethod)
            {
            case "OwnedEntity":
                newMember = GraphNodeFactory.Create(_currentMember, accessor, false, true);
                break;

            case "AssociatedEntity":
                newMember = GraphNodeFactory.Create(_currentMember, accessor, false, false);
                break;

            case "OwnedCollection":
                newMember = GraphNodeFactory.Create(_currentMember, accessor, true, true);
                break;

            case "AssociatedCollection":
                newMember = GraphNodeFactory.Create(_currentMember, accessor, true, false);
                break;

            default:
                throw new NotSupportedException("The method used in the update mapping is not supported");
            }
            return(newMember);
        }
        private static void BuildEntityGraph <TAggregate>(GraphNode parent, Type type, Dictionary <string, bool> visited)
        {
            var properties = type.GetProperties()
                             .Select(p => new
            {
                Accessor = p,
                IsOwned  = p.GetCustomAttributes(typeof(OwnedAttribute), true)
                           .Cast <OwnedAttribute>()
                           .Any(attr => attr.AggregateType == null || attr.AggregateType == typeof(TAggregate)),
                IsAssociated = p.GetCustomAttributes(typeof(AssociatedAttribute), true)
                               .Cast <AssociatedAttribute>()
                               .Any(attr => attr.AggregateType == null || attr.AggregateType == typeof(TAggregate))
            })
                             .Where(p => p.IsOwned || p.IsAssociated);

            foreach (var property in properties)
            {
                var  propertyType = property.Accessor.PropertyType;
                bool isCollection = false;

                // if collection
                var genericType = propertyType
                                  .GetInterfaces()
                                  .Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                                  .Select(t => t.GetGenericArguments().First())
                                  .FirstOrDefault();

                if (genericType != null)
                {
                    isCollection = true;
                    propertyType = genericType;
                }
                else if (propertyType.IsArray)
                {
                    isCollection = true;
                    propertyType = type.GetElementType();
                }

                var node = GraphNodeFactory.Create(parent, property.Accessor, isCollection, property.IsOwned);
                parent.Members.Push(node);

                if (property.IsOwned)
                {
                    if (IsVisited(visited, node))
                    {
                        throw new NotSupportedException("Attribute mapping does not support circular graphs");
                    }
                    SetVisited(visited, node);
                    BuildEntityGraph <TAggregate>(node, propertyType, visited);
                }
            }
        }