Пример #1
0
        /// <summary>
        /// Saves the saga entity to the persistence store.
        /// </summary>
        /// <param name="saga">The saga entity to save.</param>
        public void Save(IContainSagaData saga)
        {
            var sagaTypeName = saga.GetType().Name;

            if (!(saga is IHaveDocumentVersion))
            {
                throw new InvalidOperationException(
                          string.Format("Saga type {0} does not implement IHaveDocumentVersion", sagaTypeName));
            }

            var uniqueProperty = UniqueAttribute.GetUniqueProperty(saga);

            if (uniqueProperty.HasValue)
            {
                this.EnsureUniqueIndex(saga, uniqueProperty.Value);
                CheckUniqueProperty(saga, uniqueProperty.Value);
            }

            var collection = this.mongoDatabase.GetCollection(sagaTypeName);
            var result     = collection.Insert(saga);

            if (result.HasLastErrorMessage)
            {
                throw new InvalidOperationException(string.Format("Unable to save with id {0}", saga.Id));
            }
        }
Пример #2
0
        public void Update(IContainSagaData saga)
        {
            var p = UniqueAttribute.GetUniqueProperty(saga);

            if (!p.HasValue)
            {
                return;
            }

            var uniqueProperty = p.Value;

            var metadata = Session.Advanced.GetMetadataFor(saga);

            //if the user just added the unique property to a saga with existing data we need to set it
            if (!metadata.ContainsKey(UniqueValueMetadataKey))
            {
                StoreUniqueProperty(saga);
                return;
            }

            var storedValue = metadata[UniqueValueMetadataKey].ToString();

            var currentValue = uniqueProperty.Value.ToString();

            if (currentValue == storedValue)
            {
                return;
            }

            DeleteUniqueProperty(saga, new KeyValuePair <string, object>(uniqueProperty.Key, storedValue));
            StoreUniqueProperty(saga);
        }
Пример #3
0
        private bool IsUniqueProperty <T>(string property)
        {
            //TODO Cache?
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(typeof(T));

            if (uniqueProperty == null)
            {
                return(false);
            }
            return(uniqueProperty.Name == property);
        }
Пример #4
0
        public void Complete(IContainSagaData saga)
        {
            Session.Delete(saga);

            var uniqueProperty = UniqueAttribute.GetUniqueProperty(saga);

            if (!uniqueProperty.HasValue)
            {
                return;
            }

            DeleteUniqueProperty(saga, uniqueProperty.Value);
        }
Пример #5
0
        private void EnsureUniqueIndex(IContainSagaData saga)
        {
            var sagaDataType   = saga.GetType();
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(sagaDataType);

            if (uniqueProperty == null)
            {
                return;
            }

            var classmap        = BsonClassMap.LookupClassMap(sagaDataType);
            var uniqueFieldName = GetFieldName(classmap, uniqueProperty.Name);

            _repo.EnsureUniqueIndex(sagaDataType, uniqueFieldName);
        }
Пример #6
0
        void StoreUniqueProperty(ISagaEntity saga)
        {
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(saga);

            if (!uniqueProperty.HasValue)
            {
                return;
            }

            var id = SagaUniqueIdentity.FormatId(saga.GetType(), uniqueProperty.Value);

            Session.Store(new SagaUniqueIdentity {
                Id = id, SagaId = saga.Id, UniqueValue = uniqueProperty.Value.Value
            });

            SetUniqueValueMetadata(saga, uniqueProperty.Value);
        }
Пример #7
0
        void StoreUniqueProperty(IContainSagaData saga)
        {
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(saga);

            if (!uniqueProperty.HasValue)
            {
                return;
            }

            var id        = SagaUniqueIdentity.FormatId(saga.GetType(), uniqueProperty.Value);
            var sagaDocId = sessionFactory.Store.Conventions.FindFullDocumentKeyFromNonStringIdentifier(saga.Id, saga.GetType(), false);

            Session.Store(new SagaUniqueIdentity
            {
                Id          = id,
                SagaId      = saga.Id,
                UniqueValue = uniqueProperty.Value.Value,
                SagaDocId   = sagaDocId
            });

            SetUniqueValueMetadata(saga, uniqueProperty.Value);
        }
Пример #8
0
        private void UpdateUniquePropertyForSaga(ISagaEntity saga, SagaData sagaData)
        {
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(saga);

            sagaData.UniqueProperty = uniqueProperty != null?GetUniqueProperty(saga.GetType(), uniqueProperty.Value) : Guid.NewGuid().ToString();
        }
Пример #9
0
        public void Ensure_null_returned_when_no_attributes_exists()
        {
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(typeof(ModelWithNoUniqueProperty));

            Assert.IsNull(uniqueProperty);
        }
Пример #10
0
        public void Ensure_single_property_is_returned_when_attribute_exists()
        {
            var uniqueProperty = UniqueAttribute.GetUniqueProperty(typeof(ModelWithUniqueProperty));

            Assert.IsNotNull(uniqueProperty);
        }
Пример #11
0
 public void Ensure_exception_is_thrown_when_multiple_attributes_exists()
 {
     Assert.Throws <InvalidOperationException>(() => UniqueAttribute.GetUniqueProperty(typeof(ModelWithMultipleUniqueProperty)));
 }
Пример #12
0
        public static void Main(string[] args)
        {
            string directory;
            string op;

            if (TryFetchWithInfo(args, Keys.Directory, out directory) == false ||
                TryFetchWithInfo(args, Keys.Operation, out op) == false)
            {
                return;
            }

            var sagaTypes = FindAllSagaTypes();

            if (sagaTypes.Length == 0)
            {
                Console.WriteLine("No Saga types found! Have you put all the dlls with sagas in the deduplicator directory?");
                return;
            }

            var sagaToProperty     = new Dictionary <string, string>();
            var sagasWithoutUnique = new List <string>();

            foreach (var sagaType in sagaTypes)
            {
                var uniqueProperty = UniqueAttribute.GetUniqueProperty(sagaType);
                if (uniqueProperty == null)
                {
                    sagasWithoutUnique.Add(sagaType.Name);
                }
                else
                {
                    sagaToProperty.Add(sagaType.Name, uniqueProperty.Name);
                }
            }

            PrintReport(sagaToProperty, sagasWithoutUnique);

            var settings = ConfigurationManager.ConnectionStrings[ConnectionStringName];

            string connectionString;

            if (!string.IsNullOrWhiteSpace(settings?.ConnectionString))
            {
                connectionString = settings.ConnectionString;
            }
            else
            {
                // try to parse the last param
                CloudStorageAccount account;
                var possibleConnectionString = args.Last();
                if (CloudStorageAccount.TryParse(possibleConnectionString, out account) == false)
                {
                    Console.WriteLine("Provide one connection string in the standard 'connectionStrings' App.config section with following name: '{0}'", ConnectionStringName);
                    return;
                }

                connectionString = possibleConnectionString;
            }

            var operation = (OperationType)Enum.Parse(typeof(OperationType), op, true);

            var program = new Program(connectionString, directory);

            switch (operation)
            {
            case OperationType.Download:
                foreach (var kvp in sagaToProperty)
                {
                    program.DownloadConflictingSagas(kvp.Key, kvp.Value);
                }
                return;

            case OperationType.Upload:
                foreach (var kvp in sagaToProperty)
                {
                    program.UploadResolvedConflicts(kvp.Key);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }