C# 存储库通用自定义查询

C# repository Generic with custom queries

我目前有一种方法允许我为我的网络应用程序表单中的下拉列表生成一个 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();

我创建了以下支持class(我暂时排除了 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);
    }