简体   繁体   中英

Get specific columns in UnitOfWork and generic repository in Entity Framework

I have a Windows Forms application and generic Entity Framework (latest version) method and multi-layer design pattern.

I want load specific columns, not all columns.

For example, I have 10 columns (a1...a10), but I want get data from (a1 ... a8) columns only. This is my code for get all columns - how do I that?

Repository layer:

public Repository(GlobalERPEntities context)
{
    Context = context;
    dbSet = Context.Set<T>();
}

public virtual IEnumerable<T> GetAll()
{
    return dbSet.ToList();
}

UnitOfWork layer: call get all method from repository layer

public UnitOfWork()
{
    Context = new GlobalERPEntities();
}

public Repository<T> Repository<T>() where T : class
{
    if (repositories == null)
    {
        repositories = new Dictionary<Type, object>();
    }

    if (repositories.Keys.Contains(typeof(T)) == true)
    {
        return repositories[typeof(T)] as Repository<T>;
    }

    Repository<T> repo = new Repository<T>(Context);
    repositories.Add(typeof(T), repo);

    return repo;
}

BLL layer: call get all method from UnitOfWork layer

protected UnitOfWork uow;

public Service()
{
    uow = new UnitOfWork();
}

public virtual IEnumerable<T> GetAll()
{
    return uow.Repository<T>().GetAll().ToList();
}

How to change it to get a custom set of columns, and how to call it in my form?

I am giving you a fool-proof solution as follows: first write IUnitOfWork as follows:

public interface IUnitOfWork
{
    IRepository<T> Repository<T>() where T : class;

    Task SaveChangesAsync();

    void ResetContextState();
}

Then UnitOfWork class as follows:

public class UnitOfWork : IUnitOfWork
{
    private readonly YourDbContext _dbContext;
    private Hashtable _repositories;
    public UnitOfWork(YourDbContext dbContext)
    {
        _dbContext = dbContext;
    }


    public IRepository<T> Repository<T>() where T : class
    {
        if (_repositories == null)
            _repositories = new Hashtable();

        var type = typeof(T).Name;

        if (!_repositories.ContainsKey(type))
        {
            var repositoryType = typeof(Repository<>);

            var repositoryInstance =
                Activator.CreateInstance(repositoryType
                    .MakeGenericType(typeof(T)), _dbContext);

            _repositories.Add(type, repositoryInstance);
        }

        return (IRepository<T>)_repositories[type];
    }

    public async Task SaveChangesAsync()
    {
        await _dbContext.SaveChangesAsync();
    }

    public void ResetContextState()
    {
        _dbContext.ChangeTracker.Entries().Where(e => e.Entity != null).ToList()
            .ForEach(e => e.State = EntityState.Detached);
    }
}

Now write IRepository interface as follows:

public interface IRepository<TEntity> where TEntity : class
{
    IQueryable<TEntity> GetEntities(Expression<Func<TEntity, bool>> condition = null, 
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "");

    Task<bool> IsEntityExists(Expression<Func<TEntity, bool>> filter = null);
    void InsertEntity(TEntity entity);
    void InsertEntities(List<TEntity> entities);
    void UpdateEntity(TEntity entity, params string[] excludeProperties);
    void DeleteEntity(TEntity entity);
    void DeleteEntities(List<TEntity> entities);
    Task<bool> IsTableEmptyAsync();
}

