用存储库替换Linq where语句

时间:2015-02-27 09:43:11

标签: c# entity-framework

我在我的WPF应用程序中实现了这样的存储库模式:

 public interface ICrud<T> where  T : class 
        {
            IEnumerable<T> GetAll();
            Task<IEnumerable<T>> AsyncGetAll(); 
            void AddNew(params T[] items);
            void Delete(params T[] items);
            void Update(params T[] items);
            void SaveOrUpdate(params T[] items);
        }

     public class Crud<T> : ICrud<T> where T : class 
        {

            public void AddNew(params T[] items)
            {
                using (var context = new DataEntities())
                {
                    foreach (T item in items)
                    {
                        context.Entry(item).State = System.Data.Entity.EntityState.Added;
                    }
                    context.SaveChanges();
                }
            }

            public void Delete(params T[] items)
            {
                using (var context = new DataEntities())
                {
                    foreach (T item in items)
                    {
                        context.Entry(item).State = System.Data.Entity.EntityState.Deleted;
                    }
                    context.SaveChanges();
                }
            }

            public void Update(params T[] items)
            {
                using (var context = new DataEntities())
                {
                    foreach (T item in items)
                    {
                        context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                    }
                    context.SaveChanges ();
                }
            }


            public  void SaveOrUpdate(params T[] items)
            {
                using (var context = new DataEntities())
                {
                    foreach (T item in items)
                    {
                        try
                        {
                            context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                             context.SaveChanges();
                        }
                        catch (Exception)
                        {
                            context.Entry(item).State = System.Data.Entity.EntityState.Added;
                             context.SaveChanges();
                        }

                    }

                }
            }

            public IEnumerable<T> GetAll()
            {
                using (var context = new DataEntities())
                {
                    DbSet<T> dbSet = context.Set<T>(); 
                    return dbSet.AsEnumerable().ToList();
                }
            }


            public Task<IEnumerable<T>> AsyncGetAll()
            {
                return Task.Factory.StartNew(() =>
                {

                        var context = new DataEntities();
                        DbSet<T> dbSet = context.Set<T>();
                        return dbSet.AsEnumerable();

                });
            }
        }

我需要在这个实现中添加一个替换long linq where statement的方法,我解释一下:

替换

_DataContext.AddRange( (await SimpleIoc.Default.GetInstance<ICrud<Student>>().AsyncGetAll()).Where(x => x.Name == "toto" && x.Age < 30 && .....).ToList() );

以示例

_DataContext.AddRange(await SimpleIoc.Default.GetInstance<ICrud<Student>>().Fetch(ListOfConditions));

我需要知道我该怎么做?有什么建议吗?

由于

1 个答案:

答案 0 :(得分:3)

尝试使用以下

    public IEnumerable<T> Search(System.Linq.Expressions.Expression<Func<T, bool>> predicate = null)
    {
      using(var context = new DataEntities())
      {
              return null != predicate
                    ? context.Set<T>().Where(predicate).AsEnumerable()
                    : context.Set<T>().AsEnumerable();
      }
    }

您可以使用

等代码调用该方法
 Crud.Search(e => e.Id == 1);

类Crud实现接口。