【发布时间】:2015-08-09 00:38:01
【问题描述】:
我的“用户”表中有与部门、角色和团队相关的关系。
建议是当我通过存储库查询数据时,因此我更喜欢返回“用户”模型,并为部门、角色和团队填充完整的对象和属性。
我目前的解决方案是使用服务层从相关存储库(UserService.cs)设置用户属性。
我的项目有服务层(Service)、数据层(Repository)和领域模型。
请参见下面的示例代码:
模型/User.cs
public class User{
public string username { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public Int32 DepartmentId { get; set; }
public Department Department { get; set; }
public Int32 RoleId { get; set; }
public Role Role { get; set; }
public IList<Team> Teams { get; set; }
}
存储库/UserRepository.cs
public class UserRepository : IUserRepository{
private sqlSelectStatement = String.Format(@"SELECT u.[Id]
,[UserName]
,[FirstName]
,[LastName]
,[Email]
,[Telephone]
,[DepartmentId]
,[TeamId]
,[RoleId]
,[Enabled]
,[Created]
,[Modified]
,[UpdatedBy]
FROM {0}[User] ", SchemaOwner);
public IEnumerable<User> GetAll()
{
var dbcommand = this.SessionContext.GetSqlCommand(sqlSelectStatement );
return this.SessionContext.GetList<User>(dbcommand);
}
}
服务/UserService.cs
public class UserService : IUserService
{
private IUserRepository userRepository;
private ITeamRepository teamRepository;
private IDepartmentRepository departmentRepository;
private IRoleRepository roleRepository;
public IEnumerable<User> GetAll()
{
var users = userRepository.GetAll();
var departments = departmentRepository.GetAll();
var teamMembers = teamMemberRepository.GetAll();
var roles= roleRepository.GetAll();
foreach (User user in users){
user.Department = departments.Where(department=>department.Id== user.departmentId);
user.Role = roles.Where(role=>role.Id== user.roleId);
user.Teams = teams.where(team=>team.UserId == user.Id);
}
}
}
请注意,我没有使用任何“ORM”框架,也没有计划使用任何“ORM”
【问题讨论】:
标签: c# repository-pattern