question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Mvc6 + AutoFac

See original GitHub issue

IMO dependency injection via ctor is fine (better), but I did also some experiments with property setting.

Got 2 FV related issues:

  1. the OnActivating event was raised always after the resolved object was used within IValidator<T>
  2. when I was trying SingleInstance, only the 1st call triggered validation

No1. The issue is FV and architecture related. If I do add a constructor to the validator and reference there a differrent validator, then that 2nd validator gets resolved, and the OnActivating event on that second validator get’s called… so this is fine: public EmployeeDetailsDtoValidator(IValidator<EmployeeAddressDto> employeeAddressDtoValidator, IValidatorFactory f)

But the behavior, when the main IValidator<> is resolved via attribute (FVs MVC infrastructure) is differrent.

No2. I can understand that validators for some reasons must exist per UC. If so, I do really miss this kinda of information from the documentation.

Some samples:

Classic registration:

services.AddMvc().AddFluentValidation(fv => { fv.ValidatorFactoryType = typeof(AttributedValidatorFactory); });

Then separate (manual) registration with property setter, so if IValidator<T> has an IHasFactoryProperty interface, then Factory property will be set, thus allowing to access other objects…

builder
	.RegisterAssemblyTypes(validationAssemblies)
	.Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
	.AsImplementedInterfaces()
	.OnActivating(e =>
		{
			if (e.Instance is IHasFactoryProperty)
				(e.Instance as IHasFactoryProperty).Factory = e.Context.Resolve<IValidatorFactory>();
		})
	.InstancePerLifetimeScope();

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
AndreiMazcommented, Dec 28, 2017

@JeremySkinner Just integrated it in the upcoming version of nopCommerce (https://github.com/nopSolutions/nopCommerce/commit/1f3b2f0935a76cd0c8ce81585430e7b1b527d241). Everything works fine. Thanks again!

0reactions
AndreiMazcommented, Dec 14, 2017

@JeremySkinner Thanks a lot, Jeremy! We’ll use in out next version of nopCommerce

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting autofac to work with mvc6 beta5
I'm using mvc6 beta5 + latest version of Autofac from the myget feed and it's resulting in the following exception: System.
Read more >
MVC — Autofac 7.0.0 documentation
The MVC integration includes an Autofac module that will add HTTP request lifetime scoped registrations for the web abstraction classes. This will allow...
Read more >
mvc
It looks like ASP.NET MVC 1.0 has been released and is available for download. Read more... autofac (26) web-services (23) wscf (15) links...
Read more >
How to use built-in dependency inversion in MVC6 and ASP ...
Examples of containers I've used are NInject.MVC, Autofac, and Sprint.NET. In MVC6, Microsoft has entered this field, by including a simple ...
Read more >
Dependency Injection (DI) in ASP.NET MVC 6
In our application we will be using Autofac. This is an addictive IOC container for .NET 4.5 apps with support for DNX. More...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found