Skip to content

Commit a131da0

Browse files
committed
Removing UnitOfWork usage
1 parent 7ff48f0 commit a131da0

7 files changed

Lines changed: 42 additions & 144 deletions

File tree

Data/Repository/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ public interface IRepository<T> where T : class
1515
public Task<List<T>> GetWhere(Expression<Func<T, bool>> filter, string[] includes = null);
1616
public Task<List<T>> GetWhere(Expression<Func<T, bool>> filter, string includes);
1717
public Task<T?> FirstOrDefault(Expression<Func<T, bool>> filter, string[] includes = null);
18+
Task<Result<bool>> CommitAsync();
1819
}

Data/Repository/Repository.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public class Repository<T>(InventoryDbContext context) : IRepository<T> where T
99
{
1010
private readonly InventoryDbContext _context = context;
1111
private readonly DbSet<T> _set = context.Set<T>();
12-
private bool _disposed = false;
1312

1413
public async Task<Result<T>> CreateAsync(T Entity)
1514
{
@@ -77,4 +76,17 @@ public async Task<List<T>> GetWhere(Expression<Func<T, bool>> filter, string inc
7776

7877
return await query.FirstOrDefaultAsync(filter);
7978
}
79+
80+
public async Task<Result<bool>> CommitAsync()
81+
{
82+
try
83+
{
84+
await _context.SaveChangesAsync();
85+
return Result<bool>.Success(true);
86+
}
87+
catch (DbUpdateException ex)
88+
{
89+
return Result<bool>.Failure($"Não foi possível salvar as informações {ex.Message}", false);
90+
}
91+
}
8092
}

Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
options.UseMySQL(builder.Configuration.GetConnectionString("AZURE_MYSQL_CONNECTIONSTRING"))
1717
);
1818

19-
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
2019
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
2120
builder.Services.AddScoped<IDeviceService, DeviceService>();
2221
builder.Services.AddScoped<IDeviceModelService, DeviceModelService>();

Services/Brand/BrandService.cs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
using TechInventory.Models;
2-
using TechInventory.Data.UnitOfWork;
2+
using TechInventory.Data.Context;
33
using TechInventory.Data.Repository;
44

55
namespace TechInventory.Services.Brand;
66

7-
public class BrandService : IBrandService, IDisposable
7+
public class BrandService(InventoryDbContext context) : IBrandService
88
{
9-
private readonly UnitOfWork _unitOfWork;
10-
private readonly IRepository<Models.Brand> _repository;
11-
private bool _disposed = false;
12-
13-
public BrandService(IUnitOfWork unitOfWork)
14-
{
15-
_unitOfWork = (UnitOfWork)unitOfWork;
16-
_repository = _unitOfWork.BrandRepository;
17-
}
9+
private readonly IRepository<Models.Brand> _repository = new Repository<Models.Brand>(context);
1810

1911
public async Task<Result<bool>> CreateBrand(Models.Brand brand)
2012
{
2113
await _repository.CreateAsync(brand);
22-
return await _unitOfWork.CommitAsync();
14+
return await _repository.CommitAsync();
2315
}
2416

2517
public Task<Models.Brand> GetBrandById(int id)
@@ -40,7 +32,7 @@ public async Task<Result<bool>> CreateBrand(Models.Brand brand)
4032
public async Task<Result<bool>> UpdateBrand(Models.Brand brand)
4133
{
4234
_repository.Update(brand);
43-
return await _unitOfWork.CommitAsync();
35+
return await _repository.CommitAsync();
4436
}
4537

4638
public async Task<Result<bool>> DeleteBrand(int id)
@@ -53,25 +45,6 @@ public async Task<Result<bool>> DeleteBrand(int id)
5345
if (!deleteResult.IsSuccessful)
5446
return Result<bool>.Failure(deleteResult.Message, false);
5547

56-
return await _unitOfWork.CommitAsync();
57-
}
58-
59-
protected virtual void Dispose(bool disposing)
60-
{
61-
if (!_disposed)
62-
{
63-
if (disposing)
64-
{
65-
// Nothing to dispose here as per the request.
66-
// _unitOfWork is managed by DI container.
67-
}
68-
_disposed = true;
69-
}
70-
}
71-
72-
public void Dispose()
73-
{
74-
Dispose(true);
75-
GC.SuppressFinalize(this);
48+
return await _repository.CommitAsync();
7649
}
7750
}

Services/Device/DeviceService.cs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
using TechInventory.Models;
22
using TechInventory.Data.Repository;
3-
using TechInventory.Data.UnitOfWork;
3+
using TechInventory.Data.Context;
44

55
namespace TechInventory.Services.Device;
66

