Tôi có lớp cơ sở "Sản phẩm", một số lớp khác "ProductBookDetail", "ProductDVDDetail" kế thừa từ lớp này. Tôi sử dụng một lớp ProductService để thực hiện thao tác trên các lớp này. Nhưng, tôi phải làm một số kiểm tra tùy thuộc vào loại (ISBN cho Sách, ngôn ngữ cho DVD). Tôi muốn biết cách tốt nhất để truyền giá trị "productDetail", tôi nhận được trong SaveOrupdate. Tôi cố gắng GetType() và dàn diễn viên với (ProductBookDetail)productDetail
nhưng điều đó không làm việcGeneric <T> cách truyền?
Cảm ơn,
var productDetail = new ProductDetailBook() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailBook>>();
service.SaveOrUpdate(productDetail);
var productDetail = new ProductDetailDVD() { .... };
var service = IoC.Resolve<IProductServiceGeneric<ProductDetailDVD>>();
service.SaveOrUpdate(productDetail);
public class ProductServiceGeneric<T> : IProductServiceGeneric<T>
{
private readonly ISession _session;
private readonly IProductRepoGeneric<T> _repo;
public ProductServiceGeneric()
{
_session = UnitOfWork.CurrentSession;
_repo = IoC.Resolve<IProductRepoGeneric<T>>();
}
public void SaveOrUpdate(T productDetail)
{
using (ITransaction tx = _session.BeginTransaction())
{
//here i'd like ot know the type and access properties depending of the class
_repo.SaveOrUpdate(productDetail);
tx.Commit();
}
}
}
có thể bạn nên thực hiện dịch vụ cụ thể và để lưu hoặc cập nhật chung nếu bạn không cần lưu bất kỳ dữ liệu nào khác hoặc bạn có thể sử dụng thừa kế trong orm của mình. Sự cần thiết của thực thể cụ thể có vẻ kỳ lạ: thông thường bạn làm điều gì đó như: Sản phẩm mớiServiceGeneric(). Và tất cả trong mã của bạn sẽ là sách, vì vậy bạn không cần phải biết. –