diff --git a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerFactory.cs b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerFactory.cs index dbfd211ff2..b6f0fd477a 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DefaultControllerFactory.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DefaultControllerFactory.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Reflection; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.Mvc.ModelBinding; @@ -48,44 +47,20 @@ namespace Microsoft.AspNet.Mvc private void InitializeController(object controller, ActionContext actionContext) { - var controllerType = controller.GetType(); + Injector.InjectProperty(controller, "ActionContext", actionContext); - foreach (var prop in controllerType.GetRuntimeProperties()) - { - if(prop.Name == "ActionContext" && - prop.PropertyType.GetTypeInfo().IsAssignableFrom(typeof(ActionContext).GetTypeInfo())) - { - prop.SetValue(controller, actionContext); - } - else if (prop.Name == "ViewData" && - prop.PropertyType.GetTypeInfo().IsAssignableFrom(typeof(ViewDataDictionary).GetTypeInfo())) - { - prop.SetValue(controller, new ViewDataDictionary( - _serviceProvider.GetService(), actionContext.ModelState)); - } - else if (prop.Name == "Url" && - prop.PropertyType.GetTypeInfo().IsAssignableFrom(typeof(IUrlHelper).GetTypeInfo())) - { - var urlHelper = new UrlHelper( - actionContext.HttpContext, - actionContext.Router, - actionContext.RouteValues); + var viewData = new ViewDataDictionary( + _serviceProvider.GetService(), + actionContext.ModelState); + Injector.InjectProperty(controller, "ViewData", viewData); - prop.SetValue(controller, urlHelper); - } - } + var urlHelper = new UrlHelper( + actionContext.HttpContext, + actionContext.Router, + actionContext.RouteValues); + Injector.InjectProperty(controller, "Url", urlHelper); - var method = controllerType.GetRuntimeMethods().FirstOrDefault(m => m.Name.Equals("Initialize", StringComparison.OrdinalIgnoreCase)); - - if (method == null) - { - return; - } - - var args = method.GetParameters() - .Select(p => _serviceProvider.GetService(p.ParameterType)).ToArray(); - - method.Invoke(controller, args); + Injector.CallInitializer(controller, _serviceProvider); } } } diff --git a/src/Microsoft.AspNet.Mvc.Core/Injector.cs b/src/Microsoft.AspNet.Mvc.Core/Injector.cs new file mode 100644 index 0000000000..461550241d --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Core/Injector.cs @@ -0,0 +1,53 @@ +using System; +using System.Linq; +using System.Reflection; +using Microsoft.AspNet.DependencyInjection; + +namespace Microsoft.AspNet.Mvc +{ + public static class Injector + { + public static void CallInitializer([NotNull] object obj, [NotNull] IServiceProvider services) + { + var type = obj.GetType(); + + var initializeMethod = + type.GetRuntimeMethods() + .FirstOrDefault(m => m.Name.Equals("Initialize", StringComparison.OrdinalIgnoreCase)); + + if (initializeMethod == null) + { + return; + } + + var args = + initializeMethod.GetParameters() + .Select(p => services.GetService(p.ParameterType)) + .ToArray(); + + initializeMethod.Invoke(obj, args); + } + + public static void InjectProperty([NotNull] object obj, [NotNull] string propertyName, TProperty value) + { + var type = obj.GetType(); + + var property = type.GetProperty(propertyName); + if (property.PropertyType.IsAssignableFrom(typeof(TProperty))) + { + property.SetValue(obj, value); + } + } + + public static void InjectProperty([NotNull] object obj, [NotNull] string propertyName, [NotNull] IServiceProvider services) + { + var type = obj.GetType(); + + var property = type.GetProperty(propertyName); + if (property.PropertyType.IsAssignableFrom(typeof(TProperty))) + { + property.SetValue(obj, services.GetService()); + } + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs index 595266f6d9..0209dcc7f3 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ViewComponents/DefaultViewComponentInvoker.cs @@ -1,6 +1,5 @@  using System; -using System.Linq; using System.Reflection; using System.Runtime.ExceptionServices; using System.Threading.Tasks; @@ -75,33 +74,14 @@ namespace Microsoft.AspNet.Mvc var activator = _serviceProvider.GetService(); object component = activator.CreateInstance(_serviceProvider, _componentType.AsType()); - foreach (var prop in _componentType.AsType().GetRuntimeProperties()) - { - if (prop.Name == "ViewContext" && - typeof(ViewContext).GetTypeInfo().IsAssignableFrom(prop.PropertyType.GetTypeInfo())) - { - prop.SetValue(component, context); - } - else if (prop.Name == "ViewData" && - typeof(ViewDataDictionary).GetTypeInfo().IsAssignableFrom(prop.PropertyType.GetTypeInfo())) - { - // We're flowing the viewbag across, but the concept of model doesn't really apply here - var viewData = new ViewDataDictionary(context.ViewData); - viewData.Model = null; + Injector.InjectProperty(component, "ViewContext", context); - prop.SetValue(component, viewData); - } - } + // We're flowing the viewbag across, but the concept of model doesn't really apply here + var viewData = new ViewDataDictionary(context.ViewData); + viewData.Model = null; + Injector.InjectProperty(component, "ViewData", viewData); - var method = _componentType.AsType().GetRuntimeMethods() - .FirstOrDefault(m => m.Name.Equals("Initialize", StringComparison.OrdinalIgnoreCase)); - if (method != null) - { - var args = method.GetParameters() - .Select(p => _serviceProvider.GetService(p.ParameterType)).ToArray(); - - method.Invoke(component, args); - } + Injector.CallInitializer(component, _serviceProvider); return component; }