我有这样的方法:不同之处仅在传递给它们的类我可以将派生类中的这些方法与基类中的方法合并吗?
public void AddOrUpdate(Product product)
{
try
{
_productRepository.AddOrUpdate(product);
}
catch (Exception ex)
{
_ex.Errors.Add("", "Error when adding product");
throw _ex;
}
}
public void AddOrUpdate(Content content)
{
try
{
_contentRepository.AddOrUpdate(content);
}
catch (Exception ex)
{
_ex.Errors.Add("", "Error when adding content");
throw _ex;
}
}
加上更多的方法。
是否有某种方法可以在基类中编写这些方法,而不是在每个派生类中重复该方法?我正在考虑基于泛型的东西,但我不知道如何实现,也不知道如何传入_productRepository。
仅供参考这里的_productRepository和_contentRepository的定义方式:
private void Initialize(string dataSourceID)
{
_productRepository = StorageHelper.GetTable<Product>(dataSourceID);
_contentRepository = StorageHelper.GetTable<Content>(dataSourceID);
_ex = new ServiceException();
}
框架? – 2011-12-19 06:49:17
不使用实体框架 – 2011-12-19 06:58:11