Untitled

mail@pastecode.io avatar
unknown
plain_text
4 years ago
1.2 kB
3
Indexable
Never
public class ProductDao() {
    private readonly DbContext _dbContext;

    public void InsertProduct(EntityObjectNotDto dto) {
        _dbContext.Product.Add(product);
        _dbContext.SaveChanges();
    }

    public void InsertProductDetail(EntityObjectNotDto dto) {
        _dbContext.ProductDetail.Add(productDetail);
        _dbContext.SaveChanges();
    }
}

public class ProductFacade() {
    private readonly ProductMapper _mapper;
    private readOnly ProductDao _productDao;
    
    // this is a facade method that will do all logics
    @Transactional // <-- rollback if anything fails
    public void AddProduct(AddProductDto dto) {
        var product = _mapper.MapToProduct(dto);
        var productDetail = _mapper.MapToProductDetail(dto);
        
        // Dao layer doesn't need your DTO. pass only Entity to dao layer
        _productDao.InsertProduct(_mapper.MapToEntity(product));
        _productDao.InsertProductDetail(_mapper.MapToEntity(productDetail));
    }
}

public class ProductController() {
    private readonly ProductFacade _productFacade;
    
    @PostMapping("/products")
    public void addProduct() {
        _productFacade.addProduct(productFromThinAir);
    }
}