2

I use SimpleInjector in mvc with this code

public static class SimpleInjectorInitializer
{
    public static void Initialize()
    {
        var container = new Container();
        InitializeContainer(container);
        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
        container.Verify();
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    }

    private static void InitializeContainer(Container container)
    {
        container.Register<IPersonRepository, PersonEntityRepository>();
    }
}

but now I want to test LightInject Ioc in my mvc application but How ?

I wrote this code

[assembly: WebActivator.PostApplicationStartMethod(typeof(LightInjectInitializer), "InitializeLI")]

public static class LightInjectInitializer
{
    public static void InitializeLI()
    {
        var container = new ServiceContainer();            
        InitializeContainer(container);
        container.RegisterControllers(Assembly.GetExecutingAssembly());
        container.EnableMvc();
    }

    private static void InitializeContainer(ServiceContainer container)
    {
        container.Register(typeof(PersonEntityRepository), typeof(IPersonRepository), new PerScopeLifetime());

    }
}

enter image description here

but show me this error

No parameterless constructor defined for this object. 

Can anyone help me for writing equivalent code that working in MVC ?

Sample Code : Download

Hamed F
  • 800
  • 3
  • 11
  • 23
  • Any particular reason you are switching to Light Inject? Any feature in particular that you are missing in Simple Injector? – Steven Aug 18 '14 at 19:17
  • please see benchmark http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison – Hamed F Aug 19 '14 at 10:57
  • 1
    I'm familiar with that benchmark. Is your only reason one of performance? Do note that LightInject is just marginally faster than Simple Injector. You will see no measurable performance difference in any real-life application. – Steven Aug 19 '14 at 11:08
  • I agree with what you said - But be happy if I learn how to work with the library - Can you resolve my problem or not ? – Hamed F Aug 19 '14 at 14:10
  • No, I can not. I built Simple Injector and can tell you anything about that. I have no experience with LightInject. – Steven Aug 19 '14 at 14:11
  • 1
    wowowow, simpleinjector is awesome , I love it – Hamed F Aug 19 '14 at 14:14

2 Answers2

4

I am the author of LightInject and would like to help you out.

This static initialize method? Where is it called from. The reason that you get this error is probably because the initialize code did not execute.

If you would like further assistance, it would be valuable to see what you have in global.asax.cs.

This page also contains information about how to set this up.

http://www.lightinject.net/#mvc

Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
seesharper
  • 3,249
  • 1
  • 19
  • 23
0

Please change the controller registration line to:

container.RegisterControllers(typeof(MVCApplicationNamespace.Controllers.HomeController).Assembly);
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
simba
  • 21
  • 4