예제 #1
0
        /// <summary>
        /// More compex scenario
        /// </summary>
        /// <param name="id"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public MyEntity Update(long id, Action <MyEntity> update)
        {
            var trans   = _dataAccess.CreateTransaction();
            var context = new MyEntityUpdateContext(id, update, _myEntityLoader, trans);
            var op      = new MyEntityUpdateOperation(context);

            op.Load();
            op.StageChanges();

            trans.Commit();
            //if we handle the not found case, we can assert that context.Entity will have a value here.
            return(context.Entity !);
        }
        public void MyEntity_Update_Test()
        {
            var id     = 1;
            var trans  = new TestDataTransaction();
            var loader = new Mock <IMyEntityLoader>();

            loader.Setup(x => x.Get(id)).Returns(new MyEntity
            {
                Name = "Original Name"
            });

            var ctx = new MyEntityUpdateContext(id, e =>
            {
                e.Name = "New Name";
            }, loader.Object, trans);

            var op = new MyEntityUpdateOperation(ctx);

            op.Load();
            op.StageChanges();

            trans.UpdatedEntityOfType <MyEntity>().Count().Should().Be(1);
            ctx.Entity !.Name.Should().Be("New Name");
        }