7-
public class DeviceService : IDeviceService, IDisposable
7+
public class DeviceService(InventoryDbContext context) : IDeviceService
88
{
9-
private readonly UnitOfWork _unitOfWork;
10-
private readonly IRepository<Models.Device> _repository;
11-
private readonly IRepository<Models.DeviceModel> _deviceModelRepository;
12-
private bool _disposed = false;
13-
14-
public DeviceService(IUnitOfWork unitOfWork)
15-
{
16-
_unitOfWork = (UnitOfWork)unitOfWork;
17-
_repository = _unitOfWork.DeviceRepository;
18-
_deviceModelRepository = _unitOfWork.DeviceModelRepository;
19-
}
9+
private readonly IRepository<Models.Device> _repository = new Repository<Models.Device>(context);
10+
private readonly IRepository<Models.DeviceModel> _deviceModelRepository = new Repository<Models.DeviceModel>(context);
2011

2112
public async Task<Result<bool>> AddDevice(Models.Device device)
2213
{
@@ -25,7 +16,7 @@ public async Task<Result<bool>> AddDevice(Models.Device device)
2516
return checkResult;
2617

2718
await _repository.CreateAsync(device);
28-
return await _unitOfWork.CommitAsync();
19+
return await _repository.CommitAsync();
2920
}
3021

3122
public async Task<Result<bool>> DeleteDevice(int id)
@@ -38,7 +29,7 @@ public async Task<Result<bool>> DeleteDevice(int id)
3829
if (!deleteResult.IsSuccessful)
3930
return Result<bool>.Failure(deleteResult.Message, false);
4031

41-
var commitResult = await _unitOfWork.CommitAsync();
32+
var commitResult = await _repository.CommitAsync();
4233

4334
return commitResult.IsSuccessful ? Result<bool>.Success(true) : Result<bool>.Failure(commitResult.Message, false);
4435
}
@@ -48,9 +39,9 @@ public async Task<Result<bool>> UpdateDevice(Models.Device device)
4839
var checkResult = await CheckIncludes(device);
4940
if (!checkResult.IsSuccessful)
5041
return checkResult;
51-
42+
5243
_repository.Update(device);
53-
return await _unitOfWork.CommitAsync();
44+
return await _repository.CommitAsync();
5445
}
5546

5647
public async Task<Models.Device?> GetDeviceById(int id)
@@ -92,24 +83,4 @@ public async Task<Result<bool>> CheckIncludes(Models.Device device)
9283

9384
return Result<bool>.Success(true);
9485
}
95-
96-
protected virtual void Dispose(bool disposing)
97-
{
98-
if (!_disposed)
99-
{
100-
if (disposing)
101-
{
102-
// Nothing to dispose here as per the request.
103-
// _unitOfWork is managed by DI container.
104-
}
105-
106-
_disposed = true;
107-
}
108-
}
109-
110-
public void Dispose()
111-
{
112-
Dispose(true);
113-
GC.SuppressFinalize(this);
114-
}
11586
}
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
using TechInventory.Data.UnitOfWork;
1+
using TechInventory.Data.Context;
22
using TechInventory.Data.Repository;
33
using TechInventory.Models;
44

55
namespace TechInventory.Services.DeviceModel;
66

7-
public class DeviceModelService : IDeviceModelService, IDisposable
7+
public class DeviceModelService(InventoryDbContext context) : IDeviceModelService
88
{
9-
private readonly UnitOfWork _unitOfWork;
10-
private readonly IRepository<Models.DeviceModel> _deviceModelRepository;
11-
private readonly IRepository<Models.Brand> _brandRepository;
12-
private bool _disposed = false;
9+
private readonly IRepository<Models.DeviceModel> _deviceModelRepository = new Repository<Models.DeviceModel>(context);
10+
private readonly IRepository<Models.Brand> _brandRepository = new Repository<Models.Brand>(context);
1311

14-
public DeviceModelService(IUnitOfWork unitOfWork)
15-
{
16-
_unitOfWork = (UnitOfWork)unitOfWork;
17-
_deviceModelRepository = _unitOfWork.DeviceModelRepository;
18-
_brandRepository = _unitOfWork.BrandRepository;
19-
}
20-
2112
public async Task<Result<bool>> CreateDeviceModel(Models.DeviceModel deviceModel)
2213
{
2314
var checkResult = await CheckIncludes(deviceModel);
2415
if (!checkResult.IsSuccessful)
2516
return checkResult;
2617

2718
await _deviceModelRepository.CreateAsync(deviceModel);
28-
return await _unitOfWork.CommitAsync();
19+
return await _deviceModelRepository.CommitAsync();
2920
}
3021

3122
public async Task<Models.DeviceModel> GetDeviceModelById(int id)
@@ -45,7 +36,7 @@ public async Task<Result<bool>> UpdateDeviceModel(Models.DeviceModel deviceModel
4536
return checkResult;
4637

4738
_deviceModelRepository.Update(deviceModel);
48-
return await _unitOfWork.CommitAsync();
39+
return await _deviceModelRepository.CommitAsync();
4940
}
5041

5142
public async Task<Result<bool>> DeleteDeviceModel(int id)
@@ -58,7 +49,7 @@ public async Task<Result<bool>> DeleteDeviceModel(int id)
5849
if (!deleteResult.IsSuccessful)
5950
return Result<bool>.Failure(deleteResult.Message, false);
6051

61-
return await _unitOfWork.CommitAsync();
52+
return await _deviceModelRepository.CommitAsync();
6253
}
6354

