public void Should_Trigger_events_with_proxied_objects_in_multithread() { var completeCount = 0; var disposeCount = 0; Parallel.For(0, 100, i => { var uowManager = The <IUnitOfWorkManager>(); using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { The <IRepository <Product> >().Insert(new Product("Oguzhan")); The <IRepository <Category> >().Insert(new Category("Selam")); uowManager.Current.Completed += (sender, args) => { completeCount++; }; uowManager.Current.Disposed += (sender, args) => { var provider = The <Provider>(); provider.ShouldNotBeNull(); uowManager.Current.ShouldBe(null); disposeCount++; }; The <IRepository <Product> >().FirstOrDefault(x => x.Name == "Oguzhan").ShouldNotBeNull(); The <IRepository <Category> >().FirstOrDefault(x => x.Name == "Selam").ShouldNotBeNull(); uow.Complete(); The <IEventBus>().Publish(new SomeUowEvent()); } }); }
public async Task Should_Rollback_Transaction_On_Failure() { const string exceptionMessage = "This is a test exception!"; string blogName = Guid.NewGuid().ToString("N"); try { using (_uowManager.Begin()) { await _blogRepository.InsertAsync( new Blog(blogName, $"http://{blogName}.com/") ); throw new Exception(exceptionMessage); } } catch (Exception ex) when(ex.Message == exceptionMessage) { } using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { _blogRepository.FirstOrDefault(x => x.Name == blogName).ShouldBeNull(); uow.Complete(); } }
public void DapperAndEfCoreShouldWorkUderSameUnitOfWork() { Resolve <IEventBus>().Register <EntityCreatingEventData <Blog> >( eventData => { eventData.Entity.Name.ShouldBe("SameUow"); throw new Exception("Uow Rollback"); }); try { using (IUnitOfWorkCompleteHandle uow = Resolve <IUnitOfWorkManager>().Begin()) { int blogId = blogDapperRepository.InsertAndGetId(new Blog("SameUow", "www.oguzhansoykan.com")); Blog person = blogRepository.Get(blogId); person.ShouldNotBeNull(); uow.Complete(); } } catch (Exception exception) { //no handling. } blogDapperRepository.FirstOrDefault(x => x.Name == "SameUow").ShouldBeNull(); blogRepository.FirstOrDefault(x => x.Name == "SameUow").ShouldBeNull(); }
public void Should_Trigger_Completed_When_Uow_Succeed() { var completeCount = 0; var disposeCount = 0; using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin()) { _userRepository.Insert(new User { Name = "Oğuzhan", Email = "osoykan@gmail", Surname = "Soykan" }); _unitOfWorkManager.Current.Completed += (sender, args) => { completeCount++; }; _unitOfWorkManager.Current.Disposed += (sender, args) => { _unitOfWorkManager.Current.ShouldBe(null); completeCount.ShouldBe(1); disposeCount++; }; uow.Complete(); } UsingDbContext(context => context.Users.Any(p => p.Name == "Oğuzhan").ShouldBe(true)); completeCount.ShouldBe(1); disposeCount.ShouldBe(1); }
public void Should_Trigger_events_with_proxied_objects() { var completeCount = 0; var disposeCount = 0; Parallel.For(0, 500, i => { var uowManager = The <IUnitOfWorkManager>(); using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { The <IRepository <User> >().Insert(new User { Name = "Oğuzhan", Email = "osoykan@gmail", Surname = "Soykan" }); uowManager.Current.Completed += (sender, args) => { completeCount++; }; uowManager.Current.Disposed += (sender, args) => { var provider = The <Provider>(); provider.ShouldNotBeNull(); uowManager.Current.ShouldBe(null); disposeCount++; }; The <IRepository <User> >().FirstOrDefault(x => x.Name == "Oğuzhan").ShouldNotBeNull(); uow.Complete(); The <IEventBus>().Trigger(new SomeUowEvent()); } }); }
public void Should_Trigger_Domain_Events_For_Aggregate_Root() { var isTriggered = false; using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { _eventBus.Register <BlogUrlChangedEventData>(data => { data.Url.ShouldBe("http://testblog1-changed.myblogs.com"); isTriggered = true; }); //Act Blog blog1 = _blogRepository.Single(b => b.Name == "test-blog-1"); blog1.ChangeUrl("http://testblog1-changed.myblogs.com"); _blogRepository.Update(blog1); _blogDapperRepository.Get(blog1.Id).ShouldNotBeNull(); uow.Complete(); } //Arrange //Assert isTriggered.ShouldBeTrue(); }
public void when_an_event_publishing_behaviour_is_defined_multiple_times_then_behavior_allow_to_edit_header_values() { const string campaignName = "selam"; const string publishedBy = "From A Unit Test"; const bool isMultiple = true; string correlationId = Guid.NewGuid().ToString(); The <IEventBus>().RegisterPublishingBehaviour((@event, headers) => { headers["PublishedBy"] = publishedBy; }); The <IEventBus>().RegisterPublishingBehaviour((@event, headers) => { headers["IsMultiple"] = isMultiple; }); The <IEventBus>().Register <CampaignCreatedEvent>((@event, headers) => { @event.Name.ShouldBe(campaignName); headers[StoveConsts.Events.CausationId].ShouldBe(correlationId); headers["PublishedBy"].ShouldBe(publishedBy); headers["IsMultiple"].ShouldBe(isMultiple); }); using (The <IStoveCommandContextAccessor>().Use(correlationId)) { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <IRepository <Campaign> >().Insert(new Campaign(campaignName)); uow.Complete(); } } }
public void Should_Trigger_Domain_Events_For_Aggregate_Root() { var isTriggered = false; string correlationId = Guid.NewGuid().ToString(); using (The <IStoveCommandContextAccessor>().Use(correlationId)) { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { _eventBus.Register <BlogUrlChangedEvent>((@event, headers) => { @event.Url.ShouldBe("http://testblog1-changed.myblogs.com"); headers["CausationId"].ShouldBe(correlationId); isTriggered = true; }); //Act Blog blog1 = _blogRepository.Single(b => b.Name == "test-blog-1"); blog1.ChangeUrl("http://testblog1-changed.myblogs.com"); _blogRepository.Update(blog1); _blogDapperRepository.Get(blog1.Id).ShouldNotBeNull(); uow.Complete(); } } //Arrange //Assert isTriggered.ShouldBeTrue(); }
public async Task should_rolled_back_when_CancellationToken_is_requested_as_cancel() { var ts = new CancellationTokenSource(); The <IEventBus>().Register <EntityCreatingEventData <Blog> >(data => { ts.Cancel(true); }); try { using (IUnitOfWorkCompleteHandle uow = _uowManager.Begin()) { await _blogRepository.InsertAsync(new Blog("cancellationtoken", "cancellationtoken.com")); _blogRepository.FirstOrDefaultAsync(x => x.Name == "cancellationtoken").ShouldNotBeNull(); await uow.CompleteAsync(ts.Token); } } catch (Exception exception) { //Handle uow should be Rolled Back! } using (IUnitOfWorkCompleteHandle uow = _uowManager.Begin()) { Blog blog = await _blogRepository.FirstOrDefaultAsync(x => x.Name == "cancellationtoken"); blog.ShouldBeNull(); await uow.CompleteAsync(); } }
public void Dapper_and_EfCore_should_work_under_same_unitofwork_and_when_any_exception_appears_then_rollback_should_be_consistent_for_two_orm() { Resolve <IEventBus>().Register <EntityCreatingEventData <Blog> >( eventData => { eventData.Entity.Name.ShouldBe("Oguzhan_Same_Uow"); throw new Exception("Uow Rollback"); }); try { using (IUnitOfWorkCompleteHandle uow = Resolve <IUnitOfWorkManager>().Begin()) { int blogId = _blogDapperRepository.InsertAndGetId(new Blog("Oguzhan_Same_Uow", "www.oguzhansoykan.com")); Blog person = _blogRepository.Get(blogId); person.ShouldNotBeNull(); uow.Complete(); } } catch (Exception exception) { //no handling. } _blogDapperRepository.FirstOrDefault(x => x.Name == "Oguzhan_Same_Uow").ShouldBeNull(); _blogRepository.FirstOrDefault(x => x.Name == "Oguzhan_Same_Uow").ShouldBeNull(); }
public void Should_Copy_Filters_To_Nested_Uow() { using (IUnitOfWorkCompleteHandle outerUow = _unitOfWorkManager.Begin()) { _unitOfWorkManager.Current.EnableFilter(StoveDataFilters.SoftDelete); _unitOfWorkManager.Current.IsFilterEnabled(StoveDataFilters.SoftDelete).ShouldBe(true); using (_unitOfWorkManager.Current.DisableFilter(StoveDataFilters.SoftDelete)) { using (IUnitOfWorkCompleteHandle nestedUow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew)) { //Because nested transaction copies outer uow's filters. _unitOfWorkManager.Current.IsFilterEnabled(StoveDataFilters.SoftDelete).ShouldBe(false); nestedUow.Complete(); } _unitOfWorkManager.Current.IsFilterEnabled(StoveDataFilters.SoftDelete).ShouldBe(false); } _unitOfWorkManager.Current.IsFilterEnabled(StoveDataFilters.SoftDelete).ShouldBe(true); outerUow.Complete(); } _unitOfWorkManager.Current.ShouldBeNull(); }
public void Update_should_work_with_object_tracking_mechanism() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- string productName = Guid.NewGuid().ToString("N"); var product = new Product(productName); string updateName = Guid.NewGuid().ToString("N"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- The <IRepository <Product, string> >().Insert(product); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- Product item = The <IRepository <Product, string> >().FirstOrDefault(x => x.Name == productName); item.Name = updateName; The <IUnitOfWorkManager>().Current.SaveChanges(); Product pant = The <IRepository <Product, string> >().FirstOrDefault(x => x.Name == item.Name); pant.ShouldNotBeNull(); pant.Name.ShouldBe(updateName); uow.Complete(); } }
//[Fact] public async Task uow_completed_async_event_should_fire_when_uow_is_completed() { var executionCount = 0; The <IEventBus>().Register <UserCretedEventAfterUowCompleted>(async completed => { executionCount++; await Task.FromResult(0); }); var uowManager = The <IUnitOfWorkManager>(); var userRepository = The <IRepository <User> >(); using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { await userRepository.InsertAsync(new User { Email = "*****@*****.**", Surname = "Sykn", Name = "Oğuz" }); await The <IUnitOfWorkCompletedEventHelper>().TriggerAsync(new UserCretedEventAfterUowCompleted() { Name = "Oğuz" }); await uow.CompleteAsync(); } executionCount.ShouldBe(1); }
public void uow_completed_event_should_fire_when_uow_is_completed() { var executionCount = 0; The <IEventBus>().Register <UserCretedEventAfterUowCompleted>(completed => { executionCount++; }); var uowManager = The <IUnitOfWorkManager>(); var userRepository = The <IRepository <User> >(); using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { userRepository.Insert(new User { Email = "*****@*****.**", Surname = "Sykn", Name = "Oğuz" }); The <IUnitOfWorkCompletedEventHelper>().Publish(new UserCretedEventAfterUowCompleted { Name = "Oğuz" }); uow.Complete(); } executionCount.ShouldBe(1); }
public async Task Should_automatically_save_changes_on_uow_completed_with_dapper() { int blog1Id; //Act using (IUnitOfWorkCompleteHandle uow = _uowManager.Begin()) { Blog blog1 = await _blogDapperRepository.SingleAsync(b => b.Name == "test-blog-1"); blog1Id = blog1.Id; blog1.Name = "test-blog-1-updated"; _blogDapperRepository.Update(blog1); await uow.CompleteAsync(); } //Assert await UsingDbContextAsync(async context => { Blog blog1 = await context.Blogs.SingleAsync(b => b.Id == blog1Id); blog1.Name.ShouldBe("test-blog-1-updated"); }); }
public void Dapper_and_NHibernate_should_work_under_same_unitofwork_and_when_any_exception_appears_then_rollback_should__be_consistent_for_two_orm() { Resolve <IEventBus>().Register <EntityCreatingEventData <Person> >( eventData => { eventData.Entity.Name.ShouldBe("Oguzhan_Same_Uow"); throw new ApplicationException("Uow Rollback"); }); try { using (IUnitOfWorkCompleteHandle uow = Resolve <IUnitOfWorkManager>().Begin()) { string personId = _personDapperRepository.InsertAndGetId(new Person("Oguzhan_Same_Uow")); Person person = _personRepository.Get(personId); person.ShouldNotBeNull(); uow.Complete(); } } catch { //no handling. } _personDapperRepository.FirstOrDefault(x => x.Name == "Oguzhan_Same_Uow").ShouldBeNull(); _personRepository.FirstOrDefault(x => x.Name == "Oguzhan_Same_Uow").ShouldBeNull(); }
public void Delete_should_work_soft_deletable_entities() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- string productName = Guid.NewGuid().ToString("N"); string address = Guid.NewGuid().ToString("N"); var product = new Product(productName); var order = new Order(address, product); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Product insertedProduct = The <IRepository <Product> >().Insert(product); Order insertedOrder = The <IRepository <Order> >().Insert(order); The <IRepository <Order> >().Delete(insertedOrder); The <IUnitOfWorkManager>().Current.SaveChanges(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- Order deletedOrder = The <IRepository <Order> >().FirstOrDefault(x => x.Product == insertedProduct); insertedOrder.IsDeleted.ShouldBe(true); deletedOrder.ShouldBeNull(); uow.Complete(); } }
public void uow_complete_handle_eventbus_should_work_with_repository_insert2() { var uowManager = The <IUnitOfWorkManager>(); var userRepository = The <IRepository <User> >(); using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { for (var i = 0; i < 1000; i++) { userRepository.Insert(new User { Email = "*****@*****.**", Surname = "Sykn", Name = "Oğuz" }); } uow.Complete(); } using (IUnitOfWorkCompleteHandle uow = uowManager.Begin()) { userRepository.GetAll().ForEach(user => user.Surname = "Soykan"); userRepository.Count(x => x.Email == "*****@*****.**").ShouldBe(1000); userRepository.FirstOrDefault(x => x.Email == "*****@*****.**").ShouldNotBeNull(); uow.Complete(); } }
public void Delete_should_work_on_hard_deletable_entities() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- string productName = Guid.NewGuid().ToString("N"); var product = new Product(productName); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- Product inserted = The <IRepository <Product> >().Insert(product); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- The <IRepository <Product> >().Delete(inserted); Product deleted = The <IRepository <Product> >().FirstOrDefault(x => x.Name == productName); deleted.ShouldBeNull(); uow.Complete(); } }
public async Task Should_Automatically_Save_Changes_On_Uow() { int blog1Id; //Act using (IUnitOfWorkCompleteHandle uow = _uowManager.Begin()) { Blog blog1 = await _blogRepository.SingleAsync(b => b.Name == "test-blog-1"); blog1Id = blog1.Id; blog1.Name = "test-blog-1-updated"; await uow.CompleteAsync(); } //Assert await UsingDbContextAsync(async context => { Blog blog1 = await context.Blogs.SingleAsync(b => b.Id == blog1Id); blog1.Name.ShouldBe("test-blog-1-updated"); }); }
public virtual void CreateOrMigrate(Action <string> logger) { var args = new ConnectionStringResolveArgs { ["DbContextType"] = typeof(TDbContext), ["DbContextConcreteType"] = typeof(TDbContext) }; string nameOrConnectionString = ConnectionStringHelper.GetConnectionString( _connectionStringResolver.GetNameOrConnectionString(args) ); logger($"Name or ConnectionString: {nameOrConnectionString}\nCurrent DbContext: {typeof(TDbContext).GetTypeInfo().Name}"); using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin(new UnitOfWorkOptions { Timeout = TimeSpan.FromSeconds(_configuration.TransactionTimeout) }) ) { _migrationStrategies.ForEach(strategy => { logger("--------------------------------------------------------"); strategy.Migrate <TDbContext, TConfiguration>(nameOrConnectionString, MigrationAssembly, logger); }); _unitOfWorkManager.Current.SaveChanges(); uow.Complete(); } }
public void AggregateRoot_repository_with_TPrimaryKey() { Guid aggregateId = Guid.NewGuid(); const string bundleName = "SampleBundle"; string eventLog = string.Empty; string headerLog = string.Empty; The <IEventBus>().RegisterPublishingBehaviour((@event, headers) => { eventLog = @event.ToJsonString(true, true); headerLog = headers.ToString(); }); The <IEventBus>().Register <ProductBundleCreated>((@event, headers) => { @event.Name.ShouldBe(bundleName); @event.Id.ShouldBe(aggregateId); headers.GetAggregateId().ShouldBe(aggregateId.ToString()); }); using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <IRepository <ProductBundle, Guid> >().Insert(ProductBundle.Create(aggregateId, bundleName)); uow.Complete(); } eventLog.ShouldNotBeEmpty(); headerLog.ShouldNotBeEmpty(); }
public override void Execute(SimpleBackgroundJobArgs args) { using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { string message = _personRepository.FirstOrDefault(person => person.Name == args.Message).Name; uow.Complete(); } }
public void AggreateRoot_event_should_raise_when_Added() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <IRepository <Campaign> >().Insert(new Campaign("selam")); uow.Complete(); } }
public void Should_Get_DbContext_2() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <IRepository <Blog> >().GetDbContext().ShouldBeOfType <BloggingDbContext>(); uow.Complete(); } }
public void Should_Get_DbContext_From_Second_DbContext_With_Custom_Repository() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <ISupportRepository <Ticket> >().GetDbContext().ShouldBeOfType <SupportDbContext>(); uow.Complete(); } }
public void DoSomeStuff() { using (Session.Use(266)) { using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { Product product = _productRepository.Insert(new Product("TShirt")); product.Name = "Kazak"; int count = _productRepository.Count(); uow.Complete(); } Product updatedProduct; using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { updatedProduct = _productRepository.FirstOrDefault(x => x.Name == "Kazak"); uow.Complete(); } updatedProduct.Name = "SecondKazak"; using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { _productRepository.Update(updatedProduct); uow.Complete(); } using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { _productRepository.Delete(updatedProduct); uow.Complete(); } using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { _productRepository.Insert(new Product("ThirdKazak")); _productRepository.Insert(new Product("ForthKazak")); uow.Complete(); } using (IUnitOfWorkCompleteHandle uow = UnitOfWorkManager.Begin()) { Product product = _productRepository.FirstOrDefault(x => x.Name == "SecondKazak"); List <Product> products = _productRepository.GetAllList(x => x.Name == "ThirdKazak" || x.Name == "ForthKazak" || x.Name == "SecondKazak"); uow.Complete(); } } }
/// <summary> /// OnActionExecuting /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) { var unitOfWorkAttribute = context.TypeOfAttributeEntity <UnitOfWorkAttribute>(); if (unitOfWorkAttribute != null && unitOfWorkAttribute.IsDisabled) { return; } UnitOfWorkCompleteHandle = _unitOfWorkManager.Begin(); }
//[Fact(Skip = "Skipped, since Effort.DbConnection does not provide Sql Text while interception time.")] public void Should_Nolocking_Work() { using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin()) { Person person = _personRepository.Nolocking(persons => persons.FirstOrDefault(x => x.Name == "halil")); person.ShouldNotBeNull(); uow.Complete(); } }
public void Dapper_And_NH_should_be_able_to_work_under_same_tranaction() { using (IUnitOfWorkCompleteHandle uow = The <IUnitOfWorkManager>().Begin()) { The <IDapperRepository <Product> >().GetAll().Count().ShouldBe(1); The <IRepository <Product> >().GetAll().Count().ShouldBe(1); uow.Complete(); } }
private static void SetCurrentUow(HttpContextBase httpContext, IUnitOfWorkCompleteHandle uow) { httpContext.Items[UowHttpContextKey] = uow; }