Beispiel #1
0
        public virtual T GetSingle(Guid id)
        {
            T item;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                // Apply eager loading
                if (NavigationProperties != null && NavigationProperties.Count > 0)
                {
                    dbQuery = NavigationProperties.Aggregate(dbQuery, (current, navigationProperty) => current.Include(navigationProperty));
                }

                item = dbQuery
                       .AsNoTracking()
                       .FirstOrDefault(b => b.Id == id);
            }
            if (item == null)
            {
                throw new NotFoundException($"The provided Id ({id}) is not found in {typeof(T)} table");
            }

            return(item);
        }
Beispiel #2
0
 public User GetSingle(string id)
 {
     using (var context = new PookDbContext())
     {
         return(context.Users.Find(id));
     }
 }
Beispiel #3
0
        public virtual IList <T> GetAll()
        {
            List <T> list;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                // Apply eager loading
                if (NavigationProperties != null && NavigationProperties.Count > 0)
                {
                    dbQuery = NavigationProperties.Aggregate(dbQuery, (current, navigationProperty) => current.Include(navigationProperty));
                }

                // Apply sorting
                dbQuery = OrderBy(dbQuery);

                list = dbQuery
                       .AsNoTracking()
                       .ToList();
            }


            return(list);
        }
Beispiel #4
0
        public virtual void Update(params T[] items)
        {
            using (var context = new PookDbContext())
            {
                foreach (T item in items)
                {
                    item.UpdatedOn = DateTime.Now;

                    T attachedEntity = context.Set <T>().SingleOrDefault(e => e.Id == item.Id);
                    if (attachedEntity != null)
                    {
                        var entry = context.Entry(attachedEntity);
                        entry.CurrentValues.SetValues(item);

                        // Attach user entity and set all properties as modified
                        entry.State = EntityState.Modified;

                        if (IgnoreProperties != null && IgnoreProperties.Count > 0)
                        {
                            foreach (var ignoreProperty in IgnoreProperties)
                            {
                                entry.Property(ignoreProperty).IsModified = false;
                            }
                        }

                        entry.Property(i => i.CreatedOn).IsModified = false;
                        entry.Property(i => i.CreatedBy).IsModified = false;
                    }
                }

                context.SaveChanges();
            }
        }
Beispiel #5
0
 public void Update(User user)
 {
     using (var context = new PookDbContext())
     {
         context.Entry(user).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Beispiel #6
0
 public void Add(User user)
 {
     using (var context = new PookDbContext())
     {
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
Beispiel #7
0
        public IList <User> GetAll()
        {
            List <User> list;

            using (var context = new PookDbContext())
            {
                list = context.Users.ToList();
            }
            return(list);
        }
Beispiel #8
0
 public virtual void Delete(params T[] items)
 {
     using (var context = new PookDbContext())
     {
         foreach (T item in items)
         {
             context.Entry(item).State = EntityState.Deleted;
         }
         context.SaveChanges();
     }
 }
Beispiel #9
0
 public void Delete(string id)
 {
     using (var context = new PookDbContext())
     {
         var user = new User {
             Id = id
         };
         context.Users.Attach(user);
         context.Users.Remove(user);
         context.SaveChanges();
     }
 }
Beispiel #10
0
        public virtual T GetFirst()
        {
            T item;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                item = dbQuery
                       .AsNoTracking()
                       .FirstOrDefault();
            }

            return(item);
        }
Beispiel #11
0
        public virtual bool Exists(Guid id)
        {
            bool exists;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                exists = dbQuery
                         .AsNoTracking()
                         .Any(obj => obj.Id == id);
            }

            return(exists);
        }
Beispiel #12
0
        public virtual T GetLastCreated()
        {
            T item;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                item = dbQuery
                       .AsNoTracking()
                       .OrderByDescending(obj => obj.CreatedOn)
                       .FirstOrDefault();
            }

            return(item);
        }
Beispiel #13
0
        public virtual IList <T> GetList(Expression <Func <T, bool> > where)
        {
            List <T> list;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();
                context.Configuration.ProxyCreationEnabled = false;

                // Apply eager loading
                if (NavigationProperties != null && NavigationProperties.Count > 0)
                {
                    dbQuery = NavigationProperties.Aggregate(dbQuery, (current, navigationProperty) => current.Include(navigationProperty));
                }

                dbQuery = dbQuery
                          .AsNoTracking()
                          .Where(where);

                // Set total number of records
                if (PagingSettings != null)
                {
                    PagingSettings.TotalNumberOfRecords = dbQuery.Count();
                }

                // Apply sorting
                dbQuery = OrderBy(dbQuery);

                // Retrieve data
                if (PagingSettings != null)
                {
                    dbQuery = dbQuery
                              .Skip((PagingSettings.Page - 1) * PagingSettings.PageSize)
                              .Take(PagingSettings.PageSize);
                }

                list = dbQuery.ToList();

                // Set number of records
                if (PagingSettings != null)
                {
                    PagingSettings.NumberOfRecords = list.Count;
                }
            }

            return(list);
        }
Beispiel #14
0
        public virtual void Add(params T[] items)
        {
            using (var context = new PookDbContext())
            {
                foreach (T item in items)
                {
                    if (item.Id == Guid.Empty)
                    {
                        item.Id = Guid.NewGuid();
                    }

                    item.CreatedOn = DateTime.Now;
                    item.UpdatedOn = DateTime.Now;

                    context.Entry(item).State = EntityState.Added;
                }
                context.SaveChanges();
            }
        }
Beispiel #15
0
        public virtual int GetCount(Expression <Func <T, bool> > where)
        {
            int count;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                dbQuery = dbQuery.AsNoTracking();

                if (where != null)
                {
                    dbQuery = dbQuery.Where(where);
                }

                // Set total number of records
                count = dbQuery.Count();
            }
            return(count);
        }
Beispiel #16
0
        public virtual T GetSingle(Expression <Func <T, bool> > where)
        {
            T item;

            using (var context = new PookDbContext())
            {
                IQueryable <T> dbQuery = context.Set <T>();

                // Apply eager loading
                if (NavigationProperties != null && NavigationProperties.Count > 0)
                {
                    dbQuery = NavigationProperties.Aggregate(dbQuery, (current, navigationProperty) => current.Include(navigationProperty));
                }

                item = dbQuery
                       .AsNoTracking()
                       .FirstOrDefault(where);
            }

            return(item);
        }