【发布时间】:2010-03-25 21:10:02
【问题描述】:
请看以下方法。
public static ProductsCollection GetDummyData(int? customerId, int? supplierId)
{
try
{
if (customerId != null && customerId > 0)
{
Filter.Add(Customres.CustomerId == customerId);
}
if (supplierId != null && supplierId > 0)
{
Filter.Add(Suppliers.SupplierId == supplierId);
}
ProductsCollection products = new ProductsCollection();
products.FetchData(Filter);
return products;
}
catch
{
throw;
}
}
public static ProductsCollection GetDummyData(int? customerId)
{
return ProductsCollection GetDummyData(customerId, (int?)null);
}
public static ProductsCollection GetDummyData()
{
return ProductsCollection GetDummyData((int?)null);
}
1- 请建议我如何为 CustomerId 和 SupplierId 进行重载,因为使用 GetDummyData(int?) 只能创建一个重载。我是否应该添加另一个参数来提及第一个参数是 CustomerId 或 SupplierId,例如 GetDummyData(int?, string)。或者我应该使用枚举作为第二个参数并提到第一个参数是 CustoemId 或 SupplierId。
2- 这个条件是正确的还是仅仅检查 > 0 就足够了 -> if (customerId != null && customerId > 0)
3- 像这样使用 Try/catch 是否正确?
4- 传递 (int?)null 是正确的或任何其他更好的方法。
编辑:
我发现了一些其他类似的帖子,因为我对泛型一无所知,这就是我面临这个问题的原因。我对吗?以下是帖子。
【问题讨论】:
标签: c#