UnityMvcActivator or global.asax
Asked Answered
B

1

8

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?

Bunker answered 14/11, 2017 at 1:26 Comment(0)
D
0

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.

Dardanelles answered 19/9, 2024 at 6:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.