How to use repository in entity for validation or other situation? #10472
-
I want to return product name in Lock methord of stock entity. How should deal with the _productRepository ?? public class stock:entity
{
public async Task Lock(Zion.ERP.Domain.Stock.Stock stock, int qty, string batchNo)
{
var item = stock.Details.Single(t => t.BatchNo == batchNo);
if (qty > stock.AvalableQty)
{
var prod = (await _productRepository.WithDetailsAsync(t => t.ProductTemplate)).Single(t => t.Id == stock.ProductId);
throw new ZionCommonException($"product{prod.ProductTemplate.Name} [{prod.ProductTemplate.MainOE}],batch:{batchNo} is not enough");
}
stock.LockedQty += qty;
item.LockQty(qty);
}
}
``` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @CAH-FlyChen, there are two things you can do,
public class stock:entity
{
public async Task Lock(IProductRepository productRepository, Stock stock, int qty, string batchNo)
{
var item = stock.Details.Single(t => t.BatchNo == batchNo);
if (qty > stock.AvalableQty)
{
var prod = (await productRepository.WithDetailsAsync(t => t.ProductTemplate)).Single(t => t.Id == stock.ProductId);
throw new ZionCommonException($"product{prod.ProductTemplate.Name} [{prod.ProductTemplate.MainOE}],batch:{batchNo} is not enough");
}
stock.LockedQty += qty;
item.LockQty(qty);
}
}
public class stock:entity
{
//remove the lock method and make related properties' setter as `internal set`
public int LockedQty { get; internal set; } //internal set -> ensure only can be set in Domain Layer
}
public class StockManager: DomainService
{
private readonly IProductRepository _productRepository;
public IssueManager(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public async Task Lock(Stock stock, int qty, string batchNo)
{
var item = stock.Details.Single(t => t.BatchNo == batchNo);
if (qty > stock.AvalableQty)
{
var prod = (await _productRepository.WithDetailsAsync(t => t.ProductTemplate)).Single(t => t.Id == stock.ProductId);
throw new ZionCommonException($"product{prod.ProductTemplate.Name} [{prod.ProductTemplate.MainOE}],batch:{batchNo} is not enough");
}
stock.LockedQty += qty;
item.LockQty(qty);
}
}
Specification Pattern is used to define named, reusable, combinable and testable filters for entities and other business objects.
|
Beta Was this translation helpful? Give feedback.
Hi @CAH-FlyChen, there are two things you can do,
IProductRepository
interface as a parameter to the Lock method