Skip to content

Commit ac209c7

Browse files
committed
Editing UnitOfWork pattern for scoped lifetime
1 parent 8cc6860 commit ac209c7

5 files changed

Lines changed: 24 additions & 25 deletions

File tree

Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using TechInventory.Services.DeviceModel;
33
using TechInventory.Services.Brand;
44
using TechInventory.Services.Maintenance;
5+
using TechInventory.Data.Repository;
56
using TechInventory.Data.UnitOfWork;
67
using TechInventory.Data.Context;
78
using Microsoft.EntityFrameworkCore;
@@ -15,6 +16,8 @@
1516
options.UseMySQL(builder.Configuration.GetConnectionString("AZURE_MYSQL_CONNECTIONSTRING"))
1617
);
1718

19+
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
20+
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
1821
builder.Services.AddScoped<IDeviceService, DeviceService>();
1922
builder.Services.AddScoped<IDeviceModelService, DeviceModelService>();
2023
builder.Services.AddScoped<IBrandService, BrandService>();

Services/Brand/BrandService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using TechInventory.Models;
22
using TechInventory.Data.UnitOfWork;
3-
using TechInventory.Data.Context;
43
using TechInventory.Data.Repository;
54

65
namespace TechInventory.Services.Brand;
@@ -10,9 +9,9 @@ public class BrandService : IBrandService
109
private readonly UnitOfWork _unitOfWork;
1110
private readonly IRepository<Models.Brand> _repository;
1211

13-
public BrandService(InventoryDbContext context)
12+
public BrandService(IUnitOfWork unitOfWork)
1413
{
15-
_unitOfWork = new UnitOfWork(context);
14+
_unitOfWork = (UnitOfWork)unitOfWork;
1615
_repository = _unitOfWork.BrandRepository;
1716
}
1817

Services/Device/DeviceService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
using TechInventory.Models;
22
using TechInventory.Data.Repository;
33
using TechInventory.Data.UnitOfWork;
4-
using TechInventory.Data.Context;
54

65
namespace TechInventory.Services.Device;
76

87
public class DeviceService : IDeviceService
98
{
109
private readonly UnitOfWork _unitOfWork;
1110
private readonly IRepository<Models.Device> _repository;
12-
private readonly IRepository<Models.DeviceModel> _modelRepository;
11+
private readonly IRepository<Models.DeviceModel> _deviceModelRepository;
1312

14-
public DeviceService(InventoryDbContext context)
13+
public DeviceService(IUnitOfWork unitOfWork)
1514
{
16-
_unitOfWork = new UnitOfWork(context);
15+
_unitOfWork = (UnitOfWork)unitOfWork;
1716
_repository = _unitOfWork.DeviceRepository;
18-
_modelRepository = _unitOfWork.DeviceModelRepository;
17+
_deviceModelRepository = _unitOfWork.DeviceModelRepository;
1918
}
2019

2120
public async Task<Result<bool>> AddDevice(Models.Device device)
@@ -85,7 +84,7 @@ public async Task<Result<bool>> UpdateDevice(Models.Device device)
8584
public async Task<Result<bool>> CheckIncludes(Models.Device device)
8685
{
8786
// Check if the device's model exists
88-
var result = await _modelRepository.GetAsync(device.DeviceModelId);
87+
var result = await _deviceModelRepository.GetAsync(device.DeviceModelId);
8988

9089
if (result == null)
9190
return Result<bool>.Failure($"Modelo com Id {device.DeviceModelId} não foi encontrado", false);

Services/DeviceModel/DeviceModelService.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using TechInventory.Data.UnitOfWork;
22
using TechInventory.Data.Repository;
3-
using TechInventory.Data.Context;
43
using TechInventory.Models;
54

65
namespace TechInventory.Services.DeviceModel;
76

87
public class DeviceModelService : IDeviceModelService
98
{
109
private readonly UnitOfWork _unitOfWork;
11-
private readonly IRepository<Models.DeviceModel> _repository;
10+
private readonly IRepository<Models.DeviceModel> _deviceModelRepository;
1211
private readonly IRepository<Models.Brand> _brandRepository;
1312

14-
public DeviceModelService(InventoryDbContext context)
13+
public DeviceModelService(IUnitOfWork unitOfWork)
1514
{
16-
_unitOfWork = new UnitOfWork(context);
17-
_repository = _unitOfWork.DeviceModelRepository;
15+
_unitOfWork = (UnitOfWork)unitOfWork;
16+
_deviceModelRepository = _unitOfWork.DeviceModelRepository;
1817
_brandRepository = _unitOfWork.BrandRepository;
1918
}
2019

@@ -24,18 +23,18 @@ public async Task<Result<bool>> CreateDeviceModel(Models.DeviceModel deviceModel
2423
if (!checkResult.IsSuccessful)
2524
return checkResult;
2625

27-
await _repository.CreateAsync(deviceModel);
26+
await _deviceModelRepository.CreateAsync(deviceModel);
2827
return await _unitOfWork.CommitAsync();
2928
}
3029

3130
public async Task<Models.DeviceModel> GetDeviceModelById(int id)
3231
{
33-
return await _repository.GetAsync(id);
32+
return await _deviceModelRepository.GetAsync(id);
3433
}
3534

3635
public async Task<List<Models.DeviceModel>> GetAllDeviceModels()
3736
{
38-
return await _repository.GetWhere(null, "Brand");
37+
return await _deviceModelRepository.GetWhere(null, "Brand");
3938
}
4039

4140
public async Task<Result<bool>> UpdateDeviceModel(Models.DeviceModel deviceModel)
@@ -44,19 +43,19 @@ public async Task<Result<bool>> UpdateDeviceModel(Models.DeviceModel deviceModel
4443
if (!checkResult.IsSuccessful)
4544
return checkResult;
4645

47-
_repository.Update(deviceModel);
46+
_deviceModelRepository.Update(deviceModel);
4847
return await _unitOfWork.CommitAsync();
4948
}
5049

5150
public Task<Result<bool>> DeleteDeviceModel(int id)
5251
{
53-
_repository.DeleteAsync(id);
52+
_deviceModelRepository.DeleteAsync(id);
5453
return _unitOfWork.CommitAsync();
5554
}
5655

5756
public async Task<List<Models.DeviceModel>> GetDeviceModelsByBrandId(int brandId)
5857
{
59-
return await _repository.GetWhere(m => m.BrandId == brandId);
58+
return await _deviceModelRepository.GetWhere(m => m.BrandId == brandId);
6059
}
6160

6261
public async Task<Result<bool>> CheckIncludes(Models.DeviceModel deviceModel)

Services/Maintenance/MaintenanceRecordService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using TechInventory.Models;
22
using TechInventory.Data.UnitOfWork;
3-
using TechInventory.Data.Context;
43
using TechInventory.Data.Repository;
54

65
namespace TechInventory.Services.Maintenance;
@@ -9,13 +8,13 @@ public class MaintenanceRecordService : IMaintenanceRecordService
98
{
109
private readonly UnitOfWork _unitOfWork;
1110
private readonly IRepository<MaintenanceRecord> _repository;
12-
private readonly IRepository<Models.Device> _deviceRepository;
11+
private readonly IRepository<Models.Device> _deviceRepository; // This is correct
1312

14-
public MaintenanceRecordService(InventoryDbContext context)
13+
public MaintenanceRecordService(IUnitOfWork unitOfWork)
1514
{
16-
_unitOfWork = new UnitOfWork(context);
15+
_unitOfWork = (UnitOfWork)unitOfWork;
1716
_repository = _unitOfWork.MaintenanceRecordRepository;
18-
_deviceRepository = _unitOfWork.DeviceRepository;
17+
_deviceRepository = _unitOfWork.DeviceRepository; // This is correct
1918
}
2019

2120
public async Task<Result<bool>> CreateRecord(MaintenanceRecord record)

0 commit comments

Comments
 (0)