From 6caf3a4c0790bf08cfdc8f2447c0f71f9d7e6327 Mon Sep 17 00:00:00 2001 From: OlgunR Date: Wed, 14 Jan 2026 15:01:13 +0100 Subject: [PATCH] Refactor to use VwmyCatalog as primary catalog entity Replaced all usage of the Catalog entity with VwmyCatalog across application, domain, and infrastructure layers. Updated AutoMapper profiles, repository interfaces, and service logic to use VwmyCatalog. Removed the obsolete Catalog entity and related mapping profiles. Moved VwmyCatalog to the Domain.Entities namespace and cleaned up project structure. This change simplifies the domain model and eliminates redundant entity definitions. --- .../Catalogs/CatalogProfile.cs | 6 +-- .../Catalogs/CatalogService.cs | 50 +++++++++---------- DbFirst.Domain/DbFirst.Domain.csproj | 1 - DbFirst.Domain/DomainEntities/Catalog.cs | 12 ----- .../Entities}/VwmyCatalog.cs | 2 +- .../Repositories/ICatalogRepository.cs | 12 ++--- .../ApplicationDbContext.cs | 2 +- .../Mappings/CatalogInfrastructureProfile.cs | 13 ----- .../Repositories/CatalogRepository.cs | 29 ++++------- 9 files changed, 47 insertions(+), 80 deletions(-) delete mode 100644 DbFirst.Domain/DomainEntities/Catalog.cs rename {DbFirst.Infrastructure/ScaffoldEntities => DbFirst.Domain/Entities}/VwmyCatalog.cs (87%) delete mode 100644 DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs diff --git a/DbFirst.Application/Catalogs/CatalogProfile.cs b/DbFirst.Application/Catalogs/CatalogProfile.cs index d16f8ee..d30bd02 100644 --- a/DbFirst.Application/Catalogs/CatalogProfile.cs +++ b/DbFirst.Application/Catalogs/CatalogProfile.cs @@ -1,5 +1,5 @@ using AutoMapper; -using DbFirst.Domain.DomainEntities; +using DbFirst.Domain.Entities; namespace DbFirst.Application.Catalogs; @@ -7,7 +7,7 @@ public class CatalogProfile : Profile { public CatalogProfile() { - CreateMap().ReverseMap(); - CreateMap(); + CreateMap().ReverseMap(); + CreateMap(); } } diff --git a/DbFirst.Application/Catalogs/CatalogService.cs b/DbFirst.Application/Catalogs/CatalogService.cs index d4488c2..ad28e69 100644 --- a/DbFirst.Application/Catalogs/CatalogService.cs +++ b/DbFirst.Application/Catalogs/CatalogService.cs @@ -1,6 +1,6 @@ using AutoMapper; -using DbFirst.Domain.DomainEntities; using DbFirst.Domain.Repositories; +using DbFirst.Domain.Entities; namespace DbFirst.Application.Catalogs; @@ -17,25 +17,25 @@ public class CatalogService : ICatalogService public async Task> GetAllAsync(CancellationToken cancellationToken = default) { - var domainItems = await _repository.GetAllAsync(cancellationToken); - return _mapper.Map>(domainItems); + var items = await _repository.GetAllAsync(cancellationToken); + return _mapper.Map>(items); } public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { - var domainItem = await _repository.GetByIdAsync(id, cancellationToken); - return domainItem == null ? null : _mapper.Map(domainItem); + var item = await _repository.GetByIdAsync(id, cancellationToken); + return item == null ? null : _mapper.Map(item); } public async Task CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) { - var domainItem = _mapper.Map(dto); - domainItem.AddedWho = "system"; - domainItem.AddedWhen = DateTime.UtcNow; - domainItem.ChangedWho = "system"; - domainItem.ChangedWhen = DateTime.UtcNow; + var entity = _mapper.Map(dto); + entity.AddedWho = "system"; + entity.AddedWhen = DateTime.UtcNow; + entity.ChangedWho = "system"; + entity.ChangedWhen = DateTime.UtcNow; - var created = await _repository.AddAsync(domainItem, cancellationToken); + var created = await _repository.AddAsync(entity, cancellationToken); return _mapper.Map(created); } @@ -47,13 +47,13 @@ public class CatalogService : ICatalogService return false; } - var domainItem = _mapper.Map(dto); - domainItem.Guid = id; - domainItem.AddedWho = existing.AddedWho; - domainItem.AddedWhen = existing.AddedWhen; - domainItem.ChangedWho = "system"; - domainItem.ChangedWhen = DateTime.UtcNow; - return await _repository.UpdateAsync(id, domainItem, cancellationToken); + var entity = _mapper.Map(dto); + entity.Guid = id; + entity.AddedWho = existing.AddedWho; + entity.AddedWhen = existing.AddedWhen; + entity.ChangedWho = "system"; + entity.ChangedWhen = DateTime.UtcNow; + return await _repository.UpdateAsync(id, entity, cancellationToken); } public async Task UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default) @@ -64,14 +64,14 @@ public class CatalogService : ICatalogService return null; } - var domainItem = _mapper.Map(dto); - domainItem.Guid = id; - domainItem.AddedWho = existing.AddedWho; - domainItem.AddedWhen = existing.AddedWhen; - domainItem.ChangedWho = "system"; - domainItem.ChangedWhen = DateTime.UtcNow; + var entity = _mapper.Map(dto); + entity.Guid = id; + entity.AddedWho = existing.AddedWho; + entity.AddedWhen = existing.AddedWhen; + entity.ChangedWho = "system"; + entity.ChangedWhen = DateTime.UtcNow; - var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken); + var updated = await _repository.UpdateWithStoredProcedureAsync(entity, cancellationToken); return updated == null ? null : _mapper.Map(updated); } diff --git a/DbFirst.Domain/DbFirst.Domain.csproj b/DbFirst.Domain/DbFirst.Domain.csproj index 4078f60..bbf0ab3 100644 --- a/DbFirst.Domain/DbFirst.Domain.csproj +++ b/DbFirst.Domain/DbFirst.Domain.csproj @@ -7,7 +7,6 @@ - diff --git a/DbFirst.Domain/DomainEntities/Catalog.cs b/DbFirst.Domain/DomainEntities/Catalog.cs deleted file mode 100644 index efbde28..0000000 --- a/DbFirst.Domain/DomainEntities/Catalog.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace DbFirst.Domain.DomainEntities; - -public class Catalog -{ - public int Guid { get; set; } - public string CatTitle { get; set; } = null!; - public string CatString { get; set; } = null!; - public string AddedWho { get; set; } = null!; - public DateTime AddedWhen { get; set; } - public string? ChangedWho { get; set; } - public DateTime? ChangedWhen { get; set; } -} diff --git a/DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs b/DbFirst.Domain/Entities/VwmyCatalog.cs similarity index 87% rename from DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs rename to DbFirst.Domain/Entities/VwmyCatalog.cs index 973a885..503bc7d 100644 --- a/DbFirst.Infrastructure/ScaffoldEntities/VwmyCatalog.cs +++ b/DbFirst.Domain/Entities/VwmyCatalog.cs @@ -1,4 +1,4 @@ -namespace DbFirst.Infrastructure.ScaffoldEntities; +namespace DbFirst.Domain.Entities; public partial class VwmyCatalog { diff --git a/DbFirst.Domain/Repositories/ICatalogRepository.cs b/DbFirst.Domain/Repositories/ICatalogRepository.cs index a65b48d..922d7c0 100644 --- a/DbFirst.Domain/Repositories/ICatalogRepository.cs +++ b/DbFirst.Domain/Repositories/ICatalogRepository.cs @@ -1,14 +1,14 @@ -using DbFirst.Domain.DomainEntities; +using DbFirst.Domain.Entities; namespace DbFirst.Domain.Repositories; public interface ICatalogRepository { - Task> GetAllAsync(CancellationToken cancellationToken = default); - Task GetByIdAsync(int id, CancellationToken cancellationToken = default); - Task AddAsync(Catalog catalog, CancellationToken cancellationToken = default); - Task UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default); - Task UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default); + Task> GetAllAsync(CancellationToken cancellationToken = default); + Task GetByIdAsync(int id, CancellationToken cancellationToken = default); + Task AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); + Task UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default); + Task UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default); Task DeleteAsync(int id, CancellationToken cancellationToken = default); Task DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default); } diff --git a/DbFirst.Infrastructure/ApplicationDbContext.cs b/DbFirst.Infrastructure/ApplicationDbContext.cs index 7065533..e8825bb 100644 --- a/DbFirst.Infrastructure/ApplicationDbContext.cs +++ b/DbFirst.Infrastructure/ApplicationDbContext.cs @@ -1,4 +1,4 @@ -using DbFirst.Infrastructure.ScaffoldEntities; +using DbFirst.Domain.Entities; using Microsoft.EntityFrameworkCore; namespace DbFirst.Infrastructure; diff --git a/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs b/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs deleted file mode 100644 index 887e621..0000000 --- a/DbFirst.Infrastructure/Mappings/CatalogInfrastructureProfile.cs +++ /dev/null @@ -1,13 +0,0 @@ -using AutoMapper; -using DbFirst.Domain.DomainEntities; -using DbFirst.Infrastructure.ScaffoldEntities; - -namespace DbFirst.Infrastructure.Mappings; - -public class CatalogInfrastructureProfile : Profile -{ - public CatalogInfrastructureProfile() - { - CreateMap(); - } -} diff --git a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs index 293d2b2..c304db4 100644 --- a/DbFirst.Infrastructure/Repositories/CatalogRepository.cs +++ b/DbFirst.Infrastructure/Repositories/CatalogRepository.cs @@ -1,7 +1,5 @@ -using AutoMapper; -using DbFirst.Domain.DomainEntities; using DbFirst.Domain.Repositories; -using DbFirst.Infrastructure.ScaffoldEntities; +using DbFirst.Domain.Entities; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using System.Data; @@ -11,27 +9,23 @@ namespace DbFirst.Infrastructure.Repositories; public class CatalogRepository : ICatalogRepository { private readonly ApplicationDbContext _db; - private readonly IMapper _mapper; - public CatalogRepository(ApplicationDbContext db, IMapper mapper) + public CatalogRepository(ApplicationDbContext db) { _db = db; - _mapper = mapper; } - public async Task> GetAllAsync(CancellationToken cancellationToken = default) + public async Task> GetAllAsync(CancellationToken cancellationToken = default) { - var viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken); - return _mapper.Map>(viewRows); + return await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken); } - public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) + public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) { - var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); - return viewRow == null ? null : _mapper.Map(viewRow); + return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); } - public async Task AddAsync(Catalog catalog, CancellationToken cancellationToken = default) + public async Task AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default) { var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken); if (created == null) @@ -42,7 +36,7 @@ public class CatalogRepository : ICatalogRepository return created; } - public async Task UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default) + public async Task UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default) { var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken); if (!existing) @@ -55,7 +49,7 @@ public class CatalogRepository : ICatalogRepository return updated != null; } - public async Task UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default) + public async Task UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default) { if (catalog.Guid == 0) { @@ -93,7 +87,7 @@ public class CatalogRepository : ICatalogRepository return await DeleteAsync(id, cancellationToken); } - private async Task UpsertWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken) + private async Task UpsertWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken) { var guidParam = new SqlParameter("@GUID", SqlDbType.Int) { @@ -116,7 +110,6 @@ public class CatalogRepository : ICatalogRepository } var guid = (int)guidParam.Value; - var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken); - return viewRow == null ? null : _mapper.Map(viewRow); + return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken); } }