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.
This commit is contained in:
OlgunR
2026-01-14 15:01:13 +01:00
parent ccd97fe9d6
commit 6caf3a4c07
9 changed files with 47 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
using AutoMapper; using AutoMapper;
using DbFirst.Domain.DomainEntities; using DbFirst.Domain.Entities;
namespace DbFirst.Application.Catalogs; namespace DbFirst.Application.Catalogs;
@@ -7,7 +7,7 @@ public class CatalogProfile : Profile
{ {
public CatalogProfile() public CatalogProfile()
{ {
CreateMap<Catalog, CatalogReadDto>().ReverseMap(); CreateMap<VwmyCatalog, CatalogReadDto>().ReverseMap();
CreateMap<CatalogWriteDto, Catalog>(); CreateMap<CatalogWriteDto, VwmyCatalog>();
} }
} }

View File

@@ -1,6 +1,6 @@
using AutoMapper; using AutoMapper;
using DbFirst.Domain.DomainEntities;
using DbFirst.Domain.Repositories; using DbFirst.Domain.Repositories;
using DbFirst.Domain.Entities;
namespace DbFirst.Application.Catalogs; namespace DbFirst.Application.Catalogs;
@@ -17,25 +17,25 @@ public class CatalogService : ICatalogService
public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default) public async Task<List<CatalogReadDto>> GetAllAsync(CancellationToken cancellationToken = default)
{ {
var domainItems = await _repository.GetAllAsync(cancellationToken); var items = await _repository.GetAllAsync(cancellationToken);
return _mapper.Map<List<CatalogReadDto>>(domainItems); return _mapper.Map<List<CatalogReadDto>>(items);
} }
public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default) public async Task<CatalogReadDto?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{ {
var domainItem = await _repository.GetByIdAsync(id, cancellationToken); var item = await _repository.GetByIdAsync(id, cancellationToken);
return domainItem == null ? null : _mapper.Map<CatalogReadDto>(domainItem); return item == null ? null : _mapper.Map<CatalogReadDto>(item);
} }
public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default) public async Task<CatalogReadDto> CreateAsync(CatalogWriteDto dto, CancellationToken cancellationToken = default)
{ {
var domainItem = _mapper.Map<Catalog>(dto); var entity = _mapper.Map<VwmyCatalog>(dto);
domainItem.AddedWho = "system"; entity.AddedWho = "system";
domainItem.AddedWhen = DateTime.UtcNow; entity.AddedWhen = DateTime.UtcNow;
domainItem.ChangedWho = "system"; entity.ChangedWho = "system";
domainItem.ChangedWhen = DateTime.UtcNow; entity.ChangedWhen = DateTime.UtcNow;
var created = await _repository.AddAsync(domainItem, cancellationToken); var created = await _repository.AddAsync(entity, cancellationToken);
return _mapper.Map<CatalogReadDto>(created); return _mapper.Map<CatalogReadDto>(created);
} }
@@ -47,13 +47,13 @@ public class CatalogService : ICatalogService
return false; return false;
} }
var domainItem = _mapper.Map<Catalog>(dto); var entity = _mapper.Map<VwmyCatalog>(dto);
domainItem.Guid = id; entity.Guid = id;
domainItem.AddedWho = existing.AddedWho; entity.AddedWho = existing.AddedWho;
domainItem.AddedWhen = existing.AddedWhen; entity.AddedWhen = existing.AddedWhen;
domainItem.ChangedWho = "system"; entity.ChangedWho = "system";
domainItem.ChangedWhen = DateTime.UtcNow; entity.ChangedWhen = DateTime.UtcNow;
return await _repository.UpdateAsync(id, domainItem, cancellationToken); return await _repository.UpdateAsync(id, entity, cancellationToken);
} }
public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default) public async Task<CatalogReadDto?> UpdateWithStoredProcedureAsync(int id, CatalogWriteDto dto, CancellationToken cancellationToken = default)
@@ -64,14 +64,14 @@ public class CatalogService : ICatalogService
return null; return null;
} }
var domainItem = _mapper.Map<Catalog>(dto); var entity = _mapper.Map<VwmyCatalog>(dto);
domainItem.Guid = id; entity.Guid = id;
domainItem.AddedWho = existing.AddedWho; entity.AddedWho = existing.AddedWho;
domainItem.AddedWhen = existing.AddedWhen; entity.AddedWhen = existing.AddedWhen;
domainItem.ChangedWho = "system"; entity.ChangedWho = "system";
domainItem.ChangedWhen = DateTime.UtcNow; entity.ChangedWhen = DateTime.UtcNow;
var updated = await _repository.UpdateWithStoredProcedureAsync(domainItem, cancellationToken); var updated = await _repository.UpdateWithStoredProcedureAsync(entity, cancellationToken);
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated); return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
} }

View File

