|
1 | | -using System; |
| 1 | +using System; |
2 | 2 | using System.Diagnostics; |
| 3 | +using Unity.Extension; |
3 | 4 |
|
4 | 5 | namespace Unity |
5 | 6 | { |
| 7 | + /// <summary> |
| 8 | + /// Diagnostic extension implements validating when calling <see cref="IUnityContainer.RegisterType"/>, |
| 9 | + /// <see cref="IUnityContainer.Resolve"/>, and <see cref="IUnityContainer.BuildUp"/> methods. When executed |
| 10 | + /// these methods provide extra layer of verification and validation as well |
| 11 | + /// as more detailed reporting of error conditions. |
| 12 | + /// </summary> |
| 13 | + /// <remarks> |
| 14 | + /// <para> |
| 15 | + /// Unity uses reflection to gather information about types, members, and parameters. |
| 16 | + /// It is quite obvious that it takes significant amount of time during execution. So, |
| 17 | + /// to optimize performance all these verifications where moved to the Diagnostic |
| 18 | + /// extension. It is recommended to include this extension only during |
| 19 | + /// development cycle and refrain from executing it in production |
| 20 | + /// environment. |
| 21 | + /// </para> |
| 22 | + /// <para> |
| 23 | + /// This extension can be registered in two ways: by adding an extension or by calling |
| 24 | + /// <c>EnableDiagnostic()</c> extension method on container. |
| 25 | + /// Adding extension to container will work in any build, where <c>EnableDiagnostic()</c> |
| 26 | + /// will only enable it in DEBUG mode. |
| 27 | + /// </para> |
| 28 | + /// </remarks> |
| 29 | + /// <example> |
| 30 | + /// <code> |
| 31 | + /// var container = new UnityContainer(); |
| 32 | + /// #if DEBUG |
| 33 | + /// container.AddExtension(new Diagnostic()); |
| 34 | + /// #endif |
| 35 | + /// </code> |
| 36 | + /// <code> |
| 37 | + /// var container = new UnityContainer(); |
| 38 | + /// container.EnableDiagnostic(); |
| 39 | + /// </code> |
| 40 | + /// </example> |
| 41 | + public class Diagnostic : UnityContainerExtension |
| 42 | + { |
| 43 | + protected override void Initialize() |
| 44 | + { |
| 45 | + ((UnityContainer)Container).SetDefaultPolicies = UnityContainer.SetDiagnosticPolicies; |
| 46 | + ((UnityContainer)Container).SetDefaultPolicies((UnityContainer)Container); |
| 47 | + } |
| 48 | + } |
| 49 | + |
6 | 50 | public static class DiagnosticExtensions |
7 | 51 | { |
8 | 52 | /// <summary> |
@@ -54,9 +98,10 @@ public static void EnableDebugDiagnostic(this UnityContainer container) |
54 | 98 | public static UnityContainer EnableDiagnostic(this UnityContainer container) |
55 | 99 | { |
56 | 100 | if (null == container) throw new ArgumentNullException(nameof(container)); |
57 | | - |
| 101 | + |
58 | 102 | container.AddExtension(new Diagnostic()); |
59 | 103 | return container; |
60 | 104 | } |
61 | 105 | } |
| 106 | + |
62 | 107 | } |
0 commit comments