public void GettingSimpleDataFromNestedUnitsOfWorkShouldGetCachedData() { // Arrange using (var unitOfWork1 = GetFactory().Create()) { var repository1 = unitOfWork1.GetRepository <ICustomerRepository>(); var customer1 = CustomerGenerator.Generate(); repository1.Add(customer1); unitOfWork1.Commit(); var generatedId = customer1.Id; using (var unitOfWork2 = GetFactory().Create()) { var repository2 = unitOfWork2.GetRepository <ICustomerRepository>(); var customer2 = repository2.Get(generatedId); using (var unitOfWork3 = GetFactory().Create()) { var repository3 = unitOfWork3.GetRepository <ICustomerRepository>(); var customer3 = repository3.Get(generatedId); // Assert customer1.Should().BeSameAs(customer2); customer2.Should().BeSameAs(customer3); } } } }
static void Main() { // NOTE: Use of ElasticsearchJsonFormatter is optional (but recommended as it produces // 'idiomatic' json). If you don't want to take a dependency on // Serilog.Formatting.Elasticsearch package you can also use other json formatters // such as Serilog.Formatting.Json.JsonFormatter. // Console sink send logs to stdout which will then be read by logspout ILogger logger = new LoggerConfiguration() .Enrich.WithExceptionDetails() .WriteTo.Console(new ElasticsearchJsonFormatter()) .CreateLogger() .ForContext <Program>(); var customerGenerator = new CustomerGenerator(); var orderGenerator = new OrderGenerator(); var exceptionGenerator = new ExceptionGenerator(); while (true) { var customer = customerGenerator.Generate(); var order = orderGenerator.Generate(); logger.Information("{@customer} placed {@order}", customer, order); var exception = exceptionGenerator.Generate(); if (exception != null) { logger.Error(exception, "Problem with {@order} placed by {@customer}", order, customer); } Thread.Sleep(1000); } }
public void Commit_ShouldPersist_OnlyWhenParentTransactionalAndTopMostUnitOfWorkCommits() { // Arrange var customer = CustomerGenerator.Generate(); using (var uowOutter = GetFactory().Create(ScopeType.Transactional)) { using (var uowMiddle = GetFactory().Create(ScopeType.Transactional)) { using (var uowInner = GetFactory().Create()) { uowInner.GetRepository <ICustomerRepository>().Add(customer); uowInner.Commit(); // shows that transactionally changes are not yet saved (note: we are talking about // read committed transaction level which EF uses per default). CheckCustomerExistence(false, customer.Name); } uowMiddle.Commit(); // even though a transactional uow commits, it does not mean that changes should be saved // since there is another outer transactional uow CheckCustomerExistence(false, customer.Name); } // Act uowOutter.Commit(); } // Assert CheckCustomerExistence(true, customer.Name); }
static void Main() { Console.WriteLine("Starting application producing log events..."); var configuration = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("apiKey", "secret-api-key") }) .Build(); var logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.Http( requestUri: "http://*****:*****@customer} placed {@order}", customer, order); Thread.Sleep(1000); } }
static void Main() { // Use the following request URI if you're running this console // application in a Docker container var requestUri = "http://*****:*****@customer} placed {@order}", customer, order); Thread.Sleep(1000); } }
public void ComplexUsageScenario() { var firstCustomer = CustomerGenerator.Generate(); var secondCustomer = CustomerGenerator.Generate(); var thirdCustomer = CustomerGenerator.Generate(); // Arrange using (var uow = GetFactory().Create()) { uow.GetRepository <ICustomerRepository>().Add(firstCustomer); uow.Commit(); } // Act // our controller or some other top level service (maybe on application services layer) // opens a spanning context using (var spanningUnitOfWork = GetFactory().Create()) { var repository = spanningUnitOfWork.GetRepository <ICustomerRepository>(); Customer customer = repository.FindByName(firstCustomer.Name); // let's say this was a authorization check or something customer.Name.Should().Be(firstCustomer.Name); // now we want to call a business service which would have its own transactional unit of work in there using (var businessUnitOfWork = GetFactory().Create(ScopeType.Transactional)) { using (var firstDomainService = GetFactory().Create()) { firstDomainService.GetRepository <ICustomerRepository>() .Add(secondCustomer); firstDomainService.Commit(); } using (var secondDomainService = GetFactory().Create()) { secondDomainService.GetRepository <ICustomerRepository>() .Add(thirdCustomer); secondDomainService.Commit(); } // this should commit the transaction as its the top most transactional uow businessUnitOfWork.Commit(); } } // Assert using (var verificationUoW = GetFactory().Create()) { verificationUoW.GetRepository <ICustomerRepository>() .FindByName(secondCustomer.Name).Should().NotBeNull(); verificationUoW.GetRepository <ICustomerRepository>() .FindByName(thirdCustomer.Name).Should().NotBeNull(); } }
public void ShouldRollbackCorrectlyWhenExplodesDueToExceptions() { var shouldNotBeThereCustomer = CustomerGenerator.Generate(); using (IUnitOfWork uowOuter = GetFactory().Create(ScopeType.Transactional)) { uowOuter.GetRepository <ICustomerRepository>() .Add(shouldNotBeThereCustomer); using (IUnitOfWork uowInner = GetFactory().Create()) { try { uowInner.GetRepository <ICustomerRepository>() .Add(new Customer()); // well this should fail uowInner.Commit(); } catch (Exception) { // ignored completely. Although suppressing all // it is the only way to support all implementations // since they differ in exceptions thrown } } try { uowOuter.Commit(); } catch (Exception) { // ignored completely. } } using (IUnitOfWork uow = GetFactory().Create()) { uow.GetRepository <ICustomerRepository>() .FindByName(shouldNotBeThereCustomer.Name) .Should().BeNull(); } }
public void ParallelUnitOfWorkStacksShouldHaveNoIssues() { IList <Task> tasks = new List <Task>(); IList <Customer> customers = new List <Customer>(); using (var outterUow = GetFactory().Create()) { Action action = () => { var customer = CustomerGenerator.Generate(); using (var uow1 = GetFactory().Create(ScopeType.Transactional)) { using (var uow2 = GetFactory().Create()) { uow2.GetRepository <ICustomerRepository>().Add(customer); uow2.Commit(); } uow1.Commit(); } lock (customers) customers.Add(customer); }; for (int i = 0; i < 25; i++) { tasks.Add(Task.Factory.StartNew(action)); } var singleCustomer = CustomerGenerator.Generate(); outterUow.GetRepository <ICustomerRepository>().Add(singleCustomer); outterUow.Commit(); customers.Add(singleCustomer); } Task.WaitAll(tasks.ToArray()); customers.Should().HaveCount(26); }
public void MultipleRollBacksShouldBeFine() { // Arrange using (var uow = GetFactory().Create(ScopeType.Transactional)) { uow.GetRepository <ICustomerRepository>().Add(CustomerGenerator.Generate()); // Act uow.Dispose(); // Assert Action action = () => { uow.Dispose(); uow.Dispose(); }; action.Should().NotThrow(); } }
static void Main() { ILogger logger = new LoggerConfiguration() .WriteTo.DurableHttp( requestUri: "http://*****:*****@customer} placed {@order}", customer, order); Thread.Sleep(1000); } }
static void Main() { ILogger logger = new LoggerConfiguration() .WriteTo.DurableHttpUsingFileSizeRolledBuffers( requestUri: "http://*****:*****@customer} placed {@order}", customer, order); Thread.Sleep(1000); } }
static void Main() { ILogger logger = new LoggerConfiguration() .WriteTo.Udp( "localhost", 7071, AddressFamily.InterNetwork, new Log4jTextFormatter()) .WriteTo.Console() .CreateLogger(); var customerGenerator = new CustomerGenerator(); var orderGenerator = new OrderGenerator(); while (true) { var customer = customerGenerator.Generate(); var order = orderGenerator.Generate(); logger.Information("{@customer} placed {@order}", customer, order); Thread.Sleep(1000); } }
public void Dispose_ShouldRollbackEverything() { // Arrange var customer = CustomerGenerator.Generate(); using (GetFactory().Create(ScopeType.Transactional)) { using (var uowMiddle = GetFactory().Create(ScopeType.Transactional)) { using (var uowInner = GetFactory().Create()) { uowInner.GetRepository <ICustomerRepository>().Add(customer); uowInner.Commit(); } uowMiddle.Commit(); } // Act - no commit called means dispose will do its thing at the end } // Assert CheckCustomerExistence(false, customer.Name); }
public void UsingTransactionsWithMultipleUoWsWhenTransactionalUoWIsUsedForQueryingShouldWork() { ScopedUnitOfWorkConfiguration.ConfigureDiagnosticsMode(Console.WriteLine, true); using (var unitOfWork = GetFactory().Create(ScopeType.Transactional)) { var customer = unitOfWork.GetRepository <ICustomerRepository>().FindByName("name"); // it does not really matter if customer is null, this is just for a simple query if (customer == null) { for (int i = 0; i < 10; i++) { using (var uow = GetFactory().Create()) { uow.GetRepository <ICustomerRepository>().Add(CustomerGenerator.Generate()); uow.Commit(); } } } unitOfWork.Commit(); } }