Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        /// <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;
            }
        }
Пример #3
0
        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.");
                }
            }
        }
Пример #4
0
        /// <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);
        }
        /// <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);
        }
Пример #6
0
        /// <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;
        }
Пример #7
0
        /// <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);
        }
        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.");
                }
            }
        }