我有一个“Product”基类,其他一些类“ProductBookDetail”,“ProductDVDDetail”继承自这个类。我使用ProductService类来对这些类进行操作。但是,我必须根据类型进行一些检查(书籍的ISBN,DVD的语言)。我想知道投放“productDetail”值的最佳方式,我在SaveOrupdate中收到。我试过的GetType()和投用(ProductBookDetail)productDetail
但是这不行通用<T>如何投射?
感谢,
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();
}
}
}
也许你应该做一个具体的服务,留下的保存或更新通用的,如果你不需要保存任何不同的数据,或者你可以在你的ORM使用继承。其中需要具体实体听起来很奇怪:通常你做这样的事情: 新ProductServiceGeneric()。而且你的代码中的所有内容都是书籍,所以你不必知道。 –