Exemple #1
0
        public static UniqueConstraintCheckResult <T> CheckForUniqueConstraints <T>(this IDocumentSession session, T entity)
        {
            var properties = UniqueConstraintsTypeDictionary.GetProperties(typeof(T));

            T[] loadedDocs = null;
            var typeName   = session.Advanced.DocumentStore.Conventions.GetTypeTagName(typeof(T));

            var constraintsIds = from property in properties
                                 let propertyValue                     = property.GetValue(entity, null)
                                                               let att = (UniqueConstraintAttribute)Attribute.GetCustomAttribute(property, typeof(UniqueConstraintAttribute))
                                                                         where propertyValue != null
                                                                         from item in typeof(IEnumerable).IsAssignableFrom(property.PropertyType) && property.PropertyType != typeof(string) ? ((IEnumerable)propertyValue).Cast <object>().Where(i => i != null) : new [] { propertyValue }
            select
            "UniqueConstraints/" + typeName.ToLowerInvariant() + "/" + property.Name.ToLowerInvariant() +
            "/" + Raven.Bundles.UniqueConstraints.Util.EscapeUniqueValue(item.ToString(), att.CaseInsensitive);

            var constraintDocs = session.Include <ConstraintDocument>(x => x.RelatedId).Load(constraintsIds.ToArray());

            var existingDocsIds = constraintDocs.Where(constraintDoc => constraintDoc != null)
                                  .Select(constraintDoc => constraintDoc.RelatedId)
                                  .Where(id => string.IsNullOrEmpty(id) == false)
                                  .ToArray();

            if (existingDocsIds.Any())
            {
                loadedDocs = session.Load <T>(existingDocsIds);
            }

            return(new UniqueConstraintCheckResult <T>(entity, properties, loadedDocs));
        }
Exemple #2
0
        public static UniqueConstraintCheckResult <T> CheckForUniqueConstraints <T>(this IDocumentSession session, T entity)
        {
            var properties = UniqueConstraintsTypeDictionary.GetProperties(typeof(T));

            T[] loadedDocs = null;

            if (properties != null)
            {
                var typeName = session.Advanced.DocumentStore.Conventions.GetTypeTagName(typeof(T));

                var constraintsIds = from property in properties
                                     let propertyValue = property.GetValue(entity, null)
                                                         where propertyValue != null
                                                         select "UniqueConstraints/" + typeName.ToLowerInvariant() + "/" + property.Name.ToLowerInvariant() + "/" + Raven.Bundles.UniqueConstraints.Util.EscapeUniqueValue(propertyValue.ToString());

                var constraintDocs = session.Include <ConstraintDocument>(x => x.RelatedId).Load(constraintsIds.ToArray());

                var existingDocsIds = constraintDocs.Where(constraintDoc => constraintDoc != null)
                                      .Select(constraintDoc => constraintDoc.RelatedId)
                                      .Where(id => string.IsNullOrEmpty(id) == false)
                                      .ToArray();

                if (existingDocsIds.Any())
                {
                    loadedDocs = session.Load <T>(existingDocsIds);
                }
            }

            return(new UniqueConstraintCheckResult <T>(entity, properties, loadedDocs));
        }
Exemple #3
0
        public UniqueConstraintsStoreListener(UniqueConstraintsTypeDictionary dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            this.UniqueConstraintsTypeDictionary = dictionary;
        }
Exemple #4
0
        internal static ConstraintInfo[] GetUniquePropertiesForType(this IDocumentStore store, Type type)
        {
            UniqueConstraintsTypeDictionary dictionary = UniqueConstraintsTypeDictionary.FindDictionary(store);

            if (dictionary != null)
            {
                return(dictionary.GetProperties(type));
            }

            return(null);
        }
        public static UniqueConstraintCheckResult <T> CheckForUniqueConstraints <T>(this IDocumentSession session, T entity)
        {
            var properties = UniqueConstraintsTypeDictionary.GetProperties(typeof(T));

            T[] loadedDocs = null;

            if (properties != null)
            {
                var typeName        = session.Advanced.DocumentStore.Conventions.GetTypeTagName(typeof(T));
                var constraintsIds  = new List <string>();
                var existingDocsIds = new List <string>();

                foreach (var property in properties)
                {
                    var propertyValue = property.GetValue(entity, null);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    constraintsIds.Add("UniqueConstraints/" + typeName.ToLowerInvariant() + "/" + property.Name.ToLowerInvariant() + "/" +
                                       Raven.Bundles.UniqueConstraints.Util.EscapeUniqueValue(propertyValue.ToString()));
                }

                ConstraintDocument[] constraintDocs = session.Include("Id").Load <ConstraintDocument>(constraintsIds.ToArray());

                foreach (var constraintDoc in constraintDocs)
                {
                    if (constraintDoc == null)
                    {
                        continue;
                    }

                    var id = constraintDoc.RelatedId;
                    if (!string.IsNullOrEmpty(id))
                    {
                        existingDocsIds.Add(id);
                    }
                }

                if (existingDocsIds.Count > 0)
                {
                    loadedDocs = session.Load <T>(existingDocsIds.ToArray());
                }
            }

            return(new UniqueConstraintCheckResult <T>(entity, properties, loadedDocs));
        }
