ASP.NET MVC 2 - Property Injection

May 28, 2010 at 7:02 PM

Hi,

I've tried use Setter injection but it seems not work. I have controller

public class CourseController : BaseController
{
        [Dependency]
        public ICourseService service { get; set; }
}

and in Global.asax I've initialized unity container

protected void Application_Start()
{
    GolfTracker.Mvc.Web.Unity.Bootstrapper.Configure();

    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
}
GolfTracker.Mvc.Web.Unity.Bootstrapper.Configure():
public class Bootstrapper
    {
        private static IUnityContainer _container;

        private static void Configure(IUnityContainer container)
        {
            container
                .RegisterType< ICourseRepository, CourseRepository >()
                .RegisterType< ICourseService, CourseService >();

            UnityFactoryBuilder.Init(container);
        }

        public static void Configure()
        {
            if ( _container == null )
            {
                _container = new UnityContainer();
            }
            Configure(_container);
        }
    }
UnityFactoryBuilder.Init(container):
public class UnityFactoryBuilder
{
    public static void Init(IUnityContainer container)
    {
        IControllerFactory controllerFactory = new UnityControllerFactory(container);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }
}
But when I call service.GetAll in CourseController Index action I got NullReferenceException ...so I don't know why ?