Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
 public User GetSingle(string id)
 {
     using (var context = new PookDbContext())
     {
         return(context.Users.Find(id));
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 5
0
 public void Update(User user)
 {
     using (var context = new PookDbContext())
     {
         context.Entry(user).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Ejemplo n.º 6
0
 public void Add(User user)
 {
     using (var context = new PookDbContext())
     {
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
Ejemplo n.º 7
0
        public IList <User> GetAll()
        {
            List <User> list;

            using (var context = new PookDbContext())
            {
                list = context.Users.ToList();
            }
            return(list);
        }
Ejemplo n.º 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();
     }
 }
Ejemplo n.º 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();
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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();
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }