コード例 #1
0
        public Result InsertEntity <T>(T item, bool ignoreValidation = false) where T : class
        {
            using (DBContext context = new HotelReservationsContext())
            {
                if (ContainsEntity(item, context))
                {
                    return(new Result(ResultType.ALREADY_IN_DATABASE));
                }
                // validate item if needed
                if (!ignoreValidation)
                {
                    Result result = Validate(item);
                    if (result.type != ResultType.SUCCESS)
                    {
                        return(result);
                    }
                }

                // add item
                context.Add(item);
                context.SaveChanges();
                OnDBChange?.Invoke(context, typeof(T));
            }
            return(new Result(ResultType.SUCCESS));
        }
コード例 #2
0
 public void RemoveEntity <T>(T entity)
 {
     using (DBContext context = new HotelReservationsContext())
     {
         context.Remove(entity);
         context.SaveChanges();
         OnDBChange?.Invoke(context, typeof(T));
     }
 }
コード例 #3
0
        public Result EditEntity <T>(T entity) where T : class
        {
            using (DBContext context = new HotelReservationsContext())
            {
                if (!ContainsEntity(entity, context))
                {
                    return(new Result(ResultType.NOT_IN_DATABASE));
                }

                Result result = Validate(entity);
                if (result.type != ResultType.SUCCESS)
                {
                    return(result);
                }
                context.Update(entity);
                context.SaveChanges();
                OnDBChange?.Invoke(context, typeof(T));
            }

            return(new Result(ResultType.SUCCESS));
        }