コード例 #1
0
        public async Task <EmployeeViewModel> Post([FromBody] EmployeeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new ArgumentException("Error in parameters");
            }

            var result = new EmployeeViewModel();

            //NOTE: Use transactions where there are multiple I/O operations, this time is used
            // for demo purpose
            //NOTE 2: Always start it in the controllers to prevent nested transactions
            using (var trans = _DataRepositoryFactory.GetUnitOfWork().CreateTransaction())
            {
                var repo = _DataRepositoryFactory.GetDataRepository <Employee>();

                //This whas done this way for teaching purpose
                var dto    = _Mapper.Map <EmployeeDTO>(model);
                var entity = _Mapper.Map <Employee>(dto);

                entity = await repo.AddAsync(entity);

                result = _Mapper.Map <EmployeeViewModel>(_Mapper.Map <EmployeeDTO>(entity));

                await trans.CommitAsync();
            }

            return(result);
        }
コード例 #2
0
        public void GetUnitOfWork()
        {
            var unitOfWork = _DataRepositoryFactory.GetUnitOfWork();

            using (var transaction = unitOfWork.CreateTransaction())
            {
                transaction.Commit();
            }

            Assert.IsNull(null);
        }
コード例 #3
0
        public void GetUnitOfWork()
        {
            var unitOfWork = _DataRepositoryFactory.GetUnitOfWork();

            using (var transaction = unitOfWork.CreateTransaction())
            {
                //Call whatever engine or ropository here and perform the operations

                transaction.Commit();
            }

            Assert.IsNull(null);
        }