【发布时间】:2013-08-11 09:57:16
【问题描述】:
我正在尝试弄清楚如何简化以下内容
假设我有 2 个实体类
public class A
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
与
public class B
{
public int Id { get; set; }
public string Nom { get; set; }
public string Ville { get; set; }
}
相似但不相同的类。
每个类都有一个用于 CRUD 操作的存储库类,例如...
public class RepA
{
public static List<A> GetAll()
{
List<A> list = new List<A>();
A a1 = new A() {Id=1, Name="First A", City="Boston"};
A a2 = new A() {Id=2, Name="First B", City="Chicago"};
A a3 = new A() {Id=3, Name="First C", City="San Francisco"};
list.Add(a1);
list.Add(a2);
list.Add(a3);
return list;
}
public static void SaveAll(List<A> list)
{
foreach (A a in list)
{
Console.WriteLine("Saved Id = {0} Name = {1} City={2}",
a.Id, a.Name, a.City);
}
}
}
与
public class RepB
{
public static List<B> GetAll()
{
List<B> list = new List<B>();
B b1 = new B() {Id=1, Nom="Second A", Ville="Montreal"};
B b2 = new B() {Id=2, Nom="Second B", Ville="Paris"};
B b3 = new B() {Id=3, Nom="Second C", Ville="New Orleans"};
list.Add(b1);
list.Add(b2);
list.Add(b3);
return list;
}
public static void SaveAll(List<B> list)
{
foreach (B b in list)
{
Console.WriteLine("Saved Id = {0} Name = {1} City={2}", b.Id,
b.Nom, b.Ville);
}
}
}
我将如何匿名调用我的存储库而不必诉诸于此,因为在我的真实示例中,我有 100 个存储库,而不是 2 个。
void Main()
{
ChosenType chosentype = RandomChosenType(); //A or B
switch (chosentype)
{
case ChosenType.A:
var listA = RepA.GetAll();
RepA.SaveAll(listA);
break;
case ChosenType.B:
var listB = RepB.GetAll();
RepB.SaveAll(listB);
break;
default:
break;
}
}
【问题讨论】:
-
看起来您正试图为每个本地化存储一个单独的表 - 这不是一个好主意。最好在后端保持一致,并且仅出于显示目的进行本地化。
-
本地化数据结构似乎是个坏主意。不知道你会从中获得什么。它只会使您的代码难以编写和维护。程序员真的希望能够阅读所有语言吗?
-
获取所有的类,把它放在一个列表中,并根据选择的类调用常用方法,在 Switch 中传递一个并将 LIST 作为通用关键字并附加列表 +"ClassName" ...
-
什么是
ChosenType?你真的有每种类型的枚举吗?类型是如何确定的(换句话说,RandomChosenType代表什么)? -
我的示例显示本地化,但忽略字段的名称,我试图在我的示例中快速。但表格包含不同的列和类型,彼此没有本地化版本
标签: c# .net list generics interface