Exemplo n.º 1
0
 public SyncConfiguration(Type[] types, TimeStampStrategyEnum timeStampStrategy)
 {
     if (types == null)
     {
         throw new NullReferenceException(nameof(types));
     }
     Build(types, timeStampStrategy);
 }
Exemplo n.º 2
0
 public SyncConfiguration(Assembly[] assemblies, TimeStampStrategyEnum timeStampStrategy)
 {
     if (assemblies == null)
     {
         throw new NullReferenceException(nameof(assemblies));
     }
     Type[] types = assemblies.SelectMany(sm => sm.GetTypes()).Where(w => Attribute.IsDefined(w, typeof(SyncSchemaAttribute))).ToArray();
     Build(types, timeStampStrategy);
 }
Exemplo n.º 3
0
        private void Build(Type[] types, TimeStampStrategyEnum timeStampStrategy)
        {
            TimeStampStrategy = timeStampStrategy;

            SyncTypes.Clear();
            SyncSchemaInfos.Clear();

            for (int i = 0; i < types.Length; i++)
            {
                Type type = types[i];
                if (SyncTypes.Contains(type))
                {
                    throw new SyncConfigurationDuplicateTypeException(type);
                }

                SchemaInfo schemaInfo = new SchemaInfo();

                schemaInfo.SyncSchemaAttribute = type.GetCustomAttribute <SyncSchemaAttribute>();
                if (schemaInfo.SyncSchemaAttribute == null)
                {
                    throw new SyncConfigurationMissingSyncSchemaAttributeException(type);
                }

                PropertyInfo propertyInfoId = type.GetProperties().Where(w => w.GetCustomAttribute <SyncPropertyAttribute>() != null && w.GetCustomAttribute <SyncPropertyAttribute>().PropertyIndicator == SyncPropertyAttribute.PropertyIndicatorEnum.Id).FirstOrDefault();
                if (propertyInfoId == null)
                {
                    throw new SyncConfigurationMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.Id, type);
                }
                schemaInfo.PropertyInfoId = new SchemaInfoProperty()
                {
                    Name = propertyInfoId.Name, PropertyType = propertyInfoId.PropertyType.FullName
                };

                PropertyInfo propertyInfoLastUpdated = type.GetProperties().Where(w => w.GetCustomAttribute <SyncPropertyAttribute>() != null && w.GetCustomAttribute <SyncPropertyAttribute>().PropertyIndicator == SyncPropertyAttribute.PropertyIndicatorEnum.LastUpdated).FirstOrDefault();
                if (propertyInfoLastUpdated == null)
                {
                    throw new SyncConfigurationMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.LastUpdated, type);
                }
                if (propertyInfoLastUpdated.PropertyType != typeof(long))
                {
                    throw new SyncConfigurationMismatchPropertyTypeException(propertyInfoLastUpdated, typeof(long), type);
                }
                schemaInfo.PropertyInfoLastUpdated = new SchemaInfoProperty()
                {
                    Name = propertyInfoLastUpdated.Name, PropertyType = propertyInfoLastUpdated.PropertyType.FullName
                };

                PropertyInfo propertyInfoDeleted = type.GetProperties().Where(w => w.GetCustomAttribute <SyncPropertyAttribute>() != null && w.GetCustomAttribute <SyncPropertyAttribute>().PropertyIndicator == SyncPropertyAttribute.PropertyIndicatorEnum.Deleted).FirstOrDefault();
                if (propertyInfoDeleted == null)
                {
                    throw new SyncConfigurationMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.Deleted, type);
                }
                if (TimeStampStrategy == TimeStampStrategyEnum.GlobalTimeStamp && propertyInfoDeleted.PropertyType != typeof(long?))
                {
                    throw new SyncConfigurationMismatchPropertyTypeException(propertyInfoDeleted, typeof(long?), type);
                }
                if (TimeStampStrategy == TimeStampStrategyEnum.DatabaseTimeStamp && propertyInfoDeleted.PropertyType != typeof(bool))
                {
                    throw new SyncConfigurationMismatchPropertyTypeException(propertyInfoDeleted, typeof(bool), type);
                }
                schemaInfo.PropertyInfoDeleted = new SchemaInfoProperty()
                {
                    Name = propertyInfoDeleted.Name, PropertyType = propertyInfoDeleted.PropertyType.FullName
                };

                PropertyInfo propertyInfoFriendlyId = type.GetProperties().Where(w => w.GetCustomAttribute <SyncFriendlyIdAttribute>() != null).FirstOrDefault();
                if (propertyInfoFriendlyId != null)
                {
                    if (propertyInfoFriendlyId.PropertyType != typeof(string))
                    {
                        throw new SyncConfigurationMismatchPropertyTypeException(propertyInfoFriendlyId, typeof(string), type);
                    }
                    schemaInfo.PropertyInfoFriendlyId = new SchemaInfoProperty()
                    {
                        Name = propertyInfoFriendlyId.Name, PropertyType = propertyInfoFriendlyId.PropertyType.FullName
                    };
                }

                if (TimeStampStrategy == TimeStampStrategyEnum.DatabaseTimeStamp)
                {
                    PropertyInfo propertyInfoDatabaseInstanceId = type.GetProperties().Where(w => w.GetCustomAttribute <SyncPropertyAttribute>() != null && w.GetCustomAttribute <SyncPropertyAttribute>().PropertyIndicator == SyncPropertyAttribute.PropertyIndicatorEnum.DatabaseInstanceId).FirstOrDefault();
                    if (propertyInfoDatabaseInstanceId == null)
                    {
                        throw new SyncConfigurationMissingSyncPropertyAttributeException(SyncPropertyAttribute.PropertyIndicatorEnum.DatabaseInstanceId, type);
                    }
                    if (propertyInfoDatabaseInstanceId.PropertyType != typeof(string))
                    {
                        throw new SyncConfigurationMismatchPropertyTypeException(propertyInfoDatabaseInstanceId, typeof(string), type);
                    }
                    schemaInfo.PropertyInfoDatabaseInstanceId = new SchemaInfoProperty()
                    {
                        Name = propertyInfoDatabaseInstanceId.Name, PropertyType = propertyInfoDatabaseInstanceId.PropertyType.FullName
                    };
                }

                SyncTypes.Add(type);
                SyncSchemaInfos.Add(type, schemaInfo);
            }
        }