Пример #1
0
        /// <summary>
        /// 依照某一個ViewModel的值,產生對應的Entity並且新增到資料庫
        /// </summary>
        /// <typeparam name="TViewModel">ViewModel的形態</typeparam>
        /// <param name="viewModel">ViewModel的Reference</param>
        /// <returns>是否儲存成功</returns>
        public void CreateViewModelToDatabase <TViewModel>(TViewModel viewModel)
        {
            var entity = DataModelToViewModel.GenericMapper <TViewModel, T>(viewModel);

            db.Repository <T>().Create(entity);
            db.SaveChanges();
        }
Пример #2
0
        /// <summary>
        /// 依照某一個ViewModel的值,更新對應的Entity
        /// </summary>
        /// <typeparam name="TViewModel">ViewModel的形態</typeparam>
        /// <param name="viewModel">ViewModel的值</param>
        /// <param name="wherePredicate">過濾條件 - 要被更新的那一筆過濾條件</param>
        /// <returns>是否刪除成功</returns>
        public virtual void UpdateViewModelToDatabase <TViewModel>(TViewModel viewModel, Expression <Func <T, bool> > wherePredicate)
        {
            var entity = db.Repository <T>().Read(wherePredicate);

            var result = DataModelToViewModel.GenericMapper(viewModel, entity);

            db.Repository <T>().Update(result);

            db.SaveChanges();
        }
Пример #3
0
        public void MemberMoidfyPassword <TViewModel>(TViewModel viewModel, int id, string repassword)
        {
            var entity = db.Repository <Member>().Read(x => x.MemberID == id);

            DataModelToViewModel.GenericMapper(viewModel, entity);
            var hashPasswrod = Hash.EncodePassword(repassword, entity.HashCode);

            entity.ResetPasswordCode = entity.Password;
            entity.Password          = hashPasswrod;
            db.Repository <Member>().Update(entity);
        }
Пример #4
0
        /// <summary>
        /// 取得某一個條件下面的某一筆Entity並且轉成對應的ViewModel
        /// </summary>
        /// <typeparam name="TViewModel">ViewModel的形態</typeparam>
        /// <param name="wherePredicate">過濾邏輯</param>
        /// <param name="includes">需要Include的Entity</param>
        /// <returns>取得轉換過的ViewModel或者是null</returns>
        public virtual TViewModel GetSpecificDetailToViewModel <TViewModel>(Expression <Func <T, bool> > wherePredicate, params Expression <Func <T, object> >[] includes)
        {
            var data = db.Repository <T>().Reads();

            foreach (var item in includes)
            {
                data.Include(item);
            }

            return(DataModelToViewModel.GenericMapper <T, TViewModel>(data.Where(wherePredicate).FirstOrDefault()));
        }
Пример #5
0
        public string AddMember <TViewModel>(TViewModel viewModel, string password)
        {
            var    keyNew = Hash.GeneratePassword(10);
            Member entity = new Member()
            {
                ActivationCode = Guid.NewGuid(),
                HashCode       = keyNew, //hash 加密
                EmailVerified  = false,  //註冊時將Email認證設為false
                RoleID         = 4       //初始為Guest
            };

            DataModelToViewModel.GenericMapper(viewModel, entity);

            var hashPasswrod = Hash.EncodePassword(password, keyNew);

            entity.Password = hashPasswrod;

            db.Repository <Member>().Create(entity);
            db.SaveChanges();

            return(entity.ActivationCode.ToString());
        }