Reputation: 815
I have the following classes:
public class AllowanceManager : IAllowanceManager
{
public AllowanceManager(ITranslationManager t_Manager, ISessionManager s_Manager)
{...}
}
public class TranslationManager : ITranslationManager
{
public TranslationManager(string culture)
{...}
}
public class SessionManager : ISessionManager
{
public SessionManager(string key)
{...}
}
How can I initialize this classes up in ObjectFactory so that getting an instance of IAllowanceManager also autowires and initializes (with the constructor arguments) StateManager and TranslationManager. So that I only need to retrieve the instance of IAllowanceDeduction like so:
IAllowanceManager a_Manager = ObjectFactory....// Gets Allowancemanager configured with initialized instances of IStateManager and ITranslationManager
Upvotes: 3
Views: 375
Reputation: 815
I have come up with the following solution:
IStateManager stateManager = ObjectFactory
.With<string>("key")
.GetInstance<IStateManager>();
ITranslationManager translationManager = ObjectFactory
.With<string>("culture")
.GetInstance<ITranslationManager>();
manager = ObjectFactory
.With<ITranslationManager>(translationManager)
.With<IStateManager>(stateManager)
.GetInstance<IAllowanceDeductionManager>();
Upvotes: 0
Reputation: 29861
Using 2.6.1 syntax it could be written:
For<ISessionManager>().Use<SessionManager>()
.Ctor<string>("key").Is(c => GetSessionKey());
For<ITranslationManager>().Use<TranslationManager>()
.Ctor<string>("culture").Is(c => Thread.CurrentThread.CurrentCulture.Name);
For<IAllowanceManager>.Use<AllowanceManager>();
where GetSessionKey returns your session key in a way similar to how the culture is resolved.
See this blog entry for a more in depth description of how to resolve contructor arguments.
Upvotes: 1
Reputation: 27352
Edit: Even shorter.
Put this in your bootstrapper code:
ForRequestedType<IAllowanceManager>().TheDefault.Is
.ConstructedBy(() => new Allowancemanager(new StateManager(), new TranslationManager()));
Upvotes: 1