Last active
August 29, 2015 14:10
-
-
Save KatsuYuzu/ce848eff31dc5a024cee to your computer and use it in GitHub Desktop.
Unity.Mvc - UnityConfig.vb, UnityMvcActivator.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System | |
Imports Microsoft.Practices.Unity | |
Imports Microsoft.Practices.Unity.Configuration | |
''' <summary> | |
''' Specifies the Unity configuration for the main container. | |
''' </summary> | |
Public Class UnityConfig | |
#Region "Unity Container" | |
Private Shared ReadOnly _Container As Lazy(Of IUnityContainer) = New Lazy(Of IUnityContainer)( | |
Function() | |
Dim container = New UnityContainer() | |
RegisterTypes(container) | |
Return container | |
End Function) | |
''' <summary> | |
''' Gets the configured Unity container. | |
''' </summary> | |
Public Shared Function GetConfiguredContainer() As IUnityContainer | |
Return _Container.Value | |
End Function | |
#End Region | |
''' <summary>Registers the type mappings with the Unity container.</summary> | |
''' <param name="container">The unity container to configure.</param> | |
''' <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to | |
''' change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks> | |
Public Shared Sub RegisterTypes(container As IUnityContainer) | |
' NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements. | |
' container.LoadConfiguration() | |
' TODO: Register your types here | |
' container.RegisterType(Of IProductRepository, ProductRepository)() | |
End Sub | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Linq | |
Imports System.Web.Mvc | |
Imports Microsoft.Practices.Unity.Mvc | |
Imports Microsoft.Web.Infrastructure.DynamicModuleHelper | |
<Assembly: WebActivatorEx.PreApplicationStartMethod(GetType(UnityWebActivator), "Start")> | |
<Assembly: WebActivatorEx.ApplicationShutdownMethod(GetType(UnityWebActivator), "Shutdown")> | |
''' <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary> | |
Public NotInheritable Class UnityWebActivator | |
''' <summary>This is static class.</summary> | |
Private Sub New() | |
End Sub | |
''' <summary>Integrates Unity when the application starts.</summary> | |
Public Shared Sub Start() | |
Dim container = UnityConfig.GetConfiguredContainer() | |
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType(Of FilterAttributeFilterProvider)().First()) | |
FilterProviders.Providers.Add(New UnityFilterAttributeFilterProvider(container)) | |
DependencyResolver.SetResolver(New UnityDependencyResolver(container)) | |
' TODO: Uncomment if you want to use PerRequestLifetimeManager | |
' DynamicModuleUtility.RegisterModule(GetType(UnityPerRequestHttpModule)) | |
End Sub | |
''' <summary>Disposes the Unity container when the application is shut down.</summary> | |
Public Shared Sub Shutdown() | |
Dim container = UnityConfig.GetConfiguredContainer() | |
container.Dispose() | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment