示例#1
0
        ///// <summary> Implements <see cref="IModelDefinitionManager.GetModelType"/> </summary>
        //public void ResolveModel()
        //{
        //    try
        //    {
        //        Logger.Instance.Log(LogType.Info, "Starting the model resolution...");

        //        bool onError = false;
        //        if (modelTypeByName != null)
        //        {
        //            foreach (IModelType type in modelTypeByName.Values)
        //            {
        //                try
        //                {
        //                    (type as ModelType).ResolveDependencies();
        //                }
        //                catch
        //                { onError = true; }
        //            }
        //        }

        //        if (onError)
        //            throw new EtkException("Error(s) found");
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new EtkException(string.Format("Model resolution failed: {0}. Please check the log.", ex.Message));
        //    }
        //    finally
        //    {
        //        Logger.Instance.Log(LogType.Info, "Model resolution finished.");
        //    }
        //}
        #endregion

        #region protected methods
        //protected abstract IModelAccessor CreateModelAccessors(XmlModelAccessor modelAccessorDefinition);

        protected void PopulateFromXmlModelConfiguration(XmlModelConfiguration xmlModelConfiguration)
        {
            lock (syncObj)
            {
                if (xmlModelConfiguration.ModelAccessorGroupDefinitions != null)
                {
                    HasModels = true;
                    foreach (XmlModelAccessorGroup xmlGroup in xmlModelConfiguration.ModelAccessorGroupDefinitions)
                    {
                        //IModelAccessor accessor = CreateModelAccessors(modelAccessorDefinition);
                        IModelAccessorGroup group = ModelAccessorGroup.CreateInstance(this, xmlGroup);
                        if (group != null)
                        {
                            if (ModelAccessorGroupByName.ContainsKey(group.Name))
                            {
                                Logger.Instance.LogFormat(LogType.Warn, "The model accessor group '{0}' is declared more than once.", group.Name);
                            }
                            ModelAccessorGroupByName[group.Name] = group;
                        }
                    }
                }

                if (xmlModelConfiguration.TypeDefinitions != null)
                {
                    foreach (XmlModelType typeDefinition in xmlModelConfiguration.TypeDefinitions)
                    {
                        ModelType modelType = ModelType.CreateInstance(this, typeDefinition);
                        if (modelType != null)
                        {
                            if (ModelTypeByName.ContainsKey(modelType.Name.ToUpper()))
                            {
                                Logger.Instance.LogFormat(LogType.Warn, "The model UnderlyingType '{0}' is declared more than once.", modelType.Name);
                            }
                            ModelTypeByName[modelType.Name.ToUpper()] = modelType;
                        }
                    }
                }

                // Resolve model accessorsNames dependencies
                foreach (ModelAccessor modelAccessor in ModelAccessorByIdent.Values)
                {
                    modelAccessor.ResolveDependencies();
                }

                // Resolve model types dependencies
                foreach (ModelType modelType in ModelTypeByName.Values.ToList())
                {
                    modelType.ResolveDependencies();
                }

                // Resolve views dependencies of the model types
                foreach (ModelType modelType in ModelTypeByName.Values.ToList())
                {
                    modelType.ResolveViewsDependencies();
                }
            }
        }
示例#2
0
        public static IModelAccessor CreateInstance(IModelAccessorGroup parent, XmlModelAccessor definition)
        {
            if (definition == null)
            {
                return(null);
            }

            ModelAccessor accessor = new ModelAccessor();

            try
            {
                accessor.Parent = parent;
                accessor.Name   = definition.Name.EmptyIfNull().Trim();
                accessor.Ident  = definition.Ident.EmptyIfNull().Trim();
                if (string.IsNullOrEmpty(accessor.Ident))
                {
                    accessor.Ident = accessor.Name;
                }

                accessor.Description = definition.Description.EmptyIfNull().Trim();
                accessor.modelType   = definition.ReturnModelType.EmptyIfNull().Trim();

                definition.InstanceName = definition.InstanceName.EmptyIfNull().Trim();
                definition.InstanceType = definition.InstanceType.EmptyIfNull().Trim();
                definition.Method       = definition.Method.EmptyIfNull().Trim();

                definition.Method = definition.Method.EmptyIfNull().Trim();

                if (string.IsNullOrEmpty(accessor.Name))
                {
                    throw new EtkException("'Name' is mandatory");
                }
                if (string.IsNullOrEmpty(definition.Method))
                {
                    throw new EtkException("'Method' is mandatory");
                }

                DataAccessorInstanceType dataAccessorInstanceType = ModelManagement.DataAccessors.DataAccessor.AccessorInstanceTypeFrom(definition.InstanceType);
                accessor.DataAccessor = ModelManagement.DataAccessors.DataAccessor.CreateInstance(definition.Method, dataAccessorInstanceType, definition.InstanceName);
            }
            catch (Exception ex)
            {
                throw new EtkException($"Cannot create 'ModelAccessor' '{definition.Name.EmptyIfNull()} {definition.Method.EmptyIfNull()}': {ex.Message}");
            }
            return(accessor);
        }