-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Amitpnk/feature/CQRS
Feature/cqrs
- Loading branch information
Showing
29 changed files
with
288 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
OnionArchitecture/OA.Data/Migrations/20200627044955_Initial-setup.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
|
40 changes: 40 additions & 0 deletions
40
OnionArchitecture/OA.Service/Features/CustomerFeatures/Commands/CreateCustomerCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
OnionArchitecture/OA.Service/Features/CustomerFeatures/Commands/DeleteCustomerByIdCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
OnionArchitecture/OA.Service/Features/CustomerFeatures/Commands/UpdateCustomerCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
OnionArchitecture/OA.Service/Features/CustomerFeatures/Queries/GetAllCustomerQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
OnionArchitecture/OA.Service/Features/CustomerFeatures/Queries/GetCustomerByIdQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.