6455
public async Task<List<Models.DeviceModel>> GetDeviceModelsByBrandId(int brandId)
@@ -74,24 +65,4 @@ public async Task<Result<bool>> CheckIncludes(Models.DeviceModel deviceModel)
7465
return Result<bool>.Failure($"A marca com id {deviceModel.BrandId} não existe", false);
7566
return Result<bool>.Success(true);
7667
}
77-
78-
protected virtual void Dispose(bool disposing)
79-
{
80-
if (!_disposed)
81-
{
82-
if (disposing)
83-
{
84-
// Nothing to dispose here as per the request.
85-
// _unitOfWork is managed by DI container.
86-
}
87-
88-
_disposed = true;
89-
}
90-
}
91-
92-
public void Dispose()
93-
{
94-
Dispose(true);
95-
GC.SuppressFinalize(this);
96-
}
9768
}
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
using TechInventory.Models;
2-
using TechInventory.Data.UnitOfWork;
2+
using TechInventory.Data.Context;
33
using TechInventory.Data.Repository;
44

55
namespace TechInventory.Services.Maintenance;
66

7-
public class MaintenanceRecordService : IMaintenanceRecordService, IDisposable
7+
public class MaintenanceRecordService(InventoryDbContext context) : IMaintenanceRecordService
88
{
9-
private readonly UnitOfWork _unitOfWork;
10-
private readonly IRepository<MaintenanceRecord> _repository;
11-
private readonly IRepository<Models.Device> _deviceRepository; // This is correct
12-
private bool _disposed = false;
13-
14-
public MaintenanceRecordService(IUnitOfWork unitOfWork)
15-
{
16-
_unitOfWork = (UnitOfWork)unitOfWork;
17-
_repository = _unitOfWork.MaintenanceRecordRepository;
18-
_deviceRepository = _unitOfWork.DeviceRepository; // This is correct
19-
}
9+
private readonly IRepository<MaintenanceRecord> _repository = new Repository<MaintenanceRecord>(context);
10+
private readonly IRepository<Models.Device> _deviceRepository = new Repository<Models.Device>(context);
2011

2112
public async Task<Result<bool>> CreateRecord(MaintenanceRecord record)
2213
{
@@ -25,7 +16,7 @@ public async Task<Result<bool>> CreateRecord(MaintenanceRecord record)
2516
return checkResult;
2617

2718
await _repository.CreateAsync(record);
28-
return await _unitOfWork.CommitAsync();
19+
return await _repository.CommitAsync();
2920
}
3021

3122
public async Task<Result<bool>> UpdateRecord(MaintenanceRecord record)
@@ -35,7 +26,7 @@ public async Task<Result<bool>> UpdateRecord(MaintenanceRecord record)
3526
return checkResult;
3627

3728
_repository.Update(record);
38-
return await _unitOfWork.CommitAsync();
29+
return await _repository.CommitAsync();
3930
}
4031

4132
public async Task<Result<bool>> DeleteRecord(int id)
@@ -48,7 +39,7 @@ public async Task<Result<bool>> DeleteRecord(int id)
4839
if (!deleteResult.IsSuccessful)
4940
return Result<bool>.Failure(deleteResult.Message, false);
5041

51-
return await _unitOfWork.CommitAsync();
42+
return await _repository.CommitAsync();
5243
}
5344

5445
public async Task<MaintenanceRecord> GetRecordById(int id)
@@ -81,24 +72,4 @@ public async Task<Result<bool>> CheckIncludes(MaintenanceRecord record)
8172
return Result<bool>.Failure("O dispositivo associado ao registro não existe", false);
8273
return Result<bool>.Success(true);
8374
}
84-
85-
protected virtual void Dispose(bool disposing)
86-
{
87-
if (!_disposed)
88-
{
89-
if (disposing)
90-
{
91-
// Nothing to dispose here as per the request.
92-
// _unitOfWork is managed by DI container.
93-
}
94-
95-
_disposed = true;
96-
}
97-
}
98-
99-
public void Dispose()
100-
{
101-
Dispose(true);
102-
GC.SuppressFinalize(this);
103-
}
10475
}

0 commit comments

Comments
 (0)