Exemple #6
0
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            if (metadata[Constants.EnsureUniqueConstraints] != null)
            {
                return(true);
            }

            var type = entityInstance.GetType();

            var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

            if (properties != null)
            {
                metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x => x.Name).ToArray()));
            }

            return(true);
        }
Exemple #7
0
        public static Task <UniqueConstraintCheckResult <T> > CheckForUniqueConstraintsAsync <T>(this IAsyncDocumentSession session, T entity)
        {
            var properties = UniqueConstraintsTypeDictionary.GetProperties(typeof(T));

            if (properties != null)
            {
                var typeName = session.Advanced.DocumentStore.Conventions.GetTypeTagName(typeof(T));

                var constraintsIds = from property in properties
                                     let propertyValue                     = property.GetValue(entity, null)
                                                                   let att = (UniqueConstraintAttribute)Attribute.GetCustomAttribute(property, typeof(UniqueConstraintAttribute))
                                                                             where propertyValue != null
                                                                             select "UniqueConstraints/" + typeName.ToLowerInvariant() + "/" + property.Name.ToLowerInvariant() + "/" + Raven.Bundles.UniqueConstraints.Util.EscapeUniqueValue(propertyValue.ToString(), att.CaseInsensitive);

                return(session.Include <ConstraintDocument>(x => x.RelatedId).LoadAsync(constraintsIds.ToArray())
                       .ContinueWith(task =>
                {
                    var constraintDocs = task.Result;
                    var existingDocsIds = (from constraintDoc in constraintDocs
                                           where constraintDoc != null
                                           select constraintDoc.RelatedId
                                           into id where !string.IsNullOrEmpty(id) select id).ToArray();

                    if (existingDocsIds.Any() == false)
                    {
                        return new CompletedTask <UniqueConstraintCheckResult <T> >(new UniqueConstraintCheckResult <T>(entity, properties, null));
                    }

                    return session.LoadAsync <T>(existingDocsIds.ToArray()).ContinueWith(loadTask =>
                    {
                        var completedTask = new CompletedTask <UniqueConstraintCheckResult <T> >(
                            new UniqueConstraintCheckResult <T>(entity, properties, loadTask.Result)
                            );

                        return (Task <UniqueConstraintCheckResult <T> >)completedTask;
                    }).Unwrap();
                }).Unwrap());
            }

            return(new CompletedTask <UniqueConstraintCheckResult <T> >(new UniqueConstraintCheckResult <T>(entity, properties, null)));
        }
        public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original)
        {
            if (metadata[Constants.EnsureUniqueConstraints] != null)
            {
                return(true);
            }

            var type = entityInstance.GetType();

            var properties = UniqueConstraintsTypeDictionary.GetProperties(type);

            if (properties != null)
            {
                metadata.Add(Constants.EnsureUniqueConstraints, new RavenJArray(properties.Select(x =>
                {
                    var att = ((UniqueConstraintAttribute)Attribute.GetCustomAttribute(x, typeof(UniqueConstraintAttribute)));
                    return(RavenJObject.FromObject(new { x.Name, att.CaseInsensitive }));
                })));
            }

            return(true);
        }
		public UniqueConstraintsStoreListener(UniqueConstraintsTypeDictionary dictionary)
		{
			if (dictionary == null) { throw new ArgumentNullException("dictionary"); }

			this.UniqueConstraintsTypeDictionary = dictionary;
		}
Exemple #10
0
        private static MemberExpression GetMemberExpression <T>(Expression <Func <T, object> > keySelector, UniqueConstraintsTypeDictionary uniqueConstraintsTypeDictionary)
        {
            MemberExpression body;

            if (keySelector.Body is MemberExpression)
            {
                body = ((MemberExpression)keySelector.Body);
            }
            else
            {
                var op = ((UnaryExpression)keySelector.Body).Operand;
                body = ((MemberExpression)op);
            }
            bool isDef = Attribute.IsDefined(body.Member, typeof(UniqueConstraintAttribute));


            if (isDef == false)
            {
                if (uniqueConstraintsTypeDictionary.GetProperties(body.Member.DeclaringType).Any(x => x.Configuration.Name == body.Member.Name))
                {
                    return(body);
                }


                var msg = string.Format(
                    "You are calling LoadByUniqueConstraints on {0}.{1}, but you haven't marked this property with [UniqueConstraint]",
                    body.Member.DeclaringType.Name, body.Member.Name);
                throw new InvalidOperationException(msg);
            }
            return(body);
        }
Exemple #11
0
        private static string GetPropropertyNameForKeySelector <T>(Expression <Func <T, object> > keySelector, UniqueConstraintsTypeDictionary uniqueConstraintsTypeDictionary)
        {
            var body         = GetMemberExpression(keySelector, uniqueConstraintsTypeDictionary);
            var propertyName = body.Member.Name;

            return(propertyName);
        }