-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathService.cs
212 lines (184 loc) · 8.45 KB
/
Service.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Dotnet6.GraphQL4.Repositories.Abstractions;
using Dotnet6.GraphQL4.Repositories.Abstractions.Pages;
using Dotnet6.GraphQL4.Repositories.Abstractions.UnitsOfWork;
using Dotnet6.GraphQL4.CrossCutting.Notifications;
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
using Dotnet6.GraphQL4.Services.Abstractions.Models;
using Dotnet6.GraphQL4.Services.Abstractions.Resources;
using Microsoft.EntityFrameworkCore.Query;
namespace Dotnet6.GraphQL4.Services.Abstractions;
public abstract class Service<TEntity, TModel, TId> : IService<TEntity, TModel, TId>
where TEntity : Entity<TId>
where TModel : Model<TId>
where TId : struct
{
protected readonly IMapper Mapper;
protected readonly INotificationContext NotificationContext;
protected readonly IRepository<TEntity, TId> Repository;
protected readonly IUnitOfWork UnitOfWork;
protected Service(IRepository<TEntity, TId> repository, IUnitOfWork unitOfWork, INotificationContext notificationContext, IMapper mapper)
{
Repository = repository;
UnitOfWork = unitOfWork;
NotificationContext = notificationContext;
Mapper = mapper;
}
public virtual bool Delete(TId id)
{
if (IsValid(id) is false) return default;
Repository.Delete(id);
return UnitOfWork.SaveChanges();
}
public virtual bool Delete(TModel model)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return OnDelete(entity);
}
public virtual async Task<bool> DeleteAsync(TId id, CancellationToken cancellationToken)
{
if (IsValid(id) is false) return default;
await Repository.DeleteAsync(id, cancellationToken);
return await UnitOfWork.SaveChangesAsync(cancellationToken);
}
public virtual async Task<bool> DeleteAsync(TModel model, CancellationToken cancellationToken)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return await OnDeleteAsync(entity, cancellationToken);
}
public virtual TEntity Edit(TModel model)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return OnEdit(entity);
}
public virtual async Task<TEntity> EditAsync(TModel model, CancellationToken cancellationToken)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return await OnEditAsync(entity, cancellationToken);
}
public virtual bool Exists(TId id)
=> IsValid(id) ? Repository.Exists(id) : default;
public virtual async Task<bool> ExistsAsync(TId id, CancellationToken cancellationToken)
=> IsValid(id) ? await Repository.ExistsAsync(id, cancellationToken) : default;
public virtual PagedResult<TEntity> GetAll(
PageParams pageParams,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
=> Repository.GetAll(pageParams, predicate, orderBy, include, asTracking);
public virtual PagedResult<TResult> GetAllProjections<TResult>(
PageParams pageParams,
Expression<Func<TEntity, TResult>> selector = default,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
where TResult : class
=> Repository.GetAllProjections(pageParams, selector, predicate, orderBy, include, asTracking);
public virtual async Task<PagedResult<TEntity>> GetAllAsync(
PageParams pageParams,
CancellationToken cancellationToken,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
=> await Repository.GetAllAsync(pageParams, cancellationToken, predicate, orderBy, include, asTracking);
public virtual async Task<PagedResult<TResult>> GetAllProjectionsAsync<TResult>(
PageParams pageParams,
CancellationToken cancellationToken,
Expression<Func<TEntity, TResult>> selector = default,
Expression<Func<TEntity, bool>> predicate = default,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = default,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
bool asTracking = default)
where TResult : class
=> await Repository.GetAllProjectionsAsync(pageParams, cancellationToken, selector, predicate, orderBy, include, asTracking);
public virtual TEntity GetById(TId id, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default)
=> IsValid(id) ? Repository.GetById(id, include, asTracking) : default;
public virtual async Task<TEntity> GetByIdAsync(TId id, CancellationToken cancellationToken, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default)
=> IsValid(id) ? await Repository.GetByIdAsync(id, cancellationToken, include, asTracking) : default;
public virtual TEntity Save(TModel model)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return OnSave(entity);
}
public virtual async Task<TEntity> SaveAsync(TModel model, CancellationToken cancellationToken)
{
if (IsValid(model) is false) return default;
var entity = Mapper.Map<TEntity>(model);
return await OnSaveAsync(entity, cancellationToken);
}
protected TEntity OnSave(TEntity entity)
{
if (IsValid(entity) is false) return default;
Repository.Add(entity);
return UnitOfWork.SaveChanges() ? entity : default;
}
protected TEntity OnEdit(TEntity entity)
{
if (IsValid(entity) is false) return default;
Repository.Update(entity);
return UnitOfWork.SaveChanges() ? entity : default;
}
protected bool OnDelete(TEntity entity)
{
if (IsValid(entity) is false) return default;
Repository.Delete(entity);
return UnitOfWork.SaveChanges();
}
protected async Task<TEntity> OnEditAsync(TEntity entity, CancellationToken cancellationToken)
{
if (IsValid(entity) is false) return default;
await Repository.UpdateAsync(entity, cancellationToken);
return await UnitOfWork.SaveChangesAsync(cancellationToken) ? entity : default;
}
protected async Task<TEntity> OnSaveAsync(TEntity entity, CancellationToken cancellationToken)
{
if (IsValid(entity) is false) return default;
await Repository.AddAsync(entity, cancellationToken);
return await UnitOfWork.SaveChangesAsync(cancellationToken) ? entity : default;
}
protected async Task<bool> OnDeleteAsync(TEntity entity, CancellationToken cancellationToken)
{
if (IsValid(entity) is false) return default;
await Repository.DeleteAsync(entity, cancellationToken);
return await UnitOfWork.SaveChangesAsync(cancellationToken);
}
private bool IsValid(TEntity entity)
{
switch (entity)
{
case {IsValid: false}:
NotificationContext.AddNotifications(entity.ValidationResult);
return default;
case null:
NotificationContext.AddNotificationWithType(ServicesResource.Object_Null, typeof(TEntity));
return default;
default:
return true;
}
}
private bool IsValid(TModel model)
{
if (model is not null) return true;
NotificationContext.AddNotificationWithType(ServicesResource.Object_Null, typeof(TModel));
return default;
}
private bool IsValid(TId id)
{
if (Equals(id, default(TId)) is false) return true;
NotificationContext.AddNotificationWithType(ServicesResource.Identifier_Invalid, typeof(TEntity));
return default;
}
}