Пример #1
0
        private void ValidateUniqueProperties(ISagaEntity saga)
        {
            var uniqueProperties = UniqueAttribute.GetUniqueProperties(saga.GetType());

            if (!uniqueProperties.Any())
            {
                return;
            }

            var sagasFromSameType = from s in data
                                    where
                                    ((s.Value as ISagaEntity).GetType() == saga.GetType() && (s.Key != saga.Id))
                                    select s.Value;

            foreach (var storedSaga in sagasFromSameType)
            {
                foreach (var uniqueProperty in uniqueProperties)
                {
                    if (uniqueProperty.CanRead)
                    {
                        var inComingSagaPropertyValue = uniqueProperty.GetValue(saga, null);
                        var storedSagaPropertyValue   = uniqueProperty.GetValue(storedSaga, null);
                        if (inComingSagaPropertyValue.Equals(storedSagaPropertyValue))
                        {
                            throw new
                                  InvalidOperationException(
                                      string.Format("Cannot store a saga. The saga with id '{0}' already has property '{1}' with value '{2}'.", storedSaga.Id, uniqueProperty.ToString(), storedSagaPropertyValue));
                        }
                    }
                }
            }
        }
Пример #2
0
        public void Ensure_property_is_returned_when_attribute_exists()
        {
            var uniqueProperties = UniqueAttribute.GetUniqueProperties(typeof(ModelWithUniqueProperty))
                                   .ToList();

            Assert.AreEqual(1, uniqueProperties.Count);
            Assert.AreEqual("PropertyWithAttribute", uniqueProperties.First().Name);
        }
Пример #3
0
        public void EnsureOverridePropertyIsReturnedWhenAttributeExists()
        {
            var uniqueProperties = UniqueAttribute.GetUniqueProperties(typeof(InheritedModelWithOverride))
                                   .ToList();

            Assert.AreEqual(1, uniqueProperties.Count);
            Assert.AreEqual("PropertyWithAttribute", uniqueProperties.First().Name);
        }
Пример #4
0
        public void Save(ISagaEntity saga)
        {
            Action saveSagaAction = () =>
            {
                var stringId           = saga.Id.ToString("N");
                var sagaKey            = GetSagaKey(saga);
                var sagaPropertyMapKey = GetSagaPropertyMapKey(saga);
                var sagaVersionKey     = GetSagaVersionKey(saga);
                var uniqueProperties   = UniqueAttribute.GetUniqueProperties(saga);

                using (var client = GetClient())
                {
                    using (var rlock = client.AcquireLock(stringId, TimeSpan.FromSeconds(30)))
                    {
                        long version = client.Increment(sagaVersionKey, 1);

                        SetSagaVersion(saga, version);

                        var sagaString = Serialize(saga);

                        //Check that unique properties don't already exist for a different saga
                        foreach (var prop in uniqueProperties)
                        {
                            var propertyKey = GetPropertyKey(saga.GetType(), prop.Key, prop.Value);
                            var sagaId      = client.Get <string>(propertyKey);
                            if (sagaId != null)
                            {
                                if (saga.Id != Guid.Parse(sagaId))
                                {
                                    throw new UniquePropertyException("Unique property " + prop.Key + " already exists with value " + prop.Value);
                                }
                            }
                        }

                        using (var tran = client.CreateTransaction())
                        {
                            tran.QueueCommand(c => c.Set(sagaKey, sagaString));
                            foreach (var prop in uniqueProperties)
                            {
                                var propertyKey = GetPropertyKey(saga.GetType(), prop.Key, prop.Value);
                                tran.QueueCommand(c => c.Lists[sagaPropertyMapKey].Add(propertyKey));                       //Link from saga ID to property key
                                tran.QueueCommand(c => client.Set(propertyKey, stringId));                                  //Link from property key to saga
                            }
                            tran.Commit();
                        }
                    }
                }
            };

            if (Transaction.Current != null)
            {
                Transaction.Current.EnlistVolatile(new ActionResourceManager(saveSagaAction, null), EnlistmentOptions.None);
            }
            else
            {
                saveSagaAction();
            }
        }
Пример #5
0
        public void Ensure_multiple_properties_are_returned_when_multiple_attributes_exists()
        {
            var uniqueProperties = UniqueAttribute.GetUniqueProperties(typeof(ModelWithMultipleUniqueProperty))
                                   .ToList();

            Assert.AreEqual(2, uniqueProperties.Count);
            Assert.AreEqual("PropertyWithAttribute1", uniqueProperties.First().Name);
            Assert.AreEqual("PropertyWithAttribute2", uniqueProperties.Skip(1).First().Name);
        }
Пример #6
0
        bool IsUniqueProperty <T>(string property)
        {
            var  key = typeof(T).FullName + property;
            bool value;

            if (!PropertyCache.TryGetValue(key, out value))
            {
                value = UniqueAttribute.GetUniqueProperties(typeof(T)).Any(p => p.Name == property);
                PropertyCache[key] = value;
            }

            return(value);
        }
Пример #7
0
 static IEnumerable <CorrelationProperty> FindUniqueAttributes(Type sagaEntityType)
 {
     return(UniqueAttribute.GetUniqueProperties(sagaEntityType).Select(pt => new CorrelationProperty {
         Name = pt.Name
     }));
 }
Пример #8
0
 protected bool IsUniqueProperty <T>(string property)
 {
     return(UniqueAttribute.GetUniqueProperties(typeof(T)).FirstOrDefault(p => p.Name == property) != null);
 }
Пример #9
0
        public void Ensure_property_is_returned_when_no_attribute_exists()
        {
            var uniqueProperties = UniqueAttribute.GetUniqueProperties(typeof(ModelWithNoUniqueProperty));

            Assert.IsEmpty(uniqueProperties);
        }