Пример #1
0
        /// <summary>
        /// Creates a <see cref="SaveResult"/>.
        /// </summary>
        /// <returns>The save result.</returns>
        public SaveResult ToSaveResult()
        {
            var entities    = new List <object>();
            var deletedKeys = new List <EntityKey>();

            foreach (var entityInfo in SaveMap.SelectMany(o => o.Value))
            {
                var state = entityInfo.EntityState;
                if (state != EntityState.Detached)
                {
                    entities.Add(entityInfo.ClientEntity);
                }

                if (state == EntityState.Deleted || state == EntityState.Detached)
                {
                    deletedKeys.Add(new EntityKey(
                                        BreezeHelper.GetBreezeTypeFullName(entityInfo.EntityType),
                                        entityInfo.GetIdentifierValues()));
                }
            }

            return(new SaveResult
            {
                Entities = entities,
                KeyMappings = KeyMappings,
                DeletedKeys = deletedKeys
            });
        }
Пример #2
0
        private static ISessionImplementor GetSessionFromCollection(IEnumerable collection,
                                                                    int depth,
                                                                    Func <object, int, bool> predicate,
                                                                    Func <Type, GetSessionDelegate> getTypeSessionGetter,
                                                                    Predicate <Type> isEntityPredicate)
        {
            return(GetSession(collection, out var isProxy) ?? (isProxy ? null : GetSessionFromCollection()));

            ISessionImplementor GetSessionFromCollection()
            {
                if (!predicate(collection, depth) || collection == null)
                {
                    return(null);
                }

                depth++;
                // Non generic collection
                foreach (var item in collection)
                {
                    var type = BreezeHelper.GetEntityType(item, false);
                    if (isEntityPredicate(type))
                    {
                        continue;
                    }

                    var session = GetSessionFromEntity(item, depth, predicate, getTypeSessionGetter);
                    if (session != null)
                    {
                        return(session);
                    }
                }

                return(null);
            }
        }
Пример #3
0
        private EntityMetadata GetMetadata(string entityTypeName)
        {
            var entityName = BreezeHelper.GetEntityName(entityTypeName);
            var metadata   = _classMetadataProvider.Get(entityName);

            if (metadata == null)
            {
                throw new InvalidOperationException($"Unknown entity name: {entityName}");
            }

            return(_entityMetadataProvider.GetMetadata(metadata.MappedClass));
        }
Пример #4
0
        private void InitializeObject(object item, ExpandNode expandNode)
        {
            if (item is INHibernateProxy nhProxy)
            {
                nhProxy.HibernateLazyInitializer.Initialize();
                item = nhProxy.HibernateLazyInitializer.GetImplementation();
            }

            var metadata = _typeExpandMetadata.GetOrAdd(BreezeHelper.GetEntityType(item), CreateExpandMetadata);

            foreach (var expand in expandNode.Expands.Values)
            {
                Initialize(metadata.GetPropertyValue(expand.PropertyName, item), expand);
            }
        }
Пример #5
0
        /// <summary>
        /// Gets the client entity identifier values.
        /// </summary>
        /// <returns>An array with identifier values.</returns>
        public object[] GetIdentifierValues()
        {
            var propertyNames = EntityMetadata.IdentifierPropertyNames;
            var values        = new object[propertyNames.Count];

            for (var i = 0; i < propertyNames.Count; i++)
            {
                var propertyName = propertyNames[i];
                if (EntityMetadata.SyntheticForeignKeyProperties.TryGetValue(propertyName, out var syntheticProperty))
                {
                    values[i] = BreezeHelper.ConvertToType(UnmappedValuesMap[propertyName], syntheticProperty.IdentifierType);
                }
                else
                {
                    values[i] = EntityMetadata.IdentifierPropertyGetters[i](ClientEntity);
                }
            }

            return(values);
        }
Пример #6
0
        private static ISessionImplementor GetSessionFromEntity(
            object value,
            int depth,
            Func <object, int, bool> predicate,
            Func <Type, GetSessionDelegate> getTypeSessionGetter)
        {
            return(GetSession(value, out var isProxy) ?? (isProxy ? null : GetSessionFromEntityRecursive()));

            ISessionImplementor GetSessionFromEntityRecursive()
            {
                if (!predicate(value, depth) || value == null)
                {
                    return(null);
                }

                var sessionGetter = getTypeSessionGetter(BreezeHelper.GetEntityType(value, false));

                return(sessionGetter.Invoke(value, depth + 1, predicate));
            }
        }
Пример #7
0
        private ClientModelMetadata Create(Type clientType)
        {
            if (!IsClientModel(clientType))
            {
                return(null);
            }

            var properties          = clientType.GetProperties().ToDictionary(o => o.Name);
            var syntheticProperties = new List <SyntheticForeignKeyProperty>();
            var associations        = new Dictionary <string, Association>();
            var clientProperties    = new List <ClientModelProperty>(properties.Count);

            foreach (var property in clientType.GetProperties())
            {
                var propertyType = property.PropertyType;
                if (property.GetCustomAttribute <ComplexTypeAttribute>() != null)
                {
                    clientProperties.Add(new ClientModelProperty(
                                             property.Name,
                                             propertyType,
                                             true,
                                             null,
                                             true,
                                             false,
                                             false,
                                             false));
                    continue;
                }


                var isCollection          = propertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(propertyType);
                var elementType           = isCollection ? propertyType.GetGenericArguments()[0] : propertyType;
                var relatedEntityMetadata = _entityMetadataProvider.IsEntityType(elementType)
                    ? _entityMetadataProvider.GetMetadata(elementType)
                    : null;

                if (relatedEntityMetadata != null || IsClientModel(elementType))
                {
                    clientProperties.Add(
                        isCollection
                        // Add a one to many relation
                            ? CreateCollectionNavigationProperty(
                            clientType,
                            property,
                            elementType,
                            relatedEntityMetadata,
                            associations)
                        // Add a many to one relation
                            : CreateEntityNavigationProperty(
                            clientType,
                            property,
                            properties,
                            relatedEntityMetadata,
                            syntheticProperties,
                            associations)
                        );

                    continue;
                }

                clientProperties.Add(new ClientModelProperty(
                                         property.Name,
                                         propertyType,
                                         false,
                                         _dataTypeProvider.GetDataType(propertyType),
                                         BreezeHelper.IsNullable(propertyType),
                                         property.Name == nameof(IClientModel.Id),
                                         false,
                                         false));
            }

            return(new ClientModelMetadata(
                       clientType,
                       null, // TODO: base type
                       AutoGeneratedKeyType.KeyGenerator,
                       clientProperties,
                       syntheticProperties.ToDictionary(o => o.Name),
                       new List <Type>(),   // TODO: base type
                       new List <string>(), // TODO: base type
                       IdentifierPropertyNames,
                       IdentifierPropertyGetters,
                       IdentifierPropertyTypes,
                       IdentifierPropertyDataTypes,
                       IdentifierPropertyTypeLengths,
                       new HashSet <string>(associations.Values.Where(o => o.IsScalar).SelectMany(o => o.ForeignKeyPropertyNames)),
                       associations
                       ));
        }