Is IDependencyResolver an anti-pattern?

2019-01-07 11:19发布

问题:

I am designing some architectural changes into a legacy ASP.NET application. I prototyped some classes for dependency resolution that mimic the ASP.NET MVC's IDependencyResolver. I won't post because it is pretty much the same interface, but in other natural language.

I figured out it might be considered Service Location, which in turn is usually (not fully in some cases) condemned in favor of Dependency Injection. Nevertheless, I couldn't find any recommendation against the use of the ASP.NET MVC's dependency resolution implementation.

Is the ASP.NET MVC's IDependencyResolver considered an anti-pattern? Is it a bad thing?

回答1:

If you look at the signature you will see that it's just a Service Locator with another name. Service Locator is an anti-pattern and I consider the relationship transitive, so I consider IDependencyResolver an anti-pattern.

Apart from that, the interface is also broken because it has no Release method.



回答2:

I don't believe so... You can inject any IoC you want into ASP.NET MVC, which seems like a pretty good pattern to me.

Here's a blog post about injecting Unity into ASP.NET MVC 3.