【发布时间】:2010-08-26 19:15:43
【问题描述】:
我很难弄清楚如何在我尝试创建的 DTO 映射器中实现工厂模式。我很确定我需要重新考虑我的设计。这是我正在运行的一个非常小的示例:
public abstract class Person
{
public string Name { get; set; }
public decimal Salary { get; set; }
}
public class Employee : Person
{
public Employee()
{
this.Salary = 20000;
}
}
public class Pilot : Person
{
public string PilotNumber { get; set; }
public Pilot()
{
this.Salary = 50000;
}
}
public static class PersonFactory
{
public static Person CreatePerson(string typeOfPerson)
{
switch (typeOfPerson)
{
case "Employee":
return new Employee();
case "Pilot":
return new Pilot();
default:
return new Employee();
}
}
}
并使用工厂:
Person thePilot = PersonFactory.CreatePerson("Pilot");
((Pilot)thePilot).PilotNumber = "123ABC";
如何在不将其类型转换为 Pilot 的情况下加载飞行员编号?这是错误的方法吗?我可以将试点号码放在 Person 类中,但是 Employee 会继承这个号码,这不是我想要的。我能做什么?
谢谢!
-杰克逊
【问题讨论】:
-
只回复“DTO mapper”的话:automapper.codeplex.com
标签: c# factory-pattern