示例#1
0
        /// <summary>
        /// Gets the model instance.
        /// </summary>
        /// <typeparam name="T">Type of the model.</typeparam>
        /// <param name="controllerType">The type of the controller for which the model is being resolved.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns>The model instance.</returns>
        public static T GetModel <T>(Type controllerType, IDictionary <string, object> constructorParameters = null)
        {
            using (var kernel = new StandardKernel())
            {
                var assemblies = ControllerModelFactory.GetTypeHierarchyAssemblies(controllerType);
                kernel.Load(assemblies);

                var parameters = new List <ConstructorArgument>();
                if (constructorParameters != null && constructorParameters.Any())
                {
                    foreach (var param in constructorParameters)
                    {
                        parameters.Add(new ConstructorArgument(param.Key, param.Value));
                    }
                }

                return(kernel.Get <T>(parameters.ToArray()));
            }
        }
示例#2
0
        /// <summary>
        /// Gets the model instance.
        /// </summary>
        /// <typeparam name="T">Type of the model.</typeparam>
        /// <param name="controllerType">The type of the controller for which the model is being resolved.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns>The model instance.</returns>
        public static T GetModel <T>(Type controllerType, IDictionary <string, object> constructorParameters = null)
        {
            if (controllerType == null)
            {
                throw new ArgumentNullException("controllerType");
            }

            ConstructorArgument[] parameters;
            if (constructorParameters != null && constructorParameters.Count > 0)
            {
                parameters = new ConstructorArgument[constructorParameters.Count];

                int i = 0;
                foreach (var param in constructorParameters)
                {
                    parameters[i] = new ConstructorArgument(param.Key, param.Value);
                    i++;
                }
            }
            else
            {
                parameters = new ConstructorArgument[0];
            }

            if (FrontendModule.Current != null)
            {
                return(FrontendModule.Current.DependencyResolver.Get <T>(parameters));
            }
            else
            {
                using (var kernel = new StandardKernel())
                {
                    kernel.Load(ControllerModelFactory.GetTypeHierarchyAssemblies(controllerType));
                    return(kernel.Get <T>(parameters));
                }
            }
        }