Gauls
Gauls

Reputation: 1965

How to Inject a parameter to constructor using unity

Hi I am using unity in WebAPI 1.0 and registered it under Global.asax.vb file as below

Dim container As IUnityContainer = New UnityContainer()

container.RegisterType(Of Car)(New InjectionConstructor(GetType(Integer)))

Now how do i pass integer value which is passed by the client application into car type using

Public Sub New(ByVal intUserId As Int32)
    objCar = DependencyResolver.Current.GetService(Of car)()     

End Sub

Can't find anything to use (ParameterOverride("intUserId", intUserId) within DependencyResolver

Upvotes: 2

Views: 798

Answers (1)

Sameer Azazi
Sameer Azazi

Reputation: 1497

It is very similar as registering unity container with ASP.NET MVC. Though web api has a different execution pipe line. Follow these steps.

NB: I have converted this code from C# to VB using a converter. So I hope it is syntactically correct. Though it is accurate in c#.

1) Implement IDependencyResolver (Make sure you are resolving correct namespace here. IDependencyResolver should come from System.Web.Http.Dependencies. Make note of Imported namespaces.

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports Microsoft.Practices.Unity
Imports System.Web.Http.Dependencies

Namespace YourNamespace.Framework
    Public Class UnityApiDependencyResolver
        Implements IDependencyResolver

        Private _container As IUnityContainer = Nothing

        Public Sub New(container As IUnityContainer)
            Me._container = container
        End Sub

        Public Function GetService(serviceType As Type) As Object
            Try
                Return _container.Resolve(serviceType)
            Catch generatedExceptionName As Exception

                Return Nothing
            End Try
        End Function

        Public Function GetServices(serviceType As Type) As IEnumerable(Of Object)
            Try
                Return _container.ResolveAll(serviceType)
            Catch generatedExceptionName As Exception

                Return Nothing
            End Try
        End Function

        Public Function BeginScope() As IDependencyScope
            Return Me
        End Function

        Public Sub Dispose()
            '
        End Sub
    End Class
End Namespace

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

2) Configure your container either thgough config or through code.

3) Register your container in Global.asax.vb file

Dim container = New UnityContainer()
Dim section As UnityConfigurationSection = TryCast(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)

section.Configure(container, "UnitySection")

'api dependency resolver
GlobalConfiguration.Configuration.DependencyResolver = New UnityApiDependencyResolver(container)

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

Thats it. Now you can declare your dependency in any of your API controller and it will be injected by Unity

Public Sub New(repositoryFactory As IRepositoryFactory, serviceFactory As IServiceFactory)
    Me.repositoryFactory = repositoryFactory
    Me.serviceFactory = serviceFactory
End Sub

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

Your type registration is wrong as well. you have to specify either an interface or an abstract class as dependency and its concrete implementation as its mapping.

e.g container.RegisterType(Of IContext, Context)()

I don't understand what are trying to achieve by mapping an integer value to car. Do you want car object to be loaded based on integer value in your parameter?

Upvotes: 1

Related Questions