Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb2ba1c

Browse files
author
kadir.avci
committed
Changed CQRS implementation, changed Repository + UnitOfWork implementation, added ServicePattern
1 parent a4a65e6 commit fb2ba1c

File tree

16 files changed

+259
-87
lines changed

16 files changed

+259
-87
lines changed
562 KB
Binary file not shown.

‎Data/Command/CommandDispatcher.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Data.Command
2+
{
3+
/*
4+
public class CommandDispatcher : ICommandDispatcher
5+
{
6+
private readonly IContainer container;
7+
8+
public void Execute<TCommand>(TCommand command) where TCommand : ICommand
9+
{
10+
if (command == null)
11+
{
12+
throw new ArgumentNullException("command");
13+
}
14+
15+
var handler = container.Resolve<ICommandHandler<TCommand>>();
16+
17+
if (handler == null)
18+
{
19+
throw new CommandHandlerNotFoundException(typeof(TCommand));
20+
}
21+
22+
handler.Execute(command);
23+
}
24+
}
25+
*/
26+
}

‎Data/Data.csproj‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
</Content>
1919
</ItemGroup>
2020

21-
<ItemGroup>
22-
<Folder Include="Helpers\" />
23-
</ItemGroup>
24-
2521
<ItemGroup>
2622
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
2723
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="2.1.0" />

‎Data/Query/IQueryDispatcher.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Data.Query
22
{
3-
public interface IQueryDispatcher<TResult>whereTResult:IQuery
3+
public interface IQueryDispatcher
44
{
5-
TResult Execute<TQuery>(TQuery query) where TQuery : IQuery;
5+
TResult Query<TQuery,TResult>(TQuery query) where TQuery : IQuery;
66
}
77
}

‎Data/Query/IQueryHandler.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Data.Query
22
{
3-
//public interface IQueryHandler where TQuery : IQuery
4-
//{
5-
// TResult Execute(TQuery query);
6-
//}
3+
public interface IQueryHandler<TQuery,TResult> where TQuery : IQuery
4+
{
5+
TResult Query(TQuery query);
6+
}
77
}

‎Data/Query/QueryDispatcher.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Data.Query
2+
{
3+
/*
4+
public class QueryDispatcher : IQueryDispatcher
5+
{
6+
private readonly IContainer _container;
7+
8+
public QueryDispatcher(IContainer container)
9+
{
10+
_container = container;
11+
}
12+
13+
public TResult Query<TQuery, TResult>(TQuery query) where TQuery : IQuery
14+
{
15+
if (query == null)
16+
{
17+
throw new ArgumentNullException("query");
18+
}
19+
20+
var handler = _container.Resolve<IQueryHandler<TQuery, TResult>>();
21+
22+
if (handler == null)
23+
{
24+
throw new QueryHandlerNotFoundException(typeof(TQuery));
25+
}
26+
27+
return handler.Query(query);
28+
}
29+
}
30+
*/
31+
}

‎Data/Repository/IRepository.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.EntityFrameworkCore;
2-
using Model.ViewModels;
32
using System;
43
using System.Collections.Generic;
54
using System.Data;
@@ -12,32 +11,57 @@ namespace Data.Repository
1211
public interface IRepository<T> where T : class
1312
{
1413
void Add(T entity);
14+
1515
void AddRange(IEnumerable<T> list);
16+
1617
void Update(T entity);
18+
1719
void QuickUpdate(T entity, T TEntity);
20+
1821
void Delete(T entity);
22+
1923
void Delete(int id);
24+
2025
void DeleteRange(IEnumerable<T> list);
26+
2127
void Attach(T Entity);
28+
2229
void ExecQuery(string query);
30+
2331
void ChangeState(T entity, EntityState state);
2432

2533
T FindById(int id, bool lazyLoading = false);
34+
2635
Task<T> FindByIdAsync(int id, bool lazyLoading = false);
36+
2737
T FindById(string id, bool lazyLoading = false);
38+
2839
Task<T> FindByIdAsync(string id, bool lazyLoading = false);
40+
2941
T GetSingle(Expression<Func<T, bool>> expression, bool lazyLoading = false);
42+
3043
Task<T> GetSingleAsync(Expression<Func<T, bool>> expression, bool lazyLoading = false);
44+
3145
IQueryable<T> QueryAll(Expression<Func<T, bool>> expression);
46+
3247
IQueryable<T> Query();
48+
3349
IQueryable<T> Query(Expression<Func<T, bool>> expression);
50+
3451
IQueryable<T> QueryNoTracking();
52+
3553
IQueryable<T> QueryNoTracking(Expression<Func<T, bool>> expression);
54+
3655
IQueryable<T> Include(Expression<Func<T, object>> expression);
56+
3757
IQueryable<T> Include(params Expression<Func<T, object>>[] includes);
58+
3859
IList<T> SqlQuery(string query);
60+
3961
IList<dynamic> GetQueryResult(string query);
62+
4063
DataTable DataTableQuery(string query);
64+
4165
int ExecQuery(string query, params object[] parameters);
4266
}
4367
}

