我目前有一个方法,允许我在我的web应用程序表单下拉列表生成一个IEnumerable对象。
当前代码示例:
//Name with Id
StateListDp = _db.States.ToDropdownList(c => c.Name, c => Convert.ToString(c.Id, CultureInfo.InvariantCulture));
//Description with Id
StatusesListDp = _db.Statuses.ToDropdownList(c => c.Description, c => Convert.ToString(c.Id, CultureInfo.InvariantCulture));
我刚刚实现了存储库设计模式。
我现在可以把它转换成这样了:
StateListDp = unitOfWork.State.GetDropDownList().ToList();
StatusesListDp = unitOfWork.Status.GetDropDownList().ToList();
我已经创建了以下支持类(我现在已经排除了unitOfWork)
public class StatusRepository : Repository<Statuses>, IStatusRepository
{
private readonly TenDDbContext context;
public StatusRepository(TenDDbContext context): base(context)
{
this.context = context;
}
public IEnumerable<Statuses> GetAllActive()
{
return Find(x => x.IsActive == true);
}
public IEnumerable<SelectListItem> GetDropDownList()
{
return GetAllActive()
.ToDropdownList(c => c.Description, c => Convert.ToString(c.Id, CultureInfo.InvariantCulture));
}
}
public class StateRepository : Repository<States>, IStateRepository
{
private readonly TenDDbContext context;
public StateRepository(TenDDbContext context): base(context)
{
this.context = context;
}
public IEnumerable<States> FindAllActive(Expression<Func<States, bool>> predicate)
{
return Find(predicate).Where(x => x.IsActive == true);
}
public IEnumerable<States> GetAllActive()
{
return Find(x => x.IsActive == true);
}
public IEnumerable<SelectListItem> GetDropDownList()
{
return GetAllActive()
.ToDropdownList(c => c.Name, c => Convert.ToString(c.Id, CultureInfo.InvariantCulture));
}
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly TenDDbContext context;
public Repository(TenDDbContext context)
{
this.context = context;
}
public TEntity Add(TEntity entity)
{
return context.Set<TEntity>().Add(entity).Entity;
}
//public bool save(TEntity entity)
//{
// var test= Add(entity);
// test.
//}
public void AddRange(IEnumerable<TEntity> entities)
{
context.Set<TEntity>().AddRange(entities);
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return context.Set<TEntity>().Where(predicate);
}
public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
{
return context.Set<TEntity>().SingleOrDefault(predicate);
}
public TEntity Get(int id)
{
return context.Set<TEntity>().Find(id);
}
public IEnumerable<TEntity> GetAll()
{
return context.Set<TEntity>().ToList();
}
public TEntity Update(TEntity entity)
{
//context.Attach(entity).State = EntityState.Modified;
// context.Attach(entity);
//return context.Entry(entity).State = EntityState.Modified;
return context.Update(entity)
.Entity;
}
public void Remove(TEntity entity)
{
context.Set<TEntity>().Remove(entity);
}
public void RemoveRange(IEnumerable<TEntity> entities)
{
context.Set<TEntity>().RemoveRange(entities);
}
}
所以我的问题是我有10个左右的表,我需要从中检索这个非常相似的数据。
我想知道有一种方法使GetDropDownList一个通用的方法?
所以我可以限制重复代码的数量…
我甚至愿意把它变成两个方法
GetDropDownNameList和GetDropDownDescriptionList
添加下拉扩展方法
public static IEnumerable<SelectListItem> ToDropdownList<T>(this IEnumerable<T> items,
Func<T, string> text, Func<T, string> value = null, Func<T, Boolean> selected = null)
{
var listData = items.Select(p => new SelectListItem
{
Text = text.Invoke(p),
Value = (value == null ? text.Invoke(p) : value.Invoke(p)),
Selected = selected != null && selected.Invoke(p)
});
var defaultRow = new SelectListItem() { Value = "0", Text = "Please Select One", Selected = V };
var newList = listData.Prepend(defaultRow);
//return new SelectList(newList, "Value", "Text");
return newList;
}
传递用于描述和键的道具的选择应该可以做到这一点。如果你想让它在IRepository中更通用,用过滤器
替换GetAllActive()public IEnumerable<SelectListItem> GetDropDownList(Expression<Func<TEntity, string>> predicateDescription, Expression<Func<TEntity, string>> predicateKey)
{
return GetAllActive().ToDropdownList(predicateDescription, predicateKey);
}