I use unity as my IoC, and when the application loads, I set my initial DI to global.asax, which is required for my login screen.
When the user logs into system I, I install my main DI, which creates all the DI data for the rest of the application. One of the objects that I create is to save user data on the login screen, and I use this dto to enter my services, which will be used for authorization.
The problem is when the user logs out. I need to get rid of dto containing user details.
Is there any way to destroy an object in unity that is not called inside global.asax?
EDIT
In my class, bootstrapper, which is called from global.asax. I only need to install DI for security at this point.
Dim container As IUnityContainer = Bootstrapper.UnityContainer
SecurityContainer.RegisterContainer(container)
MiscContainer.RegisterMiscContainer(container)
DependencyResolver.SetResolver(New UnityDependencyResolver(container))
' Setup controller factory.
ControllerBuilder.Current.SetControllerFactory(New UnityControllerFactory(container))
In my RegisterContainer classes, they just have simple registrations, as shown below.
container.RegisterType(Of ILeadService, LeadService)(New ContainerControlledLifetimeManager())
In my method of entering mail in the controller, I have the following code. This will be sent to the security service for user authentication. If they are authenticated, I need to configure the next part of the DI, which is the rest of the application. The reason I should install this DI here instead of global.asax is because I need to use securityDto in all my other services.
Public Function LogOnWindow(loginViewModel As LoginViewModel) As ActionResult
Dim action As ActionResult = Nothing
' Authorise the user.
Dim securityResponse As ISecurityResponse = Me._securityService.AuthenticateUser(loginViewModel.UserName,
loginViewModel.Password)
' Check the response is successful.
If (securityResponse.IsSuccessful) Then
' Set the application id.
securityResponse.SecurityDto.CurrentDealerApplicationId = Me._applicationType
' Setup the bootstrapper with the security credentials.
Bootstrapper.InitialiseAfterLogin(securityResponse.SecurityDto)
' Redirect to the main application.
action = Me.RedirectToAction("KiaDashGrid",
"Kia",
New With {.Language = "en-gb"})
Else
' Create an error message.
loginViewModel.MessageManager.Merge(securityResponse.MessageManager.GetMessageCollection())
End If
If (action Is Nothing) Then
action = Me.View(loginViewModel)
End If
Return action
End Function
SecurityResponse.SecurityDto contains the user credentials that I insert into other application services.
An example of a constructor for a service using dto.
Public Class LeadService
Inherits ServiceBase
Implements ILeadService
Public Sub New(mapper As IMapper,
uow As IUnitOfWork,
leadRepository As ILeadRepository,
securityDTO As ISecurityDTO)
MyBase.New(securityDTO)
Me._mapper = mapper
Me._uow = uow
Me._leadRepository = leadRepository
Me._fetchStrategy = New FetchStrategy(Of Lead)()
End Sub
End Class
dto reset, .
.
<HttpGet()> _
Public Function LogOut() As ActionResult
' Need to clear the security dto credentials here.
Return Me.RedirectToAction("LogOnWindow",
"KiaLogin",
New With {.Language = "en-gb"})
End Function
, .
EDIT - 19/02/2014
PerSessionLifetimeManager, , gui.
bootstrapper , .
Dim manager As New PerSessionLifetimeManager()
manager.SetValue(securityDto)
container.RegisterType(Of ISecurityDTO, SecurityDTO)(manager)
container.RegisterInstance(securityDto, New PerSessionLifetimeManager())
PerSessionLifetimeManager.
Public Class PerSessionLifetimeManager
Inherits LifetimeManager
Private ReadOnly _key As Guid = Guid.NewGuid()
Public Sub New()
MyBase.New()
End Sub
Public Overrides Function GetValue() As Object
Return HttpContext.Current.Session(Me._key.ToString())
End Function
Public Overrides Sub RemoveValue()
HttpContext.Current.Session.Remove(Me._key.ToString())
End Sub
Public Overrides Sub SetValue(newValue As Object)
HttpContext.Current.Session(Me._key.ToString()) = newValue
End Sub
End Class