private void AppendSignaturePropertyCriteriaTo<TId>(ICriteria criteria, IEntityWithTypedId<TId> entity) { foreach (var signatureProperty in entity.GetSignatureProperties()) { var propertyType = signatureProperty.PropertyType; var propertyValue = signatureProperty.GetValue(entity, null); if (propertyType.IsEnum) { criteria.Add(Restrictions.Eq(signatureProperty.Name, (int)propertyValue)); } else if ( propertyType.GetInterfaces().Any( x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>))) { AppendEntityCriteriaTo<TId>(criteria, signatureProperty, propertyValue); } else if (propertyType == typeof(DateTime)) { this.AppendDateTimePropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else if (propertyType == typeof(string)) { AppendStringPropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else if (propertyType.IsValueType) { AppendValuePropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else { throw new ApplicationException( "Can't determine how to use " + entity.GetType() + "." + signatureProperty.Name + " when looking for duplicate entries. To remedy this, " + "you can create a custom validator or report an issue to the S#arp Architecture " + "project, detailing the type that you'd like to be accommodated."); } } }
static void AppendSignaturePropertyCriteriaTo <TId>(ICriteria criteria, IEntityWithTypedId <TId> entity) { foreach (var signatureProperty in entity.GetSignatureProperties()) { var propertyType = signatureProperty.PropertyType; var propertyValue = signatureProperty.GetValue(entity, null); var propertyName = signatureProperty.Name; if (propertyType.GetInterfaces().Any( x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId <>))) { AppendEntityCriteriaTo <TId>(criteria, propertyName, propertyValue); } else if (typeof(ValueObject).IsAssignableFrom(propertyType)) { AppendValueObjectSignaturePropertyCriteriaTo(criteria, entity.GetType(), propertyName, propertyValue as ValueObject); } else { AppendSimplePropertyCriteriaTo(criteria, entity.GetType(), propertyValue, propertyType, propertyName); } } }
/// <summary> /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity. /// </summary> /// <typeparam name="TId">Entity Id type.</typeparam> /// <param name="entity">The entity.</param> /// <returns> /// <c>true</c> if a duplicate exists, <c>false</c> otherwise. /// </returns> /// <exception cref="System.ArgumentNullException">entity is null. </exception> public bool DoesDuplicateExistWithTypedIdOf <TId>(IEntityWithTypedId <TId> entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } var sessionForEntity = GetSessionFor(entity); var previousFlushMode = sessionForEntity.FlushMode; // We do NOT want this to flush pending changes as checking for a duplicate should // only compare the object against data that's already in the database sessionForEntity.FlushMode = FlushMode.Manual; try { var criteria = sessionForEntity.CreateCriteria(entity.GetType()) .Add(Restrictions.Not(Restrictions.Eq("Id", entity.Id))) .SetMaxResults(1); AppendSignaturePropertyCriteriaTo(criteria, entity); var doesDuplicateExist = criteria.List().Count > 0; return(doesDuplicateExist); } finally { sessionForEntity.FlushMode = previousFlushMode; } }
protected override ValidationResult IsValid(object value, ValidationContext validationContext) { IEntityWithTypedId <int> entityToValidate = validationContext.ObjectInstance as IEntityWithTypedId <int>; if (entityToValidate == null) { throw new InvalidOperationException( "This validator must be used at the class level of an IEntityWithTypedId<int>. " + "The type you provided was " + validationContext.ObjectInstance.GetType()); } IEntityDuplicateChecker duplicateChecker = DependencyResolver.Current.GetService <IEntityDuplicateChecker>(); if (duplicateChecker == null) { throw new TypeLoadException("IEntityDuplicateChecker has not been registered with IoC"); } if (duplicateChecker.DoesDuplicateExistWithTypedIdOf(entityToValidate)) { return(new ValidationResult(String.Empty)); } return(null); }
/// <summary> /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity. /// </summary> public bool DoesDuplicateExistWithTypedIdOf <TId>(IEntityWithTypedId <TId> entity) { if (entity == null) { throw new ArgumentNullException("Entity may not be null when checking for duplicates"); } throw new NotImplementedException(); }
/// <summary> /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity. /// </summary> public bool DoesDuplicateExistWithTypedIdOf <TId>(IEntityWithTypedId <TId> entity) { if (entity == null) { throw new ArgumentNullException("entity"); } return(false); }
/// <summary> /// Uses reflection to set the Id of a <see cref = "EntityWithTypedId{IdT}" />. /// </summary> public static void SetIdOf <TId>(IEntityWithTypedId <TId> entity, TId id) { // Set the data property reflectively var idProperty = entity.GetType().GetProperty("Id", BindingFlags.Public | BindingFlags.Instance); Check.Ensure(idProperty != null, "idProperty could not be found"); idProperty.SetValue(entity, id, null); }
public bool IsValid(object value, IConstraintValidatorContext constraintValidatorContext) { IEntityWithTypedId <int> entityToValidate = value as IEntityWithTypedId <int>; Check.Require(entityToValidate != null, "This validator must be used at the class level of an " + "IdomainWithTypedId<int>. The type you provided was " + value.GetType().ToString()); IEntityDuplicateChecker duplicateChecker = SafeServiceLocator <IEntityDuplicateChecker> .GetService(); return(!duplicateChecker.DoesDuplicateExistWithTypedIdOf <int>(entityToValidate)); }
public bool DoesDuplicateExistWithTypedIdOf <IdT>(IEntityWithTypedId <IdT> entity) { Check.Require(entity != null); if (entity as Employee != null) { Employee employee = entity as Employee; return(employee.FirstName == "Billy" && employee.FirstName == "McCafferty"); } // By default, simply return false for no duplicates found return(false); }
public bool IsValid(object value, IConstraintValidatorContext constraintValidatorContext) { IEntityWithTypedId <string> entityToValidate = value as IEntityWithTypedId <string>; Check.Require(entityToValidate != null, "This validator must be used at the class level of an " + "IdomainWithTypedId<string>. The type you provided was " + value.GetType().ToString() + ". " + "Other validators exist for various Id types. Please open an issue with S#arp Architecture " + "if you need a new Id type supported; you can make your own in the meantime."); IEntityDuplicateChecker duplicateChecker = SafeServiceLocator <IEntityDuplicateChecker> .GetService(); return(!duplicateChecker.DoesDuplicateExistWithTypedIdOf <string>(entityToValidate)); }
/// <summary> /// Uses reflection to set the Id of a <see cref="EntityWithTypedId{IdT}" />. /// </summary> /// <exception cref="ArgumentNullException"><paramref name="entity"/> is <see langword="null" />.</exception> /// <exception cref="InvalidOperationException">Property with name 'Id' could not be found.</exception> public static void SetIdOf <TId>([NotNull] IEntityWithTypedId <TId> entity, TId id) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } // Set the data property reflectively PropertyInfo idProperty = entity.GetType().GetProperty("Id", BindingFlags.Public | BindingFlags.Instance); if (idProperty == null) { throw new InvalidOperationException("Property with name 'Id' could not be found."); } idProperty.SetValue(entity, id, null); }
public bool DoesDuplicateExistWithTypedIdOf <TId>(IEntityWithTypedId <TId> entity) { switch (entity) { case Contractor contractor: return(!string.IsNullOrEmpty(contractor.Name) && string.Equals(contractor.Name, @"codai", StringComparison.OrdinalIgnoreCase)); case User user: return(!string.IsNullOrEmpty(user.Ssn) && user.Ssn == "123-12-1234"); case ObjectWithGuidId objectWithGuidId: return(!string.IsNullOrEmpty(objectWithGuidId.Name) && string.Equals(objectWithGuidId.Name, @"codai", StringComparison.OrdinalIgnoreCase)); } // By default, simply return false for no duplicates found return(false); }
/// <summary> /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity. /// </summary> public bool DoesDuplicateExistWithTypedIdOf<TId>(IEntityWithTypedId<TId> entity) { if (entity == null) throw new ArgumentNullException("Entity may not be null when checking for duplicates"); ISession session = _sessionFactory.GetCurrentSession(); FlushMode previousFlushMode = session.FlushMode; // We do NOT want this to flush pending changes as checking for a duplicate should // only compare the object against data that's already in the database session.FlushMode = FlushMode.Never; var criteria = session.CreateCriteria(entity.GetType()).Add(Restrictions.Not(Restrictions.Eq("Id", entity.Id))). SetMaxResults(1); AppendSignaturePropertyCriteriaTo(criteria, entity); bool doesDuplicateExist = criteria.List().Count > 0; session.FlushMode = previousFlushMode; return doesDuplicateExist; }
/// <summary> /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity. /// </summary> public bool DoesDuplicateExistWithTypedIdOf <TId>(IEntityWithTypedId <TId> entity) { Check.Require(entity != null, "Entity may not be null when checking for duplicates"); var session = GetSessionFor(entity); var previousFlushMode = session.FlushMode; // We do NOT want this to flush pending changes as checking for a duplicate should // only compare the object against data that's already in the database session.FlushMode = FlushMode.Never; var criteria = session.CreateCriteria(entity.GetType()).Add(Restrictions.Not(Restrictions.Eq("Id", entity.Id))).SetMaxResults(1); AppendSignaturePropertyCriteriaTo(criteria, entity); var doesDuplicateExist = criteria.List().Count > 0; session.FlushMode = previousFlushMode; return(doesDuplicateExist); }
public bool DoesDuplicateExistWithTypedIdOf <IdT>(IEntityWithTypedId <IdT> entity) { Trace.Assert(entity != null); if (entity is Contractor) { var contractor = entity as Contractor; return(!string.IsNullOrEmpty(contractor.Name) && contractor.Name.ToLower() == @"codai"); } if (entity is User) { var user = entity as User; return(!string.IsNullOrEmpty(user.SSN) && user.SSN.ToLower() == "123-12-1234"); } if (entity is ObjectWithGuidId) { var objectWithGuidId = entity as ObjectWithGuidId; return(!string.IsNullOrEmpty(objectWithGuidId.Name) && objectWithGuidId.Name.ToLower() == @"codai"); } // By default, simply return false for no duplicates found return(false); }
public bool DoesDuplicateExistWithTypedIdOf <IdT>(IEntityWithTypedId <IdT> entity) { Check.Require(entity != null); if (entity as Contractor != null) { Contractor contractor = entity as Contractor; return(!string.IsNullOrEmpty(contractor.Name) && contractor.Name.ToLower() == "codai"); } else if (entity as User != null) { User user = entity as User; return(!string.IsNullOrEmpty(user.SSN) && user.SSN.ToLower() == "123-12-1234"); } else if (entity as ObjectWithGuidId != null) { ObjectWithGuidId objectWithGuidId = entity as ObjectWithGuidId; return(!string.IsNullOrEmpty(objectWithGuidId.Name) && objectWithGuidId.Name.ToLower() == "codai"); } // By default, simply return false for no duplicates found return(false); }
private void AppendSignaturePropertyCriteriaTo <IdT>(ICriteria criteria, IEntityWithTypedId <IdT> entity) { foreach (PropertyInfo signatureProperty in entity.GetSignatureProperties()) { Type propertyType = signatureProperty.PropertyType; object propertyValue = signatureProperty.GetValue(entity, null); if (propertyType.IsEnum) { criteria.Add( Expression.Eq(signatureProperty.Name, (int)propertyValue)); } else if (propertyType.IsSubclassOf(typeof(IEntityWithTypedId <IdT>))) { AppendEntityCriteriaTo(criteria, signatureProperty, propertyValue); } else if (propertyType == typeof(DateTime)) { AppendDateTimePropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else if (propertyType == typeof(String)) { AppendStringPropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else if (propertyType.IsValueType) { AppendValuePropertyCriteriaTo(criteria, signatureProperty, propertyValue); } else { throw new ApplicationException("Can't determine how to use " + entity.GetType() + "." + signatureProperty.Name + " when looking for duplicate entries. To remedy this, " + "you can create a custom validator or report an issue to the S#arp Architecture " + "project, detailing the type that you'd like to be accommodated."); } } }
public bool DoesDuplicateExistWithTypedIdOf <IdT>(IEntityWithTypedId <IdT> entity) { return(false); }
/// <summary> /// Uses reflection to set the Id of a <see cref="EntityWithTypedId{IdT}" />. /// </summary> public static IEntityWithTypedId <TId> SetIdTo <TId>(this IEntityWithTypedId <TId> entity, TId id) { SetIdOf(entity, id); return(entity); }
/// <summary> /// Uses reflection to set the Id of a <see cref="EntityWithTypedId{IdT}" />. /// </summary> public static IEntityWithTypedId <TId> SetIdTo <TId>(this IEntityWithTypedId <TId> entity, TId id) where TId : IEquatable <TId> { SetIdOf(entity, id); return(entity); }
/// <summary> /// Returns true if self and the provided entity have the same Id values /// and the Ids are not of the default Id value /// </summary> private bool HasSameNonDefaultIdAs(IEntityWithTypedId <TId> compareTo) { return(!IsTransient() && !compareTo.IsTransient() && Id.Equals(compareTo.Id)); }