Search code examples
asp.net-web-apininjectravendbsimple-injectormembershipreboot

Membership reboot replace Ninject with Simple Injector


I need add membership reboot (RavenDb) into the project that use IOC Simple Injector

Ninject implementation

var config = MembershipRebootConfig.Create();
kernel.Bind<MembershipRebootConfiguration<HierarchicalUserAccount>>().ToConstant(config);
kernel.Bind<UserAccountService<HierarchicalUserAccount>>().ToSelf();   kernel.Bind<AuthenticationService<HierarchicalUserAccount().To<SamAuthenticationService<HierarchicalUserAccount>>();
kernel.Bind<IUserAccountRepository<HierarchicalUserAccount>>().ToMethod(ctx => new BrockAllen.MembershipReboot.RavenDb.RavenUserAccountRepository("RavenDb"));
kernel.Bind<IUserAccountQuery>().ToMethod(ctx => new BrockAllen.MembershipReboot.RavenDb.RavenUserAccountRepository("RavenDb"));

Simple Injector implementation

container.Register(MembershipRebootConfig.Create);
container.Register<UserAccountService<HierarchicalUserAccount>>();
container.Register<AuthenticationService<HierarchicalUserAccount>, SamAuthenticationService<HierarchicalUserAccount>>();
container.Register<IUserAccountRepository<HierarchicalUserAccount>>(() => new RavenUserAccountRepository("RavenDb"), Lifestyle.Singleton);
container.Register<IUserAccountQuery>(() => new RavenUserAccountRepository("RavenDb"));

On row

container.Register<UserAccountService<HierarchicalUserAccount>>();

I have an error For the container to be able to create UserAccountService, it should contain exactly one public constructor, but it has 2. Parameter name: TConcrete

Thanks for your help.


Solution

  • Simple Injector forces you to let your components to have one single public constructor, because having multiple injection constructors is an anti-pattern.

    In case the UserAccountService is part of your code base, you should remove the constructor that should not be used for auto-wiring.

    In case the UserAccountService is part of a reusable library, you should prevent using your container's auto-wiring capabilities in that case as described here. In that case you should fallback to wiring the type yourself and let your code call into the proper constructor, for instance:

    container.Register<UserAccountService<HierarchicalUserAccount>>(() =>
        new UserAccountService<HierarchicalUserAccount>(
            container.GetInstance<MembershipRebootConfiguration<HierarchicalUserAccount>>(),
            container.GetInstance<IUserAccountRepository<HierarchicalUserAccount>>()));