IDictionary单例的写法 ,放入缓存中,vs2008,Linq应用
using System.Collections.Generic;
using System.Linq;
using EPMS.Domain;
using EPMS.IDao;
using EPMS.ISrv;
namespace EPMS.Srv
{
public class ProjectRepo : IProjectRepo
{
protected IList<ProjectV2> _list;
protected IProjectV2Dao _projectDao;
/// <summary>
/// 项目的id字典
/// </summary>
protected IDictionary<int, ProjectV2> _dictById;
/// <summary>
/// 项目Number字典
/// </summary>
protected IDictionary<string, ProjectV2> _dictByNumber;
public ProjectRepo(IProjectV2Dao projectDao)
{
_projectDao = projectDao;
Reload();
}
/// <summary>
/// 重新把项目信息载入字典
/// </summary>
public void Reload()
{
_list = _projectDao.SelectAll();
IDictionary<int, ProjectV2> dictById = new Dictionary<int, ProjectV2>();
foreach (ProjectV2 project in _list)
dictById.Add(project.Id, project);
_dictById = dictById;
IDictionary<string, ProjectV2> dictByNumber = new Dictionary<string, ProjectV2>();
foreach (ProjectV2 project in _list)
dictByNumber.Add(project.Number, project);
_dictByNumber = dictByNumber;
}
public IList<ProjectV2> GetAll()
{
return _list.Select(s => s.Clone()).ToList();
}
public ProjectV2 GetById(int id)
{
if (_dictById.ContainsKey(id))
return _dictById[id].Clone();
return null;
}
public ProjectV2 GetByNumber(string number)
{
if (_dictByNumber.ContainsKey(number))
return _dictByNumber[number].Clone();
return null;
}
}
}