views:

66

answers:

3

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
+1  A: 

Edit: Even shorter.

Put this in your bootstrapper code:

ForRequestedType<IAllowanceManager>().TheDefault.Is
       .ConstructedBy(() => new Allowancemanager(new StateManager(), new TranslationManager()));
Jan Jongboom
arg1 and arg2 here being instances of ITranslationManager and ISessionHelper respectively?
Waliaula Makokha
yeah, check my edit
Jan Jongboom
Please explain how StateManager and TranslationManager get constructed since I cannot see you passing "culture" and "key" arguments required.
Waliaula Makokha
+1  A: 

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.

PHeiberg
This types are being registered in a registry class which is not in a scope to retrieve the key and culutre arguments. These arguments are available when the instances are being retrieved.
Waliaula Makokha
@Waliaula where are the session key and culture coming from? What object has access to those values?
PHeiberg
The are coming from a querystring access on a page. Now why your solution won't work is that the mapping is done in a registry class that is then added to the Objectfactory in the global.asax file. This means that I dont have the access to the "key" and "culture" mapping during setup but I have them when actually retrieveing the instances at runtime. Check my anwer below.
Waliaula Makokha
@Waliaula - replace c => Thread.CurrentThread.CurrentCulture.Name with HttpContext.Current.Request["culture"] in my example and you should be good to go. Although I wouldn't recommend that approach in this case. Better IMHO would be to encapsulate the call to the request in some provider class and inject that into the TranslationManager.
PHeiberg
A: 

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>();
Waliaula Makokha