@@ -7,7 +7,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="DomainEntities\" />
<Folder Include="Repositories\" /> <Folder Include="Repositories\" />
</ItemGroup> </ItemGroup>

View File

@@ -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; }
}

View File

@@ -1,4 +1,4 @@
namespace DbFirst.Infrastructure.ScaffoldEntities; namespace DbFirst.Domain.Entities;
public partial class VwmyCatalog public partial class VwmyCatalog
{ {

View File

@@ -1,14 +1,14 @@
using DbFirst.Domain.DomainEntities; using DbFirst.Domain.Entities;
namespace DbFirst.Domain.Repositories; namespace DbFirst.Domain.Repositories;
public interface ICatalogRepository public interface ICatalogRepository
{ {
Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default); Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default);
Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default); Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default);
Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default); Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default); Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default); Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default);
Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default); Task<bool> DeleteAsync(int id, CancellationToken cancellationToken = default);
Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default); Task<bool> DeleteWithStoredProcedureAsync(int id, CancellationToken cancellationToken = default);
} }

View File

@@ -1,4 +1,4 @@
using DbFirst.Infrastructure.ScaffoldEntities; using DbFirst.Domain.Entities;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace DbFirst.Infrastructure; namespace DbFirst.Infrastructure;

View File

@@ -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<VwmyCatalog, Catalog>();
}
}

View File

@@ -1,7 +1,5 @@
using AutoMapper;
using DbFirst.Domain.DomainEntities;
using DbFirst.Domain.Repositories; using DbFirst.Domain.Repositories;
using DbFirst.Infrastructure.ScaffoldEntities; using DbFirst.Domain.Entities;
using Microsoft.Data.SqlClient; using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Data; using System.Data;
@@ -11,27 +9,23 @@ namespace DbFirst.Infrastructure.Repositories;
public class CatalogRepository : ICatalogRepository public class CatalogRepository : ICatalogRepository
{ {
private readonly ApplicationDbContext _db; private readonly ApplicationDbContext _db;
private readonly IMapper _mapper;
public CatalogRepository(ApplicationDbContext db, IMapper mapper) public CatalogRepository(ApplicationDbContext db)
{ {
_db = db; _db = db;
_mapper = mapper;
} }
public async Task<List<Catalog>> GetAllAsync(CancellationToken cancellationToken = default) public async Task<List<VwmyCatalog>> GetAllAsync(CancellationToken cancellationToken = default)
{ {
var viewRows = await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken); return await _db.VwmyCatalogs.AsNoTracking().ToListAsync(cancellationToken);
return _mapper.Map<List<Catalog>>(viewRows);
} }
public async Task<Catalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default) public async Task<VwmyCatalog?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{ {
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken); return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == id, cancellationToken);
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
} }
public async Task<Catalog> AddAsync(Catalog catalog, CancellationToken cancellationToken = default) public async Task<VwmyCatalog> AddAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
{ {
var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken); var created = await UpsertWithStoredProcedureAsync(catalog, cancellationToken);
if (created == null) if (created == null)
@@ -42,7 +36,7 @@ public class CatalogRepository : ICatalogRepository
return created; return created;
} }
public async Task<bool> UpdateAsync(int id, Catalog catalog, CancellationToken cancellationToken = default) public async Task<bool> UpdateAsync(int id, VwmyCatalog catalog, CancellationToken cancellationToken = default)
{ {
var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken); var existing = await _db.VwmyCatalogs.AsNoTracking().AnyAsync(x => x.Guid == id, cancellationToken);
if (!existing) if (!existing)
@@ -55,7 +49,7 @@ public class CatalogRepository : ICatalogRepository
return updated != null; return updated != null;
} }
public async Task<Catalog?> UpdateWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken = default) public async Task<VwmyCatalog?> UpdateWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken = default)
{ {
if (catalog.Guid == 0) if (catalog.Guid == 0)
{ {
@@ -93,7 +87,7 @@ public class CatalogRepository : ICatalogRepository
return await DeleteAsync(id, cancellationToken); return await DeleteAsync(id, cancellationToken);
} }
private async Task<Catalog?> UpsertWithStoredProcedureAsync(Catalog catalog, CancellationToken cancellationToken) private async Task<VwmyCatalog?> UpsertWithStoredProcedureAsync(VwmyCatalog catalog, CancellationToken cancellationToken)
{ {
var guidParam = new SqlParameter("@GUID", SqlDbType.Int) var guidParam = new SqlParameter("@GUID", SqlDbType.Int)
{ {
@@ -116,7 +110,6 @@ public class CatalogRepository : ICatalogRepository
} }
var guid = (int)guidParam.Value; var guid = (int)guidParam.Value;
var viewRow = await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken); return await _db.VwmyCatalogs.AsNoTracking().FirstOrDefaultAsync(x => x.Guid == guid, cancellationToken);
return viewRow == null ? null : _mapper.Map<Catalog>(viewRow);
} }
} }