Skip to content

Commit 43f43f9

Browse files
committed
Added CQRS
1 parent e0ef262 commit 43f43f9

21 files changed

+280
-117
lines changed

OnionArchitecture/OA.Data/ApplicationContext.cs renamed to OnionArchitecture/OA.Data/ApplicationDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace OA.Data
66
{
7-
public class ApplicationContext : DbContext, IApplicationContext
7+
public class ApplicationDbContext : DbContext, IApplicationDbContext
88
{
99
// This constructor is used of runit testing
10-
public ApplicationContext()
10+
public ApplicationDbContext()
1111
{
1212

1313
}
14-
public ApplicationContext(DbContextOptions options) : base(options)
14+
public ApplicationDbContext(DbContextOptions options) : base(options)
1515
{
1616
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
1717
}

OnionArchitecture/OA.Data/IApplicationContext.cs renamed to OnionArchitecture/OA.Data/IApplicationDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OA.Data
66
{
7-
public interface IApplicationContext
7+
public interface IApplicationDbContext
88
{
99
DbSet<Category> Categories { get; set; }
1010
DbSet<Customer> Customers { get; set; }

OnionArchitecture/OA.Data/Migrations/20200627044955_Initial-setup.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OnionArchitecture/OA.Data/Migrations/CustomerContextModelSnapshot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace OA.Data.Migrations
1010
{
11-
[DbContext(typeof(ApplicationContext))]
11+
[DbContext(typeof(ApplicationDbContext))]
1212
partial class CustomerContextModelSnapshot : ModelSnapshot
1313
{
1414
protected override void BuildModel(ModelBuilder modelBuilder)

OnionArchitecture/OA.Infrastructure/Extension/ConfigureServiceContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class ConfigureServiceContainer
2121
public static void AddDbContext(this IServiceCollection serviceCollection,
2222
IConfiguration configuration, IConfigurationRoot configRoot)
2323
{
24-
serviceCollection.AddDbContext<ApplicationContext>(options =>
24+
serviceCollection.AddDbContext<ApplicationDbContext>(options =>
2525
options.UseSqlServer(configuration.GetConnectionString("OnionArchConn") ?? configRoot["ConnectionStrings:OnionArchConn"])
2626
);
2727
}
@@ -44,7 +44,7 @@ public static void AddRepository(this IServiceCollection serviceCollection)
4444

4545
public static void AddTransientServices(this IServiceCollection serviceCollection)
4646
{
47-
serviceCollection.AddTransient<ICustomerService, CustomerService>();
47+
//serviceCollection.AddTransient<ICustomerService, CustomerService>();
4848
serviceCollection.AddTransient<IMailService, MailService>();
4949
}
5050

OnionArchitecture/OA.Persistence/Repository/CustomerRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace OA.Persistence.Repository
1010
{
1111
public class CustomerRepository : ICustomerRepository
1212
{
13-
private readonly ApplicationContext _context;
14-
public CustomerRepository(ApplicationContext context)
13+
private readonly ApplicationDbContext _context;
14+
public CustomerRepository(ApplicationDbContext context)
1515
{
1616
_context = context;
1717
}

OnionArchitecture/OA.Persistence/Repository/GenericRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace OA.Persistence.Repository
99
public class GenericRepository<T> : IGenericRepository<T> where T : class
1010
{
1111
private readonly DbSet<T> entities;
12-
private readonly ApplicationContext _context;
12+
private readonly ApplicationDbContext _context;
1313

14-
public GenericRepository(ApplicationContext context)
14+
public GenericRepository(ApplicationDbContext context)
1515
{
1616
_context = context;
1717
entities = _context.Set<T>();

OnionArchitecture/OA.Service/Contract/ICustomerService.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using MediatR;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace OA.Service
11+
{
12+
public static class DependencyInjection
13+
{
14+
public static void AddApplication(this IServiceCollection services)
15+
{
16+
services.AddMediatR(Assembly.GetExecutingAssembly());
17+
}
18+
}
19+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+

2+
using MediatR;
3+
using OA.Data;
4+
using OA.Domain.Entities;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Application.Features.ProductFeatures.Commands
12+
{
13+
public class CreateCustomerCommand : IRequest<int>
14+
{
15+
public string CustomerName { get; set; }
16+
public string ContactName { get; set; }
17+
public string ContactTitle { get; set; }
18+
public string Address { get; set; }
19+
public string City { get; set; }
20+
public string Region { get; set; }
21+
public string PostalCode { get; set; }
22+
public string Country { get; set; }
23+
public string Phone { get; set; }
24+
public string Fax { get; set; }
25+
public class CreateProductCommandHandler : IRequestHandler<CreateCustomerCommand, int>
26+
{
27+
private readonly IApplicationDbContext _context;
28+
public CreateProductCommandHandler(IApplicationDbContext context)
29+
{
30+
_context = context;
31+
}
32+
public async Task<int> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
33+
{
34+
var customer = new Customer();
35+
customer.CustomerName = request.CustomerName;
36+
customer.ContactName = request.ContactName;
37+
38+
_context.Customers.Add(customer);
39+
await _context.SaveChangesAsync();
40+
return customer.Id;
41+
}
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)