4

I am trying to use dependencyinjection in Controller and using System.Web.MvcDependencyResolver.Current.GetService() for creating service instances in controller class.

This works fine when Service Interfaces are non generic as below

public interface IProfileManagementService
{
   IList<News> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}

and my dependency resolver syntax as below gives me instance of ProfileManagementService

DependencyResolver.Current.GetService<IProfileManagementService>();

But If I create any generic service interface as below,

public interface ICommonProfileManagementService<T>
{
   IList<T> GetSavedSearchList(int userObjectId, ApplicationType applicationType,int? vendorUserObjectId);
}

But I get a null (CommonProfileManagementService objects are not created) for below code

DependencyResolver.Current.GetService<ICommonProfileManagementService<News>>();

Please Suggest some alternate ways of passing

IService<T>

instead of

IService

to DependencyResolver.Current.GetService()

aditi
  • 73
  • 1
  • 6
  • What is your current `Resolver`? How do you actually register the implementation of the interface? – haim770 Jun 23 '14 at 13:22

1 Answers1

3

Here is the full code including the syntax needed to return a generic from DependencyResolver.

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;

namespace ConsoleApplication2
{
    class Program
    {
        public interface IService<T>
        {
            List<T> GetService();
        }

        public class Service<T> : IService<T>
        {
            public List<T> GetService()
            {
                return new List<T>();
            }
        }

        static void Main(string[] args)
        {
            var container = new UnityContainer();

            container.RegisterType(typeof(IService<>), typeof(Service<>));

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            var service = DependencyResolver.Current.GetService(typeof(IService<string>));

            Console.WriteLine(service.ToString());

            Console.ReadKey();
        }
    }
}
Mike Dymond
  • 1,165
  • 9
  • 22
  • I tried this and it says interface name is not valid at this point – aditi Jun 20 '14 at 16:38
  • Where are you configuring your DI container? – Mike Dymond Jun 20 '14 at 16:53
  • 1
    Also, when you say, 'it says' do you mean that Visual Studio is reporting this error and hence not allowing you to compile. Or is that a run time error? – Mike Dymond Jun 20 '14 at 16:54
  • With the syntax it throws compile time error.Also I didnt configure any DI container. . DependencyResolver has defined an extension method GetService () that allows to pass interface name only.but it can not accept T along with the interface. Do I need to configure DI container and initialize that in appstart – aditi Jun 23 '14 at 04:36
  • Sorry, I meant that you should replace T with the actual type that you want to create the generic for. However you also need to implement a container and register that with DependencyResolver otherwise it will always return null. I will create a sample app and post the code here. – Mike Dymond Jun 23 '14 at 11:17