Exemplo n.º 1
0
        static EntityType GetSmallEntityType(Type runtimeType)
        {
            if (runtimeType.TypeIsNotCorrect())
            {
                return(null);
            }

            return(new EntityType
            {
                Type = runtimeType,
                AssemblyFullName = runtimeType.AssemblyQualifiedName,
                ClassFullName = runtimeType.FullName,
                ClassName = runtimeType.Name,
                TableName = TableNameAttribute.GetTableName(runtimeType),
                Name = runtimeType.Name.ToPlural(),

                PluralName = runtimeType.Name.ToPlural(),
                IsTransient = Attribute.IsDefined(runtimeType, typeof(TransientEntityAttribute)),
                IsAbstract = runtimeType.GetTypeInfo().IsAbstract,
                SoftDelete = Attribute.IsDefined(runtimeType, typeof(SoftDeleteAttribute)),

                HasDataAccessClass = Attribute.IsDefined(runtimeType, typeof(DatabaseGeneratedAttribute)),
                //HasKnownDatabase = Attribute.IsDefined(runtimeType, typeof(HasKnownDatabaseAttribute)),

                BaseType = GetSmallEntityType(runtimeType.BaseType),
            });
        }
Exemplo n.º 2
0
        //通过反射获取表名
        public static string GetName(Type type)
        {
            if (type.IsDefined(typeof(TableNameAttribute), true))
            {
                TableNameAttribute attribute = (TableNameAttribute)type.GetCustomAttribute(typeof(TableNameAttribute), true);

                return(attribute.GetTableName());
            }
            else
            {
                return(type.Name);
            }
        }
        internal static DataProviderMetaData Generate(Type type)
        {
            var tableName = TableNameAttribute.GetTableName(type);

            return(new DataProviderMetaData(type)
            {
                BaseClassTypesInOrder = GetParents(type),
                DrivedClassTypes = GetDrivedClasses(type),
                Properties = GetProperties(type).ToArray(),
                Schema = SchemaAttribute.GetSchema(type),
                TableName = tableName,
                TableAlias = tableName,
                IsSoftDeleteEnabled = SoftDeleteAttribute.IsEnabled(type, inherit: false)
            });
        }
Exemplo n.º 4
0
        internal async Task Import(ReplicateDataMessage message)
        {
            if (message.CreationUtc < RefreshRequestUtc?.Subtract(TimeSyncTolerance))
            {
                // Ignore this. We will receive a full table after this anyway.
                Log.Info("Ignored importing expired ReplicateDataMessage " + message.DeduplicationId + " because it's older the last refresh request.");
                return;
            }

            if (message.IsClearSignal)
            {
                Log.Debug($"Received Clear Signal for {message.TypeFullName}");

                await Database.GetAccess(DomainType).ExecuteNonQuery($"truncate table {SchemaAttribute.GetSchema(DomainType).WithSuffix(".")}{TableNameAttribute.GetTableName(DomainType)}");

                return;
            }

            Log.Debug($"Beginning to import ReplicateDataMessage for {message.TypeFullName}:\n{message.Entity}\n\n");

            IEntity entity;

            try { entity = await Deserialize(message.Entity); }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to deserialize a message of :> " + message.TypeFullName + " message : " + message.Entity);
                throw;
            }

            try
            {
                if (message.ToDelete)
                {
                    if (await Endpoint.OnDeleting(message, entity))
                    {
                        await Database.Delete(entity);

                        await Endpoint.OnDeleted(message, entity);
                    }
                }
                else
                {
                    var mode = entity.IsNew ? SaveMode.Insert : SaveMode.Update;

                    if (mode == SaveMode.Update && !await IsActuallyChanged(entity))
                    {
                        return;
                    }

                    if (!await Endpoint.OnSaving(message, entity, mode))
                    {
                        return;
                    }

                    var bypass = SaveBehaviour.BypassAll;

                    if (Config.Get("Database:Audit:EnableForEndpointDataReplication", defaultValue: false))
                    {
                        bypass &= ~SaveBehaviour.BypassLogging;
                    }

                    await Database.Save(entity, bypass);

                    await GlobalEntityEvents.OnInstanceSaved(new GlobalSaveEventArgs(entity, mode));

                    await Endpoint.OnSaved(message, entity, mode);

                    Log.Debug("Saved the " + entity.GetType().FullName + " " + entity.GetId());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to import.");
                throw;
            }
        }
Exemplo n.º 5
0
        public static EntityType ExtractMetadata(Type runtimeType, bool tryToFindParent = false, bool tryToFindProperties = true)
        {
            if (runtimeType.TypeIsNotCorrect())
            {
                return(null);
            }

            var propertyList = new List <Property>();
            var dbFieldsList = new List <Property>();

            var allPropertiesList  = Enumerable.Empty <Property>();
            var assignIdByDatabase = false;

            if (tryToFindProperties)
            {
                propertyList = GetPropertiesOfEntityObject(runtimeType);
                var parentProperties       = GetPropertiesOfEntityObject(runtimeType.GetTypeInfo().BaseType);
                var parentWithoutDuplicate = parentProperties.Where(p => propertyList.None(p2 => p2.Name == p.Name));
                allPropertiesList = propertyList.Concat(parentWithoutDuplicate);
            }

            var allParents = tryToFindParent ? GetAllAncestors(runtimeType) : null;

            var primaryKeyProperty        = "";
            var idColumnName              = "id";
            var hasCustomPrimaryKeyColumn = false;

            var primaryKeyProp = runtimeType.GetProperties().FirstOrDefault(x => Attribute.IsDefined(x, typeof(PrimaryKeyAttribute)) && x.Name.ToLower() != "id");

            if (primaryKeyProp != null)
            {
                primaryKeyProperty        = primaryKeyProp.PropertyType.ToString();
                idColumnName              = primaryKeyProp.Name;
                hasCustomPrimaryKeyColumn = true;
            }
            else
            if (runtimeType.GetProperties().Any(x => x.Name.ToLower() == "id"))
            {
                primaryKeyProperty = runtimeType.GetProperties().First(x => x.Name.ToLower() == "id").PropertyType.ToString();
            }

            return(new EntityType
            {
                Type = runtimeType,
                AssemblyFullName = runtimeType.AssemblyQualifiedName,
                ClassFullName = runtimeType.FullName,
                ClassName = runtimeType.Name,
                TableName = TableNameAttribute.GetTableName(runtimeType),
                Name = runtimeType.Name.ToPlural(),
                AllProperties = allPropertiesList.ToArray(),
                DirectDatabaseFields = propertyList.ToArray(),
                Properties = propertyList.ToArray(),
                IdColumnName = idColumnName,
                PrimaryKeyType = primaryKeyProperty,
                PluralName = runtimeType.Name.ToPlural(),
                AllParents = runtimeType.BaseType != null ? allParents : null,
                WithAllParents = allParents,

                IsTransient = Attribute.IsDefined(runtimeType, typeof(TransientEntityAttribute)),
                IsAbstract = runtimeType.IsAbstract,
                SoftDelete = Attribute.IsDefined(runtimeType, typeof(SoftDeleteAttribute)),

                AssignIDByDatabase = assignIdByDatabase,
                HasCustomPrimaryKeyColumn = hasCustomPrimaryKeyColumn,
                HasDataAccessClass = true,
                //HasKnownDatabase = Attribute.IsDefined(runtimeType, typeof(HasKnownDatabaseAttribute)),
                BaseType = GetSmallEntityType(runtimeType.BaseType),
            });
        }