In the examples I saw in the Unity documentation, you specify the lifecycle manager by inserting new LifetimeManager() inline. So I have this code:
container.RegisterType<ApplicationDbContext>(new PerRequestLifetimeManager()); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new PerRequestLifetimeManager(), new InjectionConstructor(typeof (ApplicationDbContext))); container.RegisterType<UserManager<ApplicationUser>>(new PerRequestLifetimeManager());
Good, but I wonder why I create so many instances. Is there a reason why I shouldn't write it like this?
var lifetimeManager = new PerRequestLifetimeManager(); container.RegisterType<ApplicationDbContext>(lifetimeManager); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(lifetimeManager, new InjectionConstructor(typeof (ApplicationDbContext))); container.RegisterType<UserManager<ApplicationUser>>(lifetimeManager);
It seems obvious, but reading through PDF all the examples in the previous style without comment, so I wonder if I don't understand something about how this works.
Casey source share