Then your Repository class as follows:

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    private readonly YourDbContext _dbContext;
    private readonly DbSet<TEntity> _dbSet;
    public Repository(YourDbContext dbContext)
    {
        _dbContext = dbContext;
        _dbSet = _dbContext.Set<TEntity>();

    }

    public IQueryable<TEntity> GetEntities(Expression<Func<TEntity, bool>> condition = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = _dbSet;

        if (condition != null)
        {
            query = query.Where(condition);
        }

        foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            query = orderBy(query);
        }

        return query;
    }

    public async Task<bool> IsEntityExists(Expression<Func<TEntity, bool>> condition)
    {
        bool status = false;

        if (condition != null)
        {
            status = await _dbSet.AnyAsync(condition);
        }

        return status;
    }

    public  void InsertEntity(TEntity entity)
    {
        _dbSet.Add(entity);
    }

    public void InsertEntities(List<TEntity> entities)
    {
        _dbSet.AddRange(entities);
    }

    public void UpdateEntity(TEntity entity, params string[] excludeProperties)
    {
        _dbContext.Entry(entity).State = EntityState.Modified;

        foreach (string property in excludeProperties)
        {
            _dbContext.Entry(entity).Property(property).IsModified = false;
        }
    }

    public void DeleteEntity(TEntity entity)
    {
        _dbSet.Remove(entity);
    }

    public void DeleteEntities(List<TEntity> entities)
    {
        _dbSet.RemoveRange(entities);
    }

    public async Task<bool> IsTableEmptyAsync()
    {
        bool hasAny = await _dbSet.AnyAsync();
        return !hasAny;
    }
}

Then use UnitOfWork in your service class or anywhere as follows:

public class EmployeeService
{
      private readonly UnitOfWork _unitOfWork;

      public EmployeeService()
      {
            _unitOfWork = new UnitOfWork();
      }

      public List<Employee> GetAllEmployees()
      {
         return _unitOfWork.Repository<Employee>().GetEntities().ToList();
      }

      public List<string> GetAllEmployeeNames()
      {
         return _unitOfWork.Repository<Employee>().GetEntities().Select(emp => emp.Name).ToList();
      }
}

Better use with dependency injection as follows:

public class EmployeeService
{
      private readonly IUnitOfWork _unitOfWork;

      public EmployeeService(IUnitOfWork unitOfWork)
      {
            _unitOfWork = unitOfWork;
      }

      public List<Employee> GetAllEmployees()
      {
         return _unitOfWork.Repository<Employee>().GetEntities().ToList();
      }

      public List<string> GetAllEmployeeNames()
      {
         return _unitOfWork.Repository<Employee>().GetEntities().Select(emp => emp.Name).ToList();
      }
}

When I use the UnitOfWork pattern, I follow pretty much the same template as specified on the Microsoft docs.

You could adjust the Get method (or add a new method) as follows (notice the additional select parameter that we use to specify the columns):

public virtual IEnumerable<TDest> Get<TDest>(
    Expression<Func<TEntity, bool>> filter = null,
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
    string includeProperties = "",
    Expression<Func<TEntity, TDest>> select = null)
{
    IQueryable<TEntity> query = dbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    foreach (var includeProperty in includeProperties.Split
        (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    {
        query = query.Include(includeProperty);
    }

    if (orderBy != null)
    {
        if (select == null)
            return (IEnumerable<TDest>)orderBy(query).ToList();

        return orderBy(query).Select(select).ToList();
    }
    else
    {
        if (select == null)
            (IEnumerable<TDest>)query.ToList();                

        return query.Select(select).ToList();
    }
}

Example use

public class Cat
{
   public string Name { get; set; }

   public int Age { get; set; }

   public string { get; set; }
}

// Select Name column only
var names = unitOfWork.CatRepository.Get(
    null, // No filter
    x => x.OrderByDescending(y => y.Age), // Order by oldest
    string.Empty, // No includeProperties
    x => x.Name // Select the Name column only
)
// Output: "Fluffy"

// Select Name and Age columns only
var namesAndAges = unitOfWork.CatRepository.Get(
    x => x.Age > 1, // Where Age is greater than 1
    null, // No order
    string.Empty, // No includeProperties
    x => new { Name = x.Name, Age = x.Age) // Select the Name and Age columns only
)
// Output: { Name = "Fluffy", Age = 3 }

// Select the entity (no columns specified)
var all = unitOfWork.CatRepository.Get<Cat>(
    null, // No filter
    null, // No order
    string.Empty, // No includeProperties
    null) // Select the Name and Age columns only
)
// Output: { Cat() { Name = "Fluffy", Age = 3, Breed = "Moggy" } }

// Same query as above
var allSimple = unitOfWork.CatRepository.Get<Cat>()
// Output: { Cat() { Name = "Fluffy", Age = 3, Breed = "Moggy" } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM