示例#1
0
        public ProductManagerChangeSet CreateChangeSet(IEnumerable <Product> products, IEnumerable <ProductCategory> productCategories)
        {
            // retrieve changes sets (modified entities)
            var productChangeSet         = GetChangeSet(products);
            var productCategoryChangeSet = GetChangeSet(productCategories);

            // reduce entities (copy changed values)
            var productsMap          = ReduceToModifications(productChangeSet);
            var productCategoriesMap = ReduceToModifications(productCategoryChangeSet);

            // fixup relations (replaces related entities with reduced entites)
            FixupRelations(
                CastToEntityTuple(productsMap),
                CastToEntityTuple(productCategoriesMap)
                );

            var changeSet = new ProductManagerChangeSet();

            if (productsMap.Count > 0)
            {
                changeSet.Products = productsMap.Select(e => e.ReducedEntity).ToList();
            }
            if (productCategoriesMap.Count > 0)
            {
                changeSet.ProductCategories = productCategoriesMap.Select(e => e.ReducedEntity).ToList();
            }

            return(changeSet);
        }
        public ProductManagerResultSet SubmitChanges(ClientInfo clientInfo, ProductManagerChangeSet changeSet)
        {
            var resultSet = new ProductManagerResultSet(changeSet);

            using (var transactionScope = CreateSavingTransactionScope())
            {
                using (var dataRepository = _repositoryFactory(clientInfo))
                {
                    // optional custom processing
                    PreProcessing(clientInfo, ref changeSet, dataRepository);

                    // apply chnages to repository
                    ApplyChanges(dataRepository, dataRepository.Products, changeSet, changeSet.Products, clientInfo);
                    ApplyChanges(dataRepository, dataRepository.ProductCategories, changeSet, changeSet.ProductCategories, clientInfo);

                    // optional custom processing
                    BeforeSaving(clientInfo, ref changeSet, dataRepository);

                    // save changes
                    SaveChanges(dataRepository, changeSet, resultSet);

                    // optional custom processing
                    PostProcessing(clientInfo, ref resultSet, dataRepository);
                }
                transactionScope.Complete();
            }
            return(resultSet);
        }
 partial void BeforeSaving(ClientInfo clientInfo, ref ProductManagerChangeSet changeSet, IProductManagerRepository repository);
 partial void PreProcessing(ClientInfo clientInfo, ref ProductManagerChangeSet changeSet, IProductManagerRepository repository);