示例#1
0
文件: Property.cs 项目: Jan-Olof/CQRS
 /// <summary>
 /// Create a property object.
 /// </summary>
 public static Property CreateProperty(PropertyType propertyType, string value, Registration registration)
 {
     return new Property
     {
         PropertyTypeId = propertyType.Id,
         PropertyType = propertyType,
         Value = value,
         Registrations = new Collection<Registration> { registration }
     };
 }
示例#2
0
        /// <summary>
        /// Add a property to a registration.
        /// </summary>
        public void AddProperty(PropertyType propertyType, KeyValuePair<string, string> property, Registration registration)
        {
            var registrationProperty = this.CheckIfPropertyIsRegistered(propertyType, property.Value);

            if (registrationProperty.Id == 0)
            {
                this.AddPropertyToDbSet(propertyType, property.Value, registration);
            }
            else
            {
                this.AddRegistrationToProperty(registrationProperty, registration);
            }
        }
示例#3
0
 /// <summary>
 /// Create registration dto from registration.
 /// </summary>
 public static RegistrationDto CreateRegistrationDto(Registration registration)
 {
     return new RegistrationDto
     {
         Id = registration.Id,
         Name = registration.Name,
         Created = registration.Created,
         Updated = registration.Updated,
         OriginalWriteEventId = registration.OriginalWriteEventId,
         RegistrationTypeId = registration.RegistrationType.Id,
         RegistrationTypeName = registration.RegistrationType.Name,
         RegistrationProperties = CreateRegistrationPropertiesDtos(registration.Properties)
     };
 }
示例#4
0
        /// <summary>
        /// Add property to db set.
        /// </summary>
        public Property AddPropertyToDbSet(PropertyType type, string value, Registration registration)
        {
            try
            {
                var property = Property.CreateProperty(type, value, registration);

                this.readContext.Properties.Add(property);
                registration.Properties.Add(property);

                return property;
            }
            catch (Exception ex)
            {
                this.logger.Error(ex);
                throw;
            }
        }
示例#5
0
        /// <summary>
        /// Add both property type and property to a registration.
        /// </summary>
        private void AddPropertyTypeAndProperty(Registration registration, KeyValuePair<string, string> gdtoProperty)
        {
            var propertyType = this.GetPropertyType(gdtoProperty.Key);

            if (propertyType.Id == 0)
            {
                propertyType = this.AddPropertyTypeToDbSet(gdtoProperty.Key);
            }

            if (propertyType.Id > -1)
            {
                this.AddProperty(propertyType, gdtoProperty, registration);
            }
        }
示例#6
0
        /// <summary>
        /// Add new properties to a registration.
        /// </summary>
        private Registration AddNewProperties(Registration registration, Gdto gdto)
        {
            if (registration.Properties == null || gdto.Properties == null)
            {
                return registration;
            }

            foreach (var keyValuePair in gdto.Properties)
            {
                bool exists = CheckIfPropertyExistInRegistration(registration, keyValuePair);

                if (!exists)
                {
                    this.AddPropertyTypeAndProperty(registration, keyValuePair);
                }
            }

            return registration;
        }
示例#7
0
        /// <summary>
        /// Update the values of existing properties with new values from the Gdto.
        /// </summary>
        private static Registration UpdateExistingProperties(Registration registration, Gdto gdto)
        {
            if (registration.Properties == null || gdto.Properties == null)
            {
                return registration;
            }

            foreach (var property in registration.Properties)
            {
                UpdateProperty(gdto, property);
            }

            return registration;
        }
示例#8
0
        /// <summary>
        /// Get the properties that should be deleted.
        /// </summary>
        private static IList<Property> GetPropertiesToDelete(Registration registration, Gdto gdto)
        {
            var toDelete = new List<Property>();

            foreach (var property in registration.Properties)
            {
                bool exists = CheckIfPropertyExistInGdto(gdto.Properties, property);

                if (!exists)
                {
                    toDelete.Add(property);
                }
            }

            return toDelete;
        }
示例#9
0
        /// <summary>
        /// Delete properties that does not exist in the GDTO.
        /// </summary>
        private static Registration DeleteNonExistingProperties(Registration registration, Gdto gdto)
        {
            if (registration.Properties == null || gdto.Properties == null)
            {
                return registration;
            }

            var toDelete = GetPropertiesToDelete(registration, gdto);

            foreach (var property in toDelete)
            {
                registration.Properties.Remove(property);
            }

            return registration;
        }
示例#10
0
        /// <summary>
        /// Check if a property exists in a registration.
        /// </summary>
        private static bool CheckIfPropertyExistInRegistration(Registration registration, KeyValuePair<string, string> gdtoProperty)
        {
            bool exists = false;
            foreach (var property in registration.Properties)
            {
                if (string.Equals(property.PropertyType.Name, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase)
                    || string.Equals(Name, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase)
                    || string.Equals(OriginalWriteEventId, gdtoProperty.Key, StringComparison.CurrentCultureIgnoreCase))
                {
                    exists = true;
                }
            }

            return exists;
        }
示例#11
0
 /// <summary>
 /// Update a registration.
 /// </summary>
 public Registration UpdateRegistration(Registration registration, RegistrationType type, DateTime timestamp, string name)
 {
     try
     {
         return Registration.UpdateRegistration(registration, type, timestamp, name);
     }
     catch (Exception ex)
     {
         this.logger.Error(ex);
         throw;
     }
 }
示例#12
0
        /// <summary>
        /// Update all properties for a registration. Also inserting new and removing old properties.
        /// </summary>
        public Registration UpdateProperties(Registration registration, Gdto gdto)
        {
            try
            {
                if (registration == null || gdto == null)
                {
                    return registration;
                }

                registration = UpdateExistingProperties(registration, gdto);

                registration = this.AddNewProperties(registration, gdto);

                registration = DeleteNonExistingProperties(registration, gdto);
            }
            catch (Exception ex)
            {
                this.logger.Error(ex);
                throw;
            }

            return registration;
        }
示例#13
0
        /// <summary>
        /// Delete a registration.
        /// </summary>
        public bool DeleteRegistration(Registration registration)
        {
            try
            {
                var properties = new List<Property>();
                properties.AddRange(registration.Properties);

                foreach (var property in properties)
                {
                    this.readContext.Properties.Remove(property);
                }

                this.readContext.Registrations.Remove(registration);
                return true;
            }
            catch (Exception ex)
            {
                this.logger.Error(ex);
                return false;
            }
        }
示例#14
0
        /// <summary>
        /// Add a registration to a property.
        /// </summary>
        public bool AddRegistrationToProperty(Property property, Registration registration)
        {
            try
            {
                if (property.Registrations == null)
                {
                    property.Registrations = new Collection<Registration>();
                }

                property.Registrations.Add(registration);
            }
            catch (Exception ex)
            {
                this.logger.Error(ex);
                throw;
            }

            return true;
        }
示例#15
0
 /// <summary>
 /// Add properties to a registration.
 /// </summary>
 public void AddRegistrationProperties(Gdto gdto, Registration registration)
 {
     foreach (var property in gdto.Properties)
     {
         this.AddPropertyTypeAndProperty(registration, property);
     }
 }
示例#16
0
        /// <summary>
        /// Update a registration.
        /// </summary>
        public static Registration UpdateRegistration(Registration registration, RegistrationType type, DateTime timestamp, string name)
        {
            registration.RegistrationType = type;
            registration.RegistrationTypeId = type.Id;
            registration.Name = name;
            registration.Updated = timestamp;

            return registration;
        }