Skip to content

Commit

Permalink
Merge pull request #15 from Amitpnk/feature/CQRS
Browse files Browse the repository at this point in the history
Feature/cqrs
  • Loading branch information
Amitpnk authored Jul 6, 2020
2 parents e0ef262 + 1e42021 commit 75524e5
Show file tree
Hide file tree
Showing 29 changed files with 288 additions and 236 deletions.
2 changes: 1 addition & 1 deletion OnionArchitecture/.template.config/template.vstemplate
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Name>Onion Architecture</Name>
<Description>WhiteApp or QuickApp API solution template which is built on Onion Architecture using .NET Core</Description>
<TemplateID>OnionArchitecture.CSharp</TemplateID>
<DefaultName>OA</DefaultName>
<DefaultName>OnionArchitecture</DefaultName>

<Icon>project-icon.png</Icon>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace OA.Data
{
public class ApplicationContext : DbContext, IApplicationContext
public class ApplicationDbContext : DbContext, IApplicationDbContext
{
// This constructor is used of runit testing
public ApplicationContext()
public ApplicationDbContext()
{

}
public ApplicationContext(DbContextOptions options) : base(options)
public ApplicationDbContext(DbContextOptions options) : base(options)
{
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OA.Data
{
public interface IApplicationContext
public interface IApplicationDbContext
{
DbSet<Category> Categories { get; set; }
DbSet<Customer> Customers { get; set; }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OA.Data.Migrations
{
[DbContext(typeof(ApplicationContext))]
[DbContext(typeof(ApplicationDbContext))]
partial class CustomerContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using OA.Infrastructure.Middleware;
using Serilog;

namespace OA.Infrastructure.Extension
{
Expand All @@ -21,5 +23,10 @@ public static void ConfigureSwagger(this IApplicationBuilder app)
});
}

public static void ConfigureSwagger(this ILoggerFactory loggerFactory)
{
loggerFactory.AddSerilog();
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -12,7 +13,6 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.NewtonsoftJson;

namespace OA.Infrastructure.Extension
{
Expand All @@ -21,7 +21,7 @@ public static class ConfigureServiceContainer
public static void AddDbContext(this IServiceCollection serviceCollection,
IConfiguration configuration, IConfigurationRoot configRoot)
{
serviceCollection.AddDbContext<ApplicationContext>(options =>
serviceCollection.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("OnionArchConn") ?? configRoot["ConnectionStrings:OnionArchConn"])
);
}
Expand All @@ -36,15 +36,15 @@ public static void AddAutoMapper(this IServiceCollection serviceCollection)
serviceCollection.AddSingleton(mapper);
}

public static void AddRepository(this IServiceCollection serviceCollection)
public static void AddAddScopedServices(this IServiceCollection serviceCollection)
{
serviceCollection.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
serviceCollection.AddScoped<ICustomerRepository, CustomerRepository>();
serviceCollection.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
}

public static void AddTransientServices(this IServiceCollection serviceCollection)
{
serviceCollection.AddTransient<ICustomerService, CustomerService>();
serviceCollection.AddTransient<IMailService, MailService>();
}

Expand Down Expand Up @@ -90,5 +90,11 @@ public static void AddController(this IServiceCollection serviceCollection)
serviceCollection.AddControllers().AddNewtonsoftJson();
}

//public static void AddMediatorCQRS(this IServiceCollection services)
//{
// //var assembly = AppDomain.CurrentDomain.Load("OA.Service");
// services.AddMediatR(Assembly.GetExecutingAssembly());
//}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace OA.Persistence.Repository
{
public class CustomerRepository : ICustomerRepository
{
private readonly ApplicationContext _context;
public CustomerRepository(ApplicationContext context)
private readonly ApplicationDbContext _context;
public CustomerRepository(ApplicationDbContext context)
{
_context = context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace OA.Persistence.Repository
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly DbSet<T> entities;
private readonly ApplicationContext _context;
private readonly ApplicationDbContext _context;

public GenericRepository(ApplicationContext context)
public GenericRepository(ApplicationDbContext context)
{
_context = context;
entities = _context.Set<T>();
Expand Down
11 changes: 0 additions & 11 deletions OnionArchitecture/OA.Service/Contract/ICustomerService.cs

This file was deleted.

23 changes: 23 additions & 0 deletions OnionArchitecture/OA.Service/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OA.Service
{
public static class DependencyInjection
{
public static void AddMediatorCQRS(this IServiceCollection services)
{
// or you can use assembly in Extension method in Infra layer with below command
// var assembly = AppDomain.CurrentDomain.Load("OA.Service");
services.AddMediatR(Assembly.GetExecutingAssembly());
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using MediatR;
using OA.Data;
using OA.Domain.Entities;
using System.Threading;
using System.Threading.Tasks;

namespace OA.Service.Features.CustomerFeatures.Commands
{
public class CreateCustomerCommand : IRequest<int>
{
public string CustomerName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public class CreateCustomerCommandHandler : IRequestHandler<CreateCustomerCommand, int>
{
private readonly IApplicationDbContext _context;
public CreateCustomerCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<int> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
{
var customer = new Customer();
customer.CustomerName = request.CustomerName;
customer.ContactName = request.ContactName;

_context.Customers.Add(customer);
await _context.SaveChangesAsync();
return customer.Id;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using OA.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace OA.Service.Features.CustomerFeatures.Commands
{
public class DeleteCustomerByIdCommand : IRequest<int>
{
public int Id { get; set; }
public class DeleteCustomerByIdCommandHandler : IRequestHandler<DeleteCustomerByIdCommand, int>
{
private readonly IApplicationDbContext _context;
public DeleteCustomerByIdCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<int> Handle(DeleteCustomerByIdCommand request, CancellationToken cancellationToken)
{
var customer = await _context.Customers.Where(a => a.Id == request.Id).FirstOrDefaultAsync();
if (customer == null) return default;
_context.Customers.Remove(customer);
await _context.SaveChangesAsync();
return customer.Id;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using MediatR;
using OA.Data;
using OA.Domain.Entities;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace OA.Service.Features.CustomerFeatures.Commands
{
public class UpdateCustomerCommand : IRequest<int>
{
public int Id { get; set; }
public string CustomerName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public class UpdateCustomerCommandHandler : IRequestHandler<UpdateCustomerCommand, int>
{
private readonly IApplicationDbContext _context;
public UpdateCustomerCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<int> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
{
var cust = _context.Customers.Where(a => a.Id == request.Id).FirstOrDefault();

if (cust == null)
{
return default;
}
else
{
var customer = new Customer();
customer.CustomerName = request.CustomerName;
customer.ContactName = request.ContactName;
await _context.SaveChangesAsync();
return cust.Id;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using OA.Data;
using OA.Domain.Entities;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace OA.Service.Features.CustomerFeatures.Queries
{
public class GetAllCustomerQuery : IRequest<IEnumerable<Customer>>
{

public class GetAllCustomerQueryHandler : IRequestHandler<GetAllCustomerQuery, IEnumerable<Customer>>
{
private readonly IApplicationDbContext _context;
public GetAllCustomerQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Customer>> Handle(GetAllCustomerQuery request, CancellationToken cancellationToken)
{
var customerList = await _context.Customers.ToListAsync();
if (customerList == null)
{
return null;
}
return customerList.AsReadOnly();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using MediatR;
using OA.Data;
using OA.Domain.Entities;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace OA.Service.Features.CustomerFeatures.Queries
{
public class GetCustomerByIdQuery : IRequest<Customer>
{
public int Id { get; set; }
public class GetCustomerByIdQueryHandler : IRequestHandler<GetCustomerByIdQuery, Customer>
{
private readonly IApplicationDbContext _context;
public GetCustomerByIdQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Customer> Handle(GetCustomerByIdQuery request, CancellationToken cancellationToken)
{
var customer = _context.Customers.Where(a => a.Id == request.Id).FirstOrDefault();
if (customer == null) return null;
return customer;
}
}
}
}
Loading

0 comments on commit 75524e5

Please sign in to comment.