private void DoValidation(IMvcModule modelInstance, string parent, int? listIndex, ref List<PropertyModel> validationInfo) { // get runtime type. Type modelType = modelInstance.GetType(); // get all properties of the object. PropertyInfo[] properties = modelType.GetProperties(); foreach (PropertyInfo propertyInfo in properties) { object propertyValue = null; // todo: need to do filter those are not supported type // is GenericType like: List<T>, current we only support List<T> & IList<T>. if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetInterface(typeof(IList<>).FullName) != null) { // we only support onetime loop. if (!string.IsNullOrEmpty(parent)) continue; // get list of GenericType, type of <T>. Type[] types = propertyInfo.PropertyType.GetGenericArguments(); // check if the GenericType implements IMvcModule interface? Type tType = types.FirstOrDefault(p => p.GetInterface(typeof(IMvcModule).FullName) != null); if (tType != null) { // reflect value of the property propertyValue = propertyInfo.GetValue(modelInstance, null); // we only support list, all list implements interface IEnumerable. IEnumerable enumerable = propertyValue as IEnumerable; if (null != enumerable) { int i = 0; foreach (object instance in enumerable) { DoValidation(instance as IMvcModule, propertyInfo.Name, i, ref validationInfo); i++; } } } } else { MvcValidationAttribute[] attributes = GetValidationAttributes(parent, propertyInfo); if (attributes.Length != 0) { // reflect value of the property propertyValue = propertyInfo.GetValue(modelInstance, null); foreach (MvcValidationAttribute attribute in attributes) { // check if it is ingored if (!string.IsNullOrEmpty(attribute.IngoreTaret)) { object theValue = GetValueByPropertyName(modelType, modelInstance, attribute.IngoreTaret); // ingored? if (theValue != null && string.Equals(theValue.ToString(), attribute.IngoreValue, StringComparison.CurrentCultureIgnoreCase)) continue; } // pre-fill ValueToCompare before checking if (attribute.ValidationType == MvcValidationType.MvcCompare) (attribute as MvcCompareAttribute).ValueToCompare = GetValueByPropertyName(modelType, modelInstance, (attribute as MvcCompareAttribute).CompareToValue); // check IsValid? if (!attribute.IsValid(propertyValue)) { validationInfo.Add(new PropertyModel(propertyInfo.Name, attribute.ErrorMessage, attribute.ValidationType, parent, listIndex)); break; } } } } } }
public void ActivateModule(ModuleType moduleType) { //only one thread at a time System.Threading.Monitor.Enter(lockObject); string assemblyQualifiedName = moduleType.ClassName + ", " + moduleType.AssemblyName; if (log.IsDebugEnabled) { log.DebugFormat("Loading module {0}.", assemblyQualifiedName); } // First, try to get the CLR module type Type moduleTypeType = Type.GetType(assemblyQualifiedName); if (moduleTypeType == null) { throw new Exception("Could not find module: " + assemblyQualifiedName); } try { // double check, if we should continue if (this._kernel.HasComponent(moduleTypeType)) { // Module is already registered if (log.IsDebugEnabled) { log.DebugFormat("The module with type {0} is already registered in the container.", moduleTypeType.ToString()); } return; } // First, register optional module services that the module might depend on. foreach (ModuleService moduleService in moduleType.ModuleServices) { Type serviceType = Type.GetType(moduleService.ServiceType); Type classType = Type.GetType(moduleService.ClassType); if (log.IsDebugEnabled) { log.DebugFormat("Loading module service {0}, (1).", moduleService.ServiceKey, moduleService.ClassType); } LifestyleType lifestyle = LifestyleType.Singleton; if (moduleService.Lifestyle != null) { try { lifestyle = (LifestyleType)Enum.Parse(typeof(LifestyleType), moduleService.Lifestyle); } catch (ArgumentException ex) { throw new Exception(String.Format("Unable to load module service {0} with invalid lifestyle {1}." , moduleService.ServiceKey, moduleService.Lifestyle), ex); } } this._kernel.AddComponent(moduleService.ServiceKey, serviceType, classType, lifestyle); } //Register the module string moduleTypeKey = "module." + moduleTypeType.FullName; this._kernel.AddComponent(moduleTypeKey, moduleTypeType); // no lifestyle because ModuleBase has the Transient attribute. //Configure NHibernate mappings and make sure we haven't already added this assembly to the NHibernate config if (typeof(INHibernateModule).IsAssignableFrom(moduleTypeType) && ((HttpContext.Current.Application[moduleType.AssemblyName]) == null)) { if (log.IsDebugEnabled) { log.DebugFormat("Adding module assembly {0} to the NHibernate mappings.", moduleTypeType.Assembly.ToString()); } this._sessionFactoryHelper.AddAssembly(moduleTypeType.Assembly); //set application variable to remember the configurated assemblies HttpContext.Current.Application.Lock(); HttpContext.Current.Application[moduleType.AssemblyName] = moduleType.AssemblyName; HttpContext.Current.Application.UnLock(); } if (typeof(IMvcModule).IsAssignableFrom(moduleTypeType)) { IMvcModule module = this._kernel.Resolve <IMvcModule>(moduleTypeKey); // Add routes to the routetable if the module supports MVC module.RegisterRoutes(RouteTable.Routes); // Register module controllers this._kernel.Register(AllTypes .Of(typeof(IController)) .FromAssembly(moduleTypeType.Assembly) .Configure(c => c.LifeStyle.Transient)); } // Register model validators for modules this._kernel.Register(AllTypes .Of <IModelValidator>() .FromAssembly(moduleTypeType.Assembly) .Configure(c => c.LifeStyle.Transient)); } finally { System.Threading.Monitor.Exit(lockObject); } } //end method