public async Task <IActionResult> GetCustomer([FromRoute] int id, CancellationToken cancellationToken) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var query = new CustomersBy { Id = id }; var customers = await _queries.Execute(query, cancellationToken).ConfigureAwait(false); var customer = customers.FirstOrDefault(); if (customer == null) { return(NotFound()); } var contract = new CustomerDto { Id = customer.Id, FirstName = customer.FirstName, LastName = customer.LastName, EmailAddress = customer.EmailAddress }; return(Ok(contract)); }
public async Task Run() { Console.WriteLine("Queued command processor"); var tokenSource = new CancellationTokenSource(); var commandQueueTask = _commandProcessorQueue.StartProcessing(tokenSource.Token); var addCustomerCommand1 = new CreateCustomerCommand { FirstName = "Bob", LastName = "Jones", EmailAddress = "*****@*****.**", //Commit = false }; var addCustomerCommand2 = new CreateCustomerCommand { FirstName = "Herbert", LastName = "Scrackle", EmailAddress = "*****@*****.**", //Commit = false }; _commandProcessorQueue.EnqueueCommand(addCustomerCommand1, tokenSource.Token); _commandProcessorQueue.EnqueueCommand(addCustomerCommand2, tokenSource.Token); _commandProcessorQueue.FinaliseQueue(); await commandQueueTask.ConfigureAwait(false); var customersByQuery = new CustomersBy { FirstName = "Herbert" }; var results = await _processQueries.Execute(customersByQuery, tokenSource.Token).ConfigureAwait(false); var resultsList = results?.ToList(); Console.WriteLine("Records created: 2"); Console.WriteLine($"Querying after creation for Customer with First Name of 'Herbert' returns {resultsList?.Count ?? 0}"); if (resultsList != null && resultsList.Count > 0) { var json = JsonSerializer.Serialize(resultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } }
protected override void Load(ContainerBuilder builder) { builder.RegisterModule <ConfigurationModule <JsonResolver <Config> > >(); var principal = new ClaimsPrincipal(new GenericIdentity("xx4321")); // TODO: Sort out how to get the actual logged on ClaimsPrincipal for non Blazor application builder.Register(c => new PrincipalProvider(principal)).As <IPrincipalProvider>(); builder.Register(c => { // TODO: Do some logging in case required fields on the config aren't specified var config = c.Resolve <IDatabaseConfig>(); var optionsBuilder = new DbContextOptionsBuilder <EntityDbContext>(); optionsBuilder = config.InMemory.Enabled ? optionsBuilder.UseInMemoryDatabase(config.InMemory.Name) : optionsBuilder.UseSqlServer(config.ConnectionString); return(optionsBuilder.Options); }).InstancePerLifetimeScope(); builder.RegisterModule <EntityFrameworkModule>(); // Turn on the CQRS pipeline in the framework builder.RegisterModule <ServicesModule>(); builder.RegisterModule <ExampleServicesModule>(); builder.Register(c => { var keyInfo = c.Resolve <IAesConfig>(); var key = Encoding.ASCII.GetBytes(keyInfo.Key); var iv = Encoding.ASCII.GetBytes(keyInfo.Iv); return(new AesProvider(key, iv)); }).As <IEncryptionProvider>(); builder.Register(c => { var scope = c.Resolve <ILifetimeScope>(); return(new MapperConfiguration(mc => mc.AddProfiles( new List <Profile> { new CommandToEntityProfile(), new EntityToViewProfile() } )).CreateMapper()); }).As <IMapper>().InstancePerLifetimeScope(); // If this line isn't done, assemblies are loaded at launch. Using any symbol from the framework ensures they are. // https://dotnetcoretutorials.com/2020/07/03/getting-assemblies-is-harder-than-you-think-in-c/ var ignoreThisCommand = new CustomersBy { FirstName = "blah" }; }
public async Task Run() { var tokenSource = new CancellationTokenSource(); // Create 2 people at 1 address var addAddressCommand1 = new CreateAddressCommand { Street = "15 My Street", Suburb = "Springfield", State = "My State", Country = "Safe Country", PostalCode = "12345" }; await _processCommands.Execute(addAddressCommand1, tokenSource.Token).ConfigureAwait(false); var addCustomerCommand1 = new CreateCustomerCommand { FirstName = "Bob", LastName = "Jones", EmailAddress = "*****@*****.**", AddressId = addAddressCommand1.CreatedEntity.Id //Commit = false }; var addCustomerCommand2 = new CreateCustomerCommand { FirstName = "Herbert", LastName = "Scrackle", EmailAddress = "*****@*****.**", AddressId = addAddressCommand1.CreatedEntity.Id //Commit = false }; await _processCommands.Execute(addCustomerCommand1, tokenSource.Token).ConfigureAwait(false); await _processCommands.Execute(addCustomerCommand2, tokenSource.Token).ConfigureAwait(false); // Now create 1 person at a second address var addAddressCommand2 = new CreateAddressCommand { Street = "742 Evergreen Terrace", Suburb = "Springfield", State = "Unknown", Country = "USA", PostalCode = "Unknown" }; await _processCommands.Execute(addAddressCommand2, tokenSource.Token).ConfigureAwait(false); var addCustomerCommand3 = new CreateCustomerCommand { FirstName = "Homer", LastName = "Simpson", EmailAddress = "*****@*****.**", AddressId = addAddressCommand2.CreatedEntity.Id //Commit = false }; await _processCommands.Execute(addCustomerCommand3, tokenSource.Token).ConfigureAwait(false); Console.WriteLine("Records created: 2 addresses, 3 people"); // Now test getting data back { // Query using "Projector" pattern where the mapping is done on Sql Server rather than in this application. // This approach is more efficient as only the required data ever comes back to the application var customersByQuery = new CustomersBy { FirstName = "Bob" }; var results = await _processQueries.Execute(customersByQuery, tokenSource.Token).ConfigureAwait(false); var resultsList = results?.ToList(); Console.WriteLine($"Querying for Customer with First Name of '{customersByQuery.FirstName}' returns {resultsList?.Count ?? 0}"); if (resultsList != null && resultsList.Count > 0) { var json = JsonSerializer.Serialize(resultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } Console.WriteLine(); } { // Query using Include expressions. There may be scenarios where an include is required. EF Core poses restrictions // when using AsExpandable() from LinqKit, so this QueryHandler shows how to pass Include expressions to Query<TEntity>(...) var customersWithIncludeByQuery = new CustomersWithIncludeBy() { FirstName = "Bob" }; var resultsUsingInclude = await _processQueries.Execute(customersWithIncludeByQuery, tokenSource.Token).ConfigureAwait(false); var resultsList = resultsUsingInclude?.ToList(); Console.WriteLine($"Querying for Customer with First Name of '{customersWithIncludeByQuery.FirstName}' returns {resultsList?.Count ?? 0}"); if (resultsList != null && resultsList.Count > 0) { var json = JsonSerializer.Serialize(resultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } Console.WriteLine(); } { // Get a single address var addressesByQuery = new AddressesBy { Id = addAddressCommand1.CreatedEntity.Id }; var addressResults = await _processQueries.Execute(addressesByQuery, tokenSource.Token).ConfigureAwait(false); var addressResultsList = addressResults?.ToList(); Console.WriteLine($"Querying for Address with id {addAddressCommand1.CreatedEntity.Id} returns {addressResultsList?.Count ?? 0}"); if (addressResultsList != null && addressResultsList.Count > 0) { var json = JsonSerializer.Serialize(addressResultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result (1 address):"); Console.WriteLine(json); } Console.WriteLine(); } { // Get all address var addressesByQuery = new AddressesBy { }; var addressResults = await _processQueries.Execute(addressesByQuery, tokenSource.Token).ConfigureAwait(false); var addressResultsList = addressResults?.ToList(); Console.WriteLine($"Querying for all Addresses returns {addressResultsList?.Count ?? 0}"); if (addressResultsList != null && addressResultsList.Count > 0) { var json = JsonSerializer.Serialize(addressResultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result (multiple addresses):"); Console.WriteLine(json); } Console.WriteLine(); } // Create an Employer, and a CustomerEmployerMapping for one of the Customers above var addEmployerCommand = new CreateEmployerCommand { Name = "Springfield Power Plant" }; await _processCommands.Execute(addEmployerCommand, tokenSource.Token); var addCustomerEmployerMappingComand = new CreateCustomerEmployerMappingCommand { CustomerId = addCustomerCommand3.CreatedEntity.Id, EmployerId = addEmployerCommand.CreatedEntity.Id }; await _processCommands.Execute(addCustomerEmployerMappingComand, tokenSource.Token); { // And now show the full details of all CustomerEmployerMappings, includinging drilling down var getAllCEMs = new GetCustomerEmployerMappings(); var cemResults = await _processQueries.Execute(getAllCEMs, tokenSource.Token).ConfigureAwait(false); var cemResultsList = cemResults?.ToList(); Console.WriteLine($"Querying for all CustomerEmployerMappings returns {cemResultsList?.Count ?? 0}"); if (cemResultsList != null && cemResultsList.Count > 0) { var json = JsonSerializer.Serialize(cemResultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } Console.WriteLine(); } }
public async Task Run() { var tokenSource = new CancellationTokenSource(); var addCustomerCommand1 = new CreateCustomerCommand { FirstName = "Bob", LastName = "Jones", EmailAddress = "*****@*****.**", //Commit = false }; var addCustomerCommand2 = new CreateCustomerCommand { FirstName = "Herbert", LastName = "Scrackle", EmailAddress = "*****@*****.**", //Commit = false }; await _processCommands.Execute(addCustomerCommand1, tokenSource.Token).ConfigureAwait(false); await _processCommands.Execute(addCustomerCommand2, tokenSource.Token).ConfigureAwait(false); // Console.WriteLine("Querying All"); var customersByQueryAll = new CustomersBy(); var results = await _processQueries.Execute(customersByQueryAll, tokenSource.Token).ConfigureAwait(false); var resultsList = results?.ToList(); if (resultsList != null && resultsList.Count > 0) { var json = JsonSerializer.Serialize(resultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } // Console.WriteLine("Deleting Bob"); var deleteCommand = new DeleteCustomerCommand() { Id = addCustomerCommand1.CreatedEntity.Id }; await _processCommands.Execute(deleteCommand, tokenSource.Token).ConfigureAwait(false); // Console.WriteLine("Querying All Again"); results = await _processQueries.Execute(customersByQueryAll, tokenSource.Token).ConfigureAwait(false); resultsList = results?.ToList(); if (resultsList != null && resultsList.Count > 0) { var json = JsonSerializer.Serialize(resultsList, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine("Query result:"); Console.WriteLine(json); } }