using System; using System.Collections.Generic; using System.Linq; using System.Text; using SQLHelper; using System.Data; using System.Data.SqlClient; namespace BusinessLib { public class Person { readonly List<Person> _children = new List<Person>(); public IList<Person> Children { get { return _children; } } public string Name { get; set; } } public static class Database { public static Person GetFamilyTree() { // In a real app this method would access a database. //这里的代码如何写 } } }
望回复,谢谢。
如果你不喜欢用ORM喜欢用SQLHelper的话,又在SQLHelper与Person之间如何实现这儿纠结的话,
建议你看看AutoMapper,当然,你搜索这个单词的话,会搜索到其他相关的Mapper,
AutoMapper并不是性能最优的,不过是单词比较容易记。其他的命名比较怪一点。
你不是有这个吗:SQLHelper
public static Person GetFamilyTree() { // In a real app this method would access a database. string sqlStr = "select DepartmentID, DepartmentName from Department"; int intCount = SqlHelper.ExecuteResultCount(sqlStr); SqlDataReader sdr = SqlHelper.ExecuteDataReader(sqlStr); for (int i = 1; i < intCount; i++) { // string SonDepartmentName = sdr["DepartmentName"].ToString(); //得到SonDepartmentID部门的所有上级部门ID //循环查找SonDepartmentID部门的上级部门 //如果该数组中存在DepartmentID部门,则把SonDepartmentID部门添加到SonDepartmentIDs中 return new Person { Name = SonDepartmentName, Children = { new Person()}}; } sdr.Close(); }
以上代码如何修改,请问SQLHelper与Person之间如何实现?