ุดูู ุงูู Output ุจุชุงุนู
- ูู ูููุช ููู Nestedุ ุงุณุฃู ููุณู: ูู ู ุญุชุงุฌู ููุง ูุฃุ
- ุบุงูุจูุง ู ุด ููุญุชุงุฌ ูุธูุฑ ุงูู Id ู ุฑุชูู ุฃู ุฃูุชุฑุ ุฒู ูู ุญุงูุฉ ุงูู Brand ุฃู ุงูู Category.
- ูุนูู ุจุฏู ู ุง ูุธูุฑ ุงูู Id ู ุน ุงูุงุณู ุ ู ู ูู ูุธูุฑ ุงูุงุณู ุจุณ ุนุดุงู ูุณูู ุงูุดุบู ุนูู ุงูู Front-end developer.
- ุฏุงูู ูุง ุฎููู ูู ุงูู Output Flat ูู ู ููุด ุฏุงุนู ููู Nesting.
View Model vs Model
- ุงูููุฑุฉ ููุง ุฅู ุงูู ุดุงูู ุงููู ุนูุฏูุง ูู ุงูู Back ู ุง ุชุฎุตุด ุงูู Front-end.
- ุนุดุงู ูุฏู ุจูุจุนุช View Model ุจุฏู ู ุง ูุจุนุช ุงูู Model ููุณู.
- ูู ูุธุงู
ุงูู MVC:
- ุจูุนู ู Map ู ู ุงูู Model ูู View Model ูู ุงูู Get.
- ูู ู ุงูู View Model ูู Model ูู ุงูู Create, Update, ู Delete.
ุงููุฑู ุจูู View Model ู DTO
- ูู ุงูู API ู ููุด ุญุงุฌุฉ ุงุณู ูุง View Modelุ ููู ููู ุญุงุฌุฉ ุงุณู ูุง DTO (Data Transfer Object).
- ุงูู DTO ุจุชููู ุงููุณูุท ุจูู ุงูู Back ูุงูู Front:
- ุงููGet: ู ู ุงูู Back ููู Front.
- ุงููCreate, Update, Delete: ู ู ุงูู Front ููู Back.
ุฎุทูุงุช ุฅูุดุงุก ุงูู DTO
- ุฅุถุงูุฉ ูููุฏุฑ Dtos:
- ูู ุงูู API ูุนู
ู ูููุฏุฑ ุงุณู
ู
Dtos
. - ูุนู
ู ููุงุณ ุงุณู
ู
ProductToReturnDto
.
- ูู ุงูู API ูุนู
ู ูููุฏุฑ ุงุณู
ู
- ุฅุนุฏุงุฏ ุงูู
ProductToReturnDto
:- ูุงุฎุฏ ุงูุญุงุฌุงุช ุงููู ู ุญุชุงุฌูููุง ู ู ุงูู Product ููุญุทูุง ูู ุงูููุงุณ.
- ุงูููุงุณ ู
ุด ูููุฑุซ ู
ู ุงูู
BaseEntity
. - ูุถูู ุงูู Properties ุจุงูุดูู ุงูุชุงูู:
public class ProductToReturnDto
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public decimal Price { get; set; }
public string PictureUrl { get; set; }
public string Brand { get; set; }
public int BrandId { get; set; }
public string Category { get; set; }
public int CategoryId { get; set; }
}
- ุฅุถุงูุฉ
AutoMapper
:- ูุนู
ู Install ููู
AutoMapper
ูู ู ุดุฑูุน ุงูู API. - ูุญุทูุง ูู ุงูู Services:
- ูุนู
ู Install ููู
builder.Services.AddAutoMapper(typeof(MappingProfiles));
AutoMapper.Extensions.Microsoft.DependencyInjection
ุฅุนุฏุงุฏ Mapping Profiles
- ุฅูุดุงุก ููุงุณ
MappingProfiles
:- ูุฑูุญ ูู ูููุฏุฑ ุงูู
Dtos
ููุนู ู ูููุฏุฑ ุฌุฏูุฏ ุงุณู ูHelpers
. - ูุนู
ู ููุงุณ ุงุณู
ู
MappingProfiles
ูููุฑุซ ู ู ููุงุณProfile
.
- ูุฑูุญ ูู ูููุฏุฑ ุงูู
- ุฅุนุฏุงุฏ ุงูู
ุงุจ:
- ูู ุงูููุงุณ ูุนู
ู Map ุจูู ุงูู Product ูุงูู
ProductToReturnDto
:
- ูู ุงูููุงุณ ูุนู
ู Map ุจูู ุงูู Product ูุงูู
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Product, ProductToReturnDto>()
.ForMember(d => d.Brand, O => O.MapFrom(s => s.Brand.Name))
.ForMember(d => d.Category, O => O.MapFrom(s => s.Category.Name));
}
}
- ู ู ุงูู Destination ุฑูุญ ุงุนู ู Map ู ู ุงูู Source ูุงุญุฏุฏ ุงูู ุจุงูุธุจุท
ุชุนุฏูู ุงูู Controller
- ุฅุถุงูุฉ IMapper:
- ูุนุฏู ุงูู Constructor ุจุชุงุน ุงูู Controller ุนุดุงู ููุจู ุงูู IMapper:
private readonly IGenericRepository<Product> _productsRepo;
private readonly IMapper _mapper;
public ProductsController(IGenericRepository<Product> productsRepo, IMapper mapper)
{
_productsRepo = productsRepo;
_mapper = mapper;
}
- GetProduct:
- ุฅุถุงูุฉ ู ูุซูุฏ ููู Get ุนุดุงู ุชุฑุฌูุน ุงูู DTO:
[HttpGet("{id}")]
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
{
var product = await _productsRepo.GetAsync(id);
if (product == null)
return NotFound(new { message = "Not Found", statusCode = 404 });
return Ok(_mapper.Map<Product, ProductToReturnDto>(product));
}
- GetProducts:
- ู ูุซูุฏ ุชุฑุฌูุน ูู ุงูู ูุชุฌุงุช ุจู DTO:
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductToReturnDto>>> GetProducts()
{
var products = await _productsRepo.GetAllWithSpecAsync(new ProductWithBrandAndCategorySpecifications());
if (products == null || !products.Any())
return NotFound("No Products available");
return Ok(_mapper.Map<IEnumerable<Product>, IEnumerable<ProductToReturnDto>>(products));
}