private BaseVM CreateVM(Type VMType, object Id = null, object[] Ids = null, Dictionary <string, object> values = null, bool passInit = false) { //通过反射创建ViewModel并赋值 var ctor = VMType.GetConstructor(Type.EmptyTypes); BaseVM rv = ctor.Invoke(null) as BaseVM; try { rv.Session = new SessionServiceProvider(HttpContext.Session); } catch { } rv.ConfigInfo = ConfigInfo; rv.Cache = Cache; rv.LoginUserInfo = LoginUserInfo; rv.DataContextCI = ConfigInfo.ConnectionStrings.Where(x => x.Key.ToLower() == CurrentCS.ToLower()).Select(x => x.DcConstructor).FirstOrDefault(); rv.DC = this.DC; rv.MSD = new ModelStateServiceProvider(ModelState); rv.FC = new Dictionary <string, object>(); rv.CreatorAssembly = this.GetType().AssemblyQualifiedName; rv.CurrentCS = CurrentCS; rv.CurrentUrl = this.BaseUrl; rv.WindowIds = ""; rv.UIService = new DefaultUIService(); rv.Log = this.Log; rv.ControllerName = this.GetType().FullName; rv.Localizer = this.Localizer; if (HttpContext != null && HttpContext.Request != null) { try { if (Request.QueryString != null) { foreach (var key in Request.Query.Keys) { if (rv.FC.Keys.Contains(key) == false) { rv.FC.Add(key, Request.Query[key]); } } } var f = HttpContext.Request.Form; foreach (var key in f.Keys) { if (rv.FC.Keys.Contains(key) == false) { rv.FC.Add(key, f[key]); } } } catch { } } //如果传递了默认值,则给vm赋值 if (values != null) { foreach (var v in values) { PropertyHelper.SetPropertyValue(rv, v.Key, v.Value, null, false); } } //如果ViewModel T继承自BaseCRUDVM<>且Id有值,那么自动调用ViewModel的GetById方法 if (Id != null && rv is IBaseCRUDVM <TopBasePoco> cvm) { cvm.SetEntityById(Id); } //如果ViewModel T继承自IBaseBatchVM<BaseVM>,则自动为其中的ListVM和EditModel初始化数据 if (rv is IBaseBatchVM <BaseVM> temp) { temp.Ids = new string[] { }; if (Ids != null) { var tempids = new List <string>(); foreach (var iid in Ids) { tempids.Add(iid.ToString()); } temp.Ids = tempids.ToArray(); } if (temp.ListVM != null) { temp.ListVM.CopyContext(rv); temp.ListVM.Ids = Ids == null ? new List <string>() : temp.Ids.ToList(); temp.ListVM.SearcherMode = ListVMSearchModeEnum.Batch; temp.ListVM.NeedPage = false; } if (temp.LinkedVM != null) { temp.LinkedVM.CopyContext(rv); } if (temp.ListVM != null) { //绑定ListVM的OnAfterInitList事件,当ListVM的InitList完成时,自动将操作列移除 temp.ListVM.OnAfterInitList += (self) => { self.RemoveActionColumn(); self.RemoveAction(); if (temp.ErrorMessage.Count > 0) { self.AddErrorColumn(); } }; if (temp.ListVM.Searcher != null) { var searcher = temp.ListVM.Searcher; searcher.CopyContext(rv); if (passInit == false) { searcher.DoInit(); } } } temp.LinkedVM?.DoInit(); //temp.ListVM?.DoSearch(); } //如果ViewModel是ListVM,则初始化Searcher并调用Searcher的InitVM方法 if (rv is IBasePagedListVM <TopBasePoco, ISearcher> lvm) { var searcher = lvm.Searcher; searcher.CopyContext(rv); if (passInit == false) { //获取保存在Cookie中的搜索条件的值,并自动给Searcher中的对应字段赋值 string namePre = ConfigInfo.CookiePre + "`Searcher" + "`" + rv.VMFullName + "`"; Type searcherType = searcher.GetType(); var pros = searcherType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).ToList(); pros.Add(searcherType.GetProperty("IsValid")); Dictionary <string, string> cookieDic = HttpContext.Session.Get <Dictionary <string, string> >("SearchCondition" + searcher.VMFullName); if (cookieDic != null) { foreach (var pro in pros) { var name = namePre + pro.Name; if (cookieDic.ContainsKey(name) && !string.IsNullOrEmpty(cookieDic[name])) { try { if (cookieDic[name] == "`") { pro.SetValue(searcher, null); } else { PropertyHelper.SetPropertyValue(searcher, pro.Name, cookieDic[name], null, true); } } catch { } } } } searcher.DoInit(); } } if (rv is IBaseImport <BaseTemplateVM> tvm) { var template = tvm.Template; template.CopyContext(rv); template.DoInit(); } //自动调用ViewMode的InitVM方法 if (passInit == false) { rv.DoInit(); } return(rv); }
//-------------------------------------------方法------------------------------------// #region CreateVM /// <summary> /// Create a ViewModel, and pass Session,cache,dc...etc to the viewmodel /// </summary> /// <param name="VMType">The type of the viewmodel</param> /// <param name="Id">If the viewmodel is a BaseCRUDVM, the data having this id will be fetched</param> /// <param name="Ids">If the viewmodel is a BatchVM, the BatchVM's Ids property will be assigned</param> /// <param name="values">properties of the viewmodel that you want to assign values</param> /// <param name="passInit">if true, the viewmodel will not call InitVM internally</param> /// <returns>ViewModel</returns> private BaseVM CreateVM(Type VMType, object Id = null, object[] Ids = null, Dictionary <string, object> values = null, bool passInit = false) { //Use reflection to create viewmodel var ctor = VMType.GetConstructor(Type.EmptyTypes); BaseVM rv = ctor.Invoke(null) as BaseVM; try { rv.Session = new SessionServiceProvider(HttpContext.Session); } catch { } rv.ConfigInfo = ConfigInfo; rv.Cache = Cache; rv.LoginUserInfo = LoginUserInfo; rv.DataContextCI = ConfigInfo.ConnectionStrings.Where(x => x.Key.ToLower() == CurrentCS.ToLower()).Select(x => x.DcConstructor).FirstOrDefault(); rv.DC = this.DC; rv.MSD = new ModelStateServiceProvider(ModelState); rv.FC = new Dictionary <string, object>(); rv.CreatorAssembly = this.GetType().AssemblyQualifiedName; rv.CurrentCS = CurrentCS; rv.CurrentUrl = this.BaseUrl; rv.WindowIds = this.WindowIds; rv.UIService = this.UIService; rv.Log = this.Log; rv.Controller = this; rv.ControllerName = this.GetType().FullName; rv.Localizer = this.Localizer; if (HttpContext != null && HttpContext.Request != null) { try { if (Request.QueryString != null) { foreach (var key in Request.Query.Keys) { if (rv.FC.Keys.Contains(key) == false) { rv.FC.Add(key, Request.Query[key]); } } } if (HttpContext.Request.HasFormContentType) { var f = HttpContext.Request.Form; foreach (var key in f.Keys) { if (rv.FC.Keys.Contains(key) == false) { rv.FC.Add(key, f[key]); } } } } catch { } } //try to set values to the viewmodel's matching properties if (values != null) { foreach (var v in values) { PropertyHelper.SetPropertyValue(rv, v.Key, v.Value, null, false); } } //if viewmodel is derrived from BaseCRUDVM<> and Id has value, call ViewModel's GetById method if (Id != null && rv is IBaseCRUDVM <TopBasePoco> cvm) { cvm.SetEntityById(Id); } //if viewmodel is derrived from IBaseBatchVM<>,set ViewMode's Ids property,and init it's ListVM and EditModel properties if (rv is IBaseBatchVM <BaseVM> temp) { temp.Ids = new string[] { }; if (Ids != null) { var tempids = new List <string>(); foreach (var iid in Ids) { tempids.Add(iid.ToString()); } temp.Ids = tempids.ToArray(); } if (temp.ListVM != null) { temp.ListVM.CopyContext(rv); temp.ListVM.Ids = Ids == null ? new List <string>() : temp.Ids.ToList(); temp.ListVM.SearcherMode = ListVMSearchModeEnum.Batch; temp.ListVM.NeedPage = false; } if (temp.LinkedVM != null) { temp.LinkedVM.CopyContext(rv); } if (temp.ListVM != null) { //Remove the action columns from list temp.ListVM.OnAfterInitList += (self) => { self.RemoveActionColumn(); self.RemoveAction(); if (temp.ErrorMessage.Count > 0) { self.AddErrorColumn(); } }; temp.ListVM.DoInitListVM(); if (temp.ListVM.Searcher != null) { var searcher = temp.ListVM.Searcher; searcher.CopyContext(rv); if (passInit == false) { searcher.DoInit(); } } } temp.LinkedVM?.DoInit(); //temp.ListVM.DoSearch(); } //if the viewmodel is a ListVM, Init it's searcher if (rv is IBasePagedListVM <TopBasePoco, ISearcher> lvm) { var searcher = lvm.Searcher; searcher.CopyContext(rv); if (passInit == false) { searcher.DoInit(); } lvm.DoInitListVM(); } if (rv is IBaseImport <BaseTemplateVM> tvm) { var template = tvm.Template; template.CopyContext(rv); template.DoInit(); } //if passinit is not set, call the viewmodel's DoInit method if (passInit == false) { rv.DoInit(); } return(rv); }