Reputation: 337
I have two questions. I installed Unity and Unity MVC packages to start investigating it. I got some simple controller constructor injection working by registering some types, so it's going well. I notice that it's starting before even global.asax is hit due to:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(example.org.UnityMvcActivator), nameof(example.org.UnityMvcActivator.Start))]
Question 1: What is even the name for this syntax? Some kind of in-line assembly reference that attaches a type/method here to elsewhere? I've never seen this before.
Question 2: The UnityMvcActivator has Start() and Shutdown() methods corresponding to the [assembly] lines above, the latter of which just calls dispose on the container. The central element of Start() is calling:
DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));
Is there a compelling reason this UnityMvcActivator exists, like the dispose(), or maybe it needs to start as early as it does OR is it just boilerplate scaffolding to get me started, and I can just as well put that SetResolver() line into my global.asax and then remove this class?
Upvotes: 8
Views: 1325
Reputation: 233150
What is even the name for this syntax? Some kind of in-line assembly reference that attaches a type/method here to elsewhere? I've never seen this before.
This is called an assembly attribute. They've been around as long as I recall (and I've been doing .NET development since .NET 1.1).
Is there a compelling reason this UnityMvcActivator exists
I can't think of any. No other DI Container that I know of works like this, and you can always get by with Pure DI and a Composition Root instead of all that extra complexity.
Upvotes: 0