Skip to content

Commit 48365d7

Browse files
Merge pull request #149 from AntonioFalcao/feature/code-smells
Feature/code smells
2 parents c954b01 + 69ddc73 commit 48365d7

29 files changed

Lines changed: 294 additions & 276 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Dotnet5.GraphQL3.CrossCutting
7+
{
8+
public static class Application
9+
{
10+
public static string Prefix { get; } = Assembly.GetEntryAssembly()?.FullName?.Substring(0, 16);
11+
public static IEnumerable<Assembly> Assemblies { get; } =
12+
AppDomain.CurrentDomain.GetAssemblies().Where(assembly => assembly.FullName?.StartsWith(Prefix) ?? false);
13+
}
14+
}

src/Dotnet5.GraphQL3.CrossCutting/Extensions/LocalAssemblyExtensions.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Dotnet5.GraphQL3.Domain.Abstractions/Extensions/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Dotnet5.GraphQL3.CrossCutting.Extensions;
1+
using Dotnet5.GraphQL3.CrossCutting;
22
using Dotnet5.GraphQL3.Domain.Abstractions.Builders;
33
using Microsoft.Extensions.DependencyInjection;
44
using Scrutor;
@@ -8,12 +8,9 @@ namespace Dotnet5.GraphQL3.Domain.Abstractions.Extensions.DependencyInjection
88
public static class ServiceCollectionExtensions
99
{
1010
public static IServiceCollection AddBuilders(this IServiceCollection services)
11-
=> services.Scan(selector
12-
=> selector
13-
.FromApplicationDependencies(assembly
14-
=> assembly.FullName?.StartsWith(assembly.GetEntryAssemblySuffix()) ?? default)
15-
.AddClasses(filter
16-
=> filter.AssignableTo(typeof(IBuilder<,>)))
11+
=> services
12+
.Scan(selector => selector.FromAssemblies(Application.Assemblies)
13+
.AddClasses(filter => filter.AssignableTo(typeof(IBuilder<,>)))
1714
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
1815
.AsImplementedInterfaces()
1916
.WithScopedLifetime());

src/Dotnet5.GraphQL3.Repositories.Abstractions/Extensions/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Dotnet5.GraphQL3.CrossCutting.Extensions;
1+
using Dotnet5.GraphQL3.CrossCutting;
22
using Dotnet5.GraphQL3.Repositories.Abstractions.UnitsOfWork;
33
using Microsoft.Extensions.DependencyInjection;
44
using Scrutor;
@@ -8,12 +8,9 @@ namespace Dotnet5.GraphQL3.Repositories.Abstractions.Extensions.DependencyInject
88
public static class ServiceCollectionExtensions
99
{
1010
public static IServiceCollection AddRepositories(this IServiceCollection services)
11-
=> services.Scan(selector
12-
=> selector
13-
.FromApplicationDependencies(assembly
14-
=> assembly.FullName?.StartsWith(assembly.GetEntryAssemblySuffix()) ?? default)
15-
.AddClasses(filter
16-
=> filter.AssignableTo(typeof(IRepository<,>)))
11+
=> services
12+
.Scan(selector => selector.FromAssemblies(Application.Assemblies)
13+
.AddClasses(filter => filter.AssignableTo(typeof(IRepository<,>)))
1714
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
1815
.AsImplementedInterfaces()
1916
.WithScopedLifetime());

src/Dotnet5.GraphQL3.Repositories.Abstractions/IRepository.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@ public interface IRepository<TEntity, in TId>
1414
where TId : struct
1515
{
1616
TEntity Add(TEntity entity);
17-
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default);
17+
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken);
1818

1919
void Delete(TId id);
2020
void Delete(TEntity entity);
21-
Task DeleteAsync(TId id, CancellationToken cancellationToken = default);
21+
Task DeleteAsync(TId id, CancellationToken cancellationToken);
22+
Task DeleteAsync(TEntity entity, CancellationToken cancellationToken);
2223

2324
bool Exists(TId id);
24-
Task<bool> ExistsAsync(TId id, CancellationToken cancellationToken = default);
25+
Task<bool> ExistsAsync(TId id, CancellationToken cancellationToken);
2526

26-
TEntity GetById(TId id, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
27-
bool asTracking = default);
28-
29-
Task<TEntity> GetByIdAsync(TId id, CancellationToken cancellationToken,
30-
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default,
31-
bool asTracking = default);
27+
TEntity GetById(TId id, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default);
28+
Task<TEntity> GetByIdAsync(TId id, CancellationToken cancellationToken, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default);
3229

3330
void Update(TEntity entity);
34-
Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default);
31+
Task UpdateAsync(TEntity entity, CancellationToken cancellationToken);
3532

3633
PagedResult<TEntity> GetAll(
3734
PageParams pageParams,

src/Dotnet5.GraphQL3.Repositories.Abstractions/Pages/PagedResult.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@ namespace Dotnet5.GraphQL3.Repositories.Abstractions.Pages
88
{
99
public class PagedResult<T>
1010
{
11+
private readonly IReadOnlyCollection<T> _items;
1112
private readonly int _index;
12-
private readonly IEnumerable<T> _items;
1313
private readonly int _size;
1414

15-
public PagedResult(IEnumerable<T> items, int index, int size)
15+
public PagedResult(IReadOnlyCollection<T> items, int index, int size)
1616
{
1717
_items = items;
1818
_index = index;
1919
_size = size;
2020
}
2121

22-
public IEnumerable<T> Items
23-
=> _items.Take(_size);
22+
public IReadOnlyCollection<T> Items
23+
=> _items.Take(_size).ToList();
2424

2525
public PageInfo PageInfo
2626
=> new()
2727
{
2828
Current = _index,
29-
Size = Items.Count(),
30-
HasNext = _size < _items.Count(),
29+
Size = Items.Count,
30+
HasNext = _size < _items.Count,
3131
HasPrevious = _index > 1
3232
};
3333

3434
public static async Task<PagedResult<T>> CreateAsync(IQueryable<T> source, PageParams pageParams, CancellationToken cancellationToken)
3535
{
3636
pageParams ??= new();
3737
var items = await ApplyPagination(source, pageParams).ToListAsync(cancellationToken);
38-
return new PagedResult<T>(items, pageParams.Index, pageParams.Size);
38+
return new(items, pageParams.Index, pageParams.Size);
3939
}
4040

4141
public static PagedResult<T> Create(IQueryable<T> source, PageParams pageParams)
4242
{
4343
pageParams ??= new();
4444
var items = ApplyPagination(source, pageParams).ToList();
45-
return new PagedResult<T>(items, pageParams.Index, pageParams.Size);
45+
return new(items, pageParams.Index, pageParams.Size);
4646
}
4747

4848
private static IQueryable<T> ApplyPagination(IQueryable<T> source, PageParams pageParams)

src/Dotnet5.GraphQL3.Repositories.Abstractions/Repository.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,31 @@ protected Repository(DbContext dbDbContext, IConfigurationProvider configuration
2727

2828
public virtual void Delete(TId id)
2929
{
30-
var entity = GetById(id);
30+
var entity = GetById(id, asTracking: true);
3131
if (entity is null) return;
3232
_dbSet.Remove(entity);
3333
}
3434

3535
public virtual void Delete(TEntity entity)
3636
{
3737
if (entity is null) return;
38+
_dbSet.Attach(entity);
3839
_dbSet.Remove(entity);
3940
}
4041

4142
public virtual async Task DeleteAsync(TId id, CancellationToken cancellationToken)
4243
{
43-
var entity = await GetByIdAsync(id, cancellationToken);
44+
var entity = await GetByIdAsync(id, cancellationToken, asTracking: true);
4445
if (entity is null) return;
4546
_dbSet.Remove(entity);
4647
}
4748

49+
public virtual async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken)
50+
{
51+
if (entity is null) return;
52+
await Task.Run(() => Delete(entity), cancellationToken);
53+
}
54+
4855
public virtual bool Exists(TId id)
4956
=> _dbSet.Any(x => Equals(x.Id, id));
5057

@@ -53,16 +60,18 @@ public virtual Task<bool> ExistsAsync(TId id, CancellationToken cancellationToke
5360

5461
public virtual TEntity Add(TEntity entity)
5562
{
63+
if (entity is null) return default;
5664
if (Exists(entity.Id)) return entity;
57-
_dbSet.Add(entity);
58-
return entity;
65+
var entityEntry = _dbSet.Add(entity);
66+
return entityEntry.Entity;
5967
}
6068

6169
public virtual async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken)
6270
{
71+
if (entity is null) return default;
6372
if (await ExistsAsync(entity.Id, cancellationToken)) return entity;
64-
await _dbSet.AddAsync(entity, cancellationToken);
65-
return entity;
73+
var entityEntry = await _dbSet.AddAsync(entity, cancellationToken);
74+
return entityEntry.Entity;
6675
}
6776

6877
public TEntity GetById(TId id, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = default, bool asTracking = default)

src/Dotnet5.GraphQL3.Repositories.Abstractions/UnitsOfWork/IUnitOfWork.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ public interface IUnitOfWork : IDisposable
99
{
1010
IDbContextTransaction BeginTransaction();
1111
Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken);
12-
void Commit();
13-
Task CommitAsync(CancellationToken cancellationToken);
14-
void Rollback();
15-
Task RollbackAsync(CancellationToken cancellationToken);
16-
void SaveChanges();
17-
Task SaveChangesAsync(CancellationToken cancellationToken);
12+
void CommitTransaction();
13+
Task CommitTransactionAsync(CancellationToken cancellationToken);
14+
void RollbackTransaction();
15+
Task RollbackTransactionAsync(CancellationToken cancellationToken);
16+
bool SaveChanges();
17+
Task<bool> SaveChangesAsync(CancellationToken cancellationToken);
1818
}
1919
}

src/Dotnet5.GraphQL3.Repositories.Abstractions/UnitsOfWork/UnitOfWork.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Threading;
23
using System.Threading.Tasks;
34
using Microsoft.EntityFrameworkCore;
@@ -20,28 +21,31 @@ public UnitOfWork(DbContext dbContext)
2021
public IDbContextTransaction BeginTransaction()
2122
=> _database.BeginTransaction();
2223

23-
public Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken)
24-
=> _database.BeginTransactionAsync(cancellationToken);
24+
public async Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken)
25+
=> await _database.BeginTransactionAsync(cancellationToken);
2526

26-
public void SaveChanges()
27-
=> _dbContext.SaveChanges(true);
27+
public bool SaveChanges()
28+
=> _dbContext.SaveChanges(true) > default(int);
2829

29-
public Task SaveChangesAsync(CancellationToken cancellationToken)
30-
=> _dbContext.SaveChangesAsync(true, cancellationToken);
30+
public async Task<bool> SaveChangesAsync(CancellationToken cancellationToken)
31+
=> await _dbContext.SaveChangesAsync(true, cancellationToken) > default(int);
3132

32-
public void Commit()
33+
public void CommitTransaction()
3334
=> _database.CommitTransaction();
3435

35-
public Task CommitAsync(CancellationToken cancellationToken)
36-
=> _database.CommitTransactionAsync(cancellationToken);
36+
public async Task CommitTransactionAsync(CancellationToken cancellationToken)
37+
=> await _database.CommitTransactionAsync(cancellationToken);
3738

38-
public void Rollback()
39+
public void RollbackTransaction()
3940
=> _database.RollbackTransaction();
4041

41-
public Task RollbackAsync(CancellationToken cancellationToken)
42-
=> _database.RollbackTransactionAsync(cancellationToken);
42+
public async Task RollbackTransactionAsync(CancellationToken cancellationToken)
43+
=> await _database.RollbackTransactionAsync(cancellationToken);
4344

4445
public void Dispose()
45-
=> _dbContext?.Dispose();
46+
{
47+
_dbContext?.Dispose();
48+
GC.SuppressFinalize(this);
49+
}
4650
}
4751
}
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Reactive.Subjects;
2-
using System.Reflection;
2+
using Dotnet5.GraphQL3.CrossCutting;
33
using Dotnet5.GraphQL3.Services.Abstractions.Messages;
44
using Microsoft.Extensions.DependencyInjection;
55
using Scrutor;
@@ -9,29 +9,25 @@ namespace Dotnet5.GraphQL3.Services.Abstractions.Extensions.DependencyInjection
99
public static class ServiceCollectionExtensions
1010
{
1111
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
12-
=> services.Scan(selector
13-
=> selector.FromApplicationDependencies(assembly
14-
=> assembly.FullName?.StartsWith(GetAssemblySuffix()) ?? default)
15-
.AddClasses(filter
16-
=> filter.AssignableTo(typeof(IService<,,>)))
17-
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
18-
.AsImplementedInterfaces()
19-
.WithScopedLifetime());
12+
=> services
13+
.Scan(selector => selector.FromAssemblies(Application.Assemblies)
14+
.AddClasses(filter => filter.AssignableTo(typeof(IService<,,>)))
15+
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
16+
.AsImplementedInterfaces()
17+
.WithScopedLifetime());
2018

21-
public static IServiceCollection AddMessageServices(this IServiceCollection services)
22-
=> services.Scan(selector
23-
=> selector.FromApplicationDependencies(assembly
24-
=> assembly.FullName?.StartsWith(GetAssemblySuffix()) ?? default)
25-
.AddClasses(filter
26-
=> filter.AssignableTo(typeof(IMessageService<,,>)))
27-
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
28-
.AsImplementedInterfaces()
29-
.WithSingletonLifetime());
19+
public static IServiceCollection AddApplicationMessageServices(this IServiceCollection services)
20+
=> services
21+
.Scan(selector => selector.FromAssemblies(Application.Assemblies)
22+
.AddClasses(filter => filter.AssignableTo(typeof(IMessageService<,,>)))
23+
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
24+
.AsImplementedInterfaces()
25+
.WithSingletonLifetime());
3026

31-
public static IServiceCollection AddSubjects(this IServiceCollection services)
32-
=> services.AddSingleton(typeof(ISubject<>), typeof(ReplaySubject<>));
27+
public static IServiceCollection AddApplicationAutoMapper(this IServiceCollection services)
28+
=> services.AddAutoMapper(Application.Assemblies);
3329

34-
private static string GetAssemblySuffix()
35-
=> Assembly.GetEntryAssembly()?.FullName?.Substring(0, 16);
30+
public static IServiceCollection AddApplicationSubjects(this IServiceCollection services)
31+
=> services.AddSingleton(typeof(ISubject<>), typeof(ReplaySubject<>));
3632
}
3733
}

0 commit comments

Comments
 (0)