目前我有一个继承结构,比如说Employee和Manager类,继承自Person抽象类 我应该为每个具体类实现一个存储库,还是为整个继承结构实现一个存储库?就个人而言,我觉得每个具体类的一个存储库更正确 以下是我对此方法的实现。我将Data Mapper模式与Repository一起使用但在实现这种方法时遇到了一些困难:我不知道除了Data Mapper类之外的哪些方法,但放在那里也很尴尬
public enum PersonType
{
Unknown = 0,
Employee = 1,
Manager = 2
}
public abstract class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public PersonType Type { get; set; }
//Omitted properties and methods
}
public class Employee : Person
{
public string EmployeeNo { get; set; }
//Omitted properties and methods
}
public class Manager : Person
{
public string ManagerNo { get; set; }
//Omitted properties and methods
}
以下是mappers类:
public interface IMapper
{
bool CanMap(PersonType type);
Person MapFrom(DataRow dataRow);
//This method is awkward to put here
SqlCommand CreateUpdateCommandFrom(Person person);
}
public abstract class MapperBase : IMapper
{
public abstract bool CanMap(PersonType type);
public abstract Person MapFrom(DataRow dataRow);
public abstract SqlCommand CreateUpdateCommandFrom(Person person);
protected T MapCommonPropertiesFrom<T>(DataRow dataRow) where T : Person, new()
{
return new T
{
//Populate common properties like id, name, age
};
}
}
public class EmployeeMapper : MapperBase
{
public override bool CanMap(PersonType type)
{
return type == PersonType.Employee;
}
public override Person MapFrom(DataRow dataRow)
{
Employee employee = MapCommonPropertiesFrom<Employee>(dataRow);
//Populate employee properties
return employee;
}
public override SqlCommand CreateUpdateCommandFrom(Person person)
{
SqlCommand updateCmd = new SqlCommand();
//Set parameters for updateCmd from person properties
return updateCmd;
}
}
//Similar class for ManagerMapper
下面是一个用于查找mapper类的寄存器类
public static class MapperRegistry
{
private static List<IMapper> _Mappers = RegisterMappers();
private static List<IMapper> RegisterMappers()
{
return new List<IMapper>
{
new EmployeeMapper()
//ManagerMapper
};
}
public static IMapper FindMapperFor(PersonType type)
{
foreach (IMapper eachMapper in _Mappers)
{
if (eachMapper.CanMap(type))
{
return eachMapper;
}
}
throw new ArgumentException(string.Format("Cannot find mapper for {0}", type));
}
}
最后一个类是存储库类
public class PersonRepository
{
public Person FindBy(int id)
{
//Execute query command in db, get datatable back, store each person
DataTable returnedData = ExecuteQueryCommand(id);
PersonType type = int.Parse(returnedData.Rows[0], "Id");
IMapper mapper = MapperRegistry.FindMapperFor(type);
return mapper.MapFrom(returnedData.Rows[0]);
}
private DataTable ExecuteQueryCommand(int id)
{
//Execute query command in db
return new DataTable();
}
public void Update<T>(T person) where T : Person
{
IMapper mapper = MapperRegistry.FindMapperFor(person.Type);
SqlCommand updateCmd = mapper.CreateUpdateCommandFrom(person);
//Execute update command
}
}
我觉得不对的是CreateUpdateCommandFrom(),目前放在Mapper类中,右边应该放在respository类中,但在一般的仓库类中,我们不知道子类的特定属性,如EmployeeNo在Employee中在Manager类中的class和ManagerNo,因此我们无法在存储库中创建常见的SqlCommand
有没有更好的方法? 非常感谢你
答案 0 :(得分:2)