‎Data/Repository/Repository.cs‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using Model;
2-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
32
using System;
43
using System.Collections.Generic;
54
using System.ComponentModel;
65
using System.Data;
76
using System.Linq;
87
using System.Linq.Expressions;
98
using System.Threading.Tasks;
10-
using Model.ViewModels;
119

1210
namespace Data.Repository
1311
{
@@ -144,7 +142,7 @@ public T GetSingle(Expression<Func<T, bool>> expression, bool lazyLoading = fals
144142
_context.ChangeTracker.AutoDetectChangesEnabled = true;
145143
_context.ChangeTracker.LazyLoadingEnabled = lazyLoading;
146144

147-
if (hasFlag("<IsDeleted>"))
145+
if (HasFlag("<IsDeleted>"))
148146
{
149147
var parameters = expression.Parameters;
150148
var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(parameters[0], "IsDeleted"), Expression.Constant(false));
@@ -165,7 +163,7 @@ public async Task<T> GetSingleAsync(Expression<Func<T, bool>> expression, bool l
165163
_context.ChangeTracker.AutoDetectChangesEnabled = true;
166164
_context.ChangeTracker.LazyLoadingEnabled = lazyLoading;
167165

168-
if (hasFlag("<IsDeleted>"))
166+
if (HasFlag("<IsDeleted>"))
169167
{
170168
var parameters = expression.Parameters;
171169
var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(parameters[0], "IsDeleted"), Expression.Constant(false));
@@ -185,7 +183,7 @@ public IQueryable<T> QueryAll(Expression<Func<T, bool>> expression)
185183
{
186184
_context.ChangeTracker.LazyLoadingEnabled = true;
187185

188-
if (hasFlag("<IsDeleted>"))
186+
if (HasFlag("<IsDeleted>"))
189187
{
190188
var parameters = expression.Parameters;
191189
var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(parameters[0], "IsDeleted"), Expression.Constant(false));
@@ -205,7 +203,7 @@ public IQueryable<T> Query()
205203
{
206204
_context.ChangeTracker.LazyLoadingEnabled = false;
207205

208-
if (hasFlag("<IsDeleted>"))
206+
if (HasFlag("<IsDeleted>"))
209207
{
210208
var argument = Expression.Parameter(typeof(T));
211209
var left = Expression.Property(argument, "IsDeleted");
@@ -224,7 +222,7 @@ public IQueryable<T> Query(Expression<Func<T, bool>> expression)
224222
{
225223
_context.ChangeTracker.LazyLoadingEnabled = false;
226224

227-
if (hasFlag("<IsDeleted>"))
225+
if (HasFlag("<IsDeleted>"))
228226
{
229227
var parameters = expression.Parameters;
230228
var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(parameters[0], "IsDeleted"), Expression.Constant(false));
@@ -253,7 +251,7 @@ public IQueryable<T> QueryNoTracking(Expression<Func<T, bool>> expression)
253251
_context.ChangeTracker.AutoDetectChangesEnabled = false;
254252
_context.ChangeTracker.LazyLoadingEnabled = false;
255253

256-
if (hasFlag("<IsDeleted>"))
254+
if (HasFlag("<IsDeleted>"))
257255
{
258256
var parameters = expression.Parameters;
259257
var checkNotDeleted = Expression.Equal(Expression.PropertyOrField(parameters[0], "IsDeleted"), Expression.Constant(false));
@@ -326,13 +324,11 @@ public int ExecQuery(string query, params object[] parameters)
326324
return DbContext.Database.ExecuteSqlCommand("EXEC " + query, parameters);
327325
}
328326

329-
public bool hasFlag(string field)
327+
public bool HasFlag(string field)
330328
{
331329
var hasFlag = false;
332-
333330
var genericTypeArguments = _dbSet.GetType().GenericTypeArguments;
334-
//entity.GetType().GetProperty("IsDeleted")
335-
331+
336332
if (genericTypeArguments.Any())
337333
{
338334
var fields = ((System.Reflection.TypeInfo)(_dbSet.GetType().GenericTypeArguments.FirstOrDefault())).DeclaredFields;
@@ -343,7 +339,7 @@ public bool hasFlag(string field)
343339
return hasFlag;
344340
}
345341

346-
public DataTable ConvertToDataTable<T>(IList<T> data)
342+
public DataTable ConvertToDataTable(IList<T> data)
347343
{
348344
var properties = TypeDescriptor.GetProperties(typeof(T));
349345
var table = new DataTable();
@@ -368,4 +364,4 @@ public DataTable ConvertToDataTable<T>(IList<T> data)
368364
return table;
369365
}
370366
}
371-
}
367+
}

‎Data/UnitOfWork/IUnitOfWork.cs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
using Data.Repository;
2-
using Model.ViewModels;
32
using System;
43
using System.Threading.Tasks;
54

65
namespace Data.UnitOfWork
76
{
87
public interface IUnitOfWork : IDisposable
98
{
10-
Model.Models.AppDbContext DbContext { get; }
119
IRepository<T> Repository<T>() where T : class;
1210

1311
int SaveChanges();
12+
1413
Task<int> SaveChangesAsync();
15-
void ChangeTracking(bool option);
16-
void LazyLoading(bool option);
1714
}
18-
}
15+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /