Skip to content

Commit 8cc6860

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

5 files changed

Lines changed: 46 additions & 16 deletions

File tree

Program.cs

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

18-
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
1918
builder.Services.AddScoped<IDeviceService, DeviceService>();
2019
builder.Services.AddScoped<IDeviceModelService, DeviceModelService>();
2120
builder.Services.AddScoped<IBrandService, BrandService>();

Services/Brand/BrandService.cs

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

56
namespace TechInventory.Services.Brand;
67

7-
public class BrandService(IUnitOfWork unitOfWork) : IBrandService
8+
public class BrandService : IBrandService
89
{
9-
private readonly IUnitOfWork _unitOfWork = unitOfWork;
10-
private readonly IRepository<Models.Brand> _repository = unitOfWork.BrandRepository;
10+
private readonly UnitOfWork _unitOfWork;
11+
private readonly IRepository<Models.Brand> _repository;
12+
13+
public BrandService(InventoryDbContext context)
14+
{
15+
_unitOfWork = new UnitOfWork(context);
16+
_repository = _unitOfWork.BrandRepository;
17+
}
1118

1219
public async Task<Result<bool>> CreateBrand(Models.Brand brand)
1320
{

Services/Device/DeviceService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
using TechInventory.Models;
22
using TechInventory.Data.Repository;
33
using TechInventory.Data.UnitOfWork;
4+
using TechInventory.Data.Context;
45

56
namespace TechInventory.Services.Device;
67

7-
public class DeviceService(IUnitOfWork unitOfWork) : IDeviceService
8+
public class DeviceService : IDeviceService
89
{
9-
private readonly IUnitOfWork _unitOfWork = unitOfWork;
10-
private readonly IRepository<Models.Device> _repository = unitOfWork.DeviceRepository;
11-
private readonly IRepository<Models.DeviceModel> _modelRepository = unitOfWork.DeviceModelRepository;
10+
private readonly UnitOfWork _unitOfWork;
11+
private readonly IRepository<Models.Device> _repository;
12+
private readonly IRepository<Models.DeviceModel> _modelRepository;
13+
14+
public DeviceService(InventoryDbContext context)
15+
{
16+
_unitOfWork = new UnitOfWork(context);
17+
_repository = _unitOfWork.DeviceRepository;
18+
_modelRepository = _unitOfWork.DeviceModelRepository;
19+
}
1220

1321
public async Task<Result<bool>> AddDevice(Models.Device device)
1422
{

Services/DeviceModel/DeviceModelService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
using TechInventory.Data.UnitOfWork;
22
using TechInventory.Data.Repository;
3+
using TechInventory.Data.Context;
34
using TechInventory.Models;
45

56
namespace TechInventory.Services.DeviceModel;
67

7-
public class DeviceModelService(IUnitOfWork unitOfWork) : IDeviceModelService
8+
public class DeviceModelService : IDeviceModelService
89
{
9-
private readonly IUnitOfWork _unitOfWork = unitOfWork;
10-
private readonly IRepository<Models.DeviceModel> _repository = unitOfWork.DeviceModelRepository;
11-
private readonly IRepository<Models.Brand> _brandRepository = unitOfWork.BrandRepository;
10+
private readonly UnitOfWork _unitOfWork;
11+
private readonly IRepository<Models.DeviceModel> _repository;
12+
private readonly IRepository<Models.Brand> _brandRepository;
13+
14+
public DeviceModelService(InventoryDbContext context)
15+
{
16+
_unitOfWork = new UnitOfWork(context);
17+
_repository = _unitOfWork.DeviceModelRepository;
18+
_brandRepository = _unitOfWork.BrandRepository;
19+
}
1220

1321
public async Task<Result<bool>> CreateDeviceModel(Models.DeviceModel deviceModel)
1422
{

Services/Maintenance/MaintenanceRecordService.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
using TechInventory.Models;
22
using TechInventory.Data.UnitOfWork;
3+
using TechInventory.Data.Context;
34
using TechInventory.Data.Repository;
45

56
namespace TechInventory.Services.Maintenance;
67

7-
public class MaintenanceRecordService(IUnitOfWork unitOfWork) : IMaintenanceRecordService
8+
public class MaintenanceRecordService : IMaintenanceRecordService
89
{
9-
private readonly IUnitOfWork _unitOfWork = unitOfWork;
10-
private readonly IRepository<MaintenanceRecord> _repository = unitOfWork.MaintenanceRecordRepository;
11-
private readonly IRepository<Models.Device> _deviceRepository = unitOfWork.DeviceRepository;
10+
private readonly UnitOfWork _unitOfWork;
11+
private readonly IRepository<MaintenanceRecord> _repository;
12+
private readonly IRepository<Models.Device> _deviceRepository;
13+
14+
public MaintenanceRecordService(InventoryDbContext context)
15+
{
16+
_unitOfWork = new UnitOfWork(context);
17+
_repository = _unitOfWork.MaintenanceRecordRepository;
18+
_deviceRepository = _unitOfWork.DeviceRepository;
19+
}
1220

1321
public async Task<Result<bool>> CreateRecord(MaintenanceRecord record)
1422
{

0 commit comments

Comments
 (0)