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); } } }
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."); } } }