Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion LightInject.Mvc/LightInject.Mvc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ LightInject.Mvc version 1.0.0.3
http://seesharper.github.io/LightInject/
http://twitter.com/bernhardrichter
******************************************************************************/

[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed")]
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:PrefixLocalCallsWithThis", Justification = "No inheritance")]
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Single source file deployment.")]
Expand All @@ -38,6 +39,7 @@ namespace LightInject
using System.Reflection;
using System.Web.Mvc;
using LightInject.Mvc;


/// <summary>
/// Extends the <see cref="IServiceContainer"/> interface with a method that
Expand Down Expand Up @@ -81,6 +83,9 @@ public static void EnableMvc(this IServiceContainer serviceContainer)
((ServiceContainer)serviceContainer).EnablePerWebRequestScope();
SetDependencyResolver(serviceContainer);
InitializeFilterAttributeProvider(serviceContainer);

ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().First());
ModelValidatorProviders.Providers.Add(new LightInjectModelValidatorProvider(serviceContainer));
}

private static void SetDependencyResolver(IServiceContainer serviceContainer)
Expand Down Expand Up @@ -113,6 +118,7 @@ namespace LightInject.Mvc
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Reflection;

/// <summary>
/// An <see cref="IDependencyResolver"/> adapter for the LightInject service container.
Expand Down Expand Up @@ -194,4 +200,48 @@ public override IEnumerable<Filter> GetFilters(ControllerContext controllerConte
return filters;
}
}
}

/// <summary>
/// A Model Validator Provider for Light Inject
/// </summary>
internal class LightInjectModelValidatorProvider: DataAnnotationsModelValidatorProvider
{
/// <summary>
/// An Instance of a service container
/// </summary>
private readonly IServiceContainer _serviceContainer;

/// <summary>
/// Initializes a new instance of the <see cref="LightInjectModelValidatorProvider"/> class.
/// </summary>
internal LightInjectModelValidatorProvider(IServiceContainer serviceContainer)
{
_serviceContainer = serviceContainer;
}

/// <summary>
/// Gets a list of validators.
/// </summary>
/// <param name="metadata">The metadata.</param>
/// <param name="context">The context.</param>
/// <param name="attributes">The list of validation attributes.</param>
/// <returns>A list of validators.</returns>
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
var attributeMethodInfo =
typeof(DataAnnotationsModelValidator).GetMethod("get_Attribute", BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance);

var validators = base.GetValidators(metadata, context, attributes).ToList();
foreach (var modelValidator in validators.OfType<DataAnnotationsModelValidator>())
{
var attribute = attributeMethodInfo.Invoke(modelValidator, new object[0]);

if (attribute.GetType().GetConstructors().Any(x => !x.GetParameters().Any()))
{
_serviceContainer.InjectProperties(attribute);
}
}
return validators;
}
}
}
1 change: 1 addition & 0 deletions LightInject.Mvc/LightInject.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
Expand Down