2012-03-02 62 views
1

我正在使用UnityContainer向ctor注册需要运行时参数的类型unityContainer

我想在其中注册AuthServiceTokenManager

AuthService将TokenManager作为私人记忆。

TokenManager有一个构造函数:TokenManager(string name, string pass)

我从UI这些字符串在运行时。

Unity要求在配置文件中注册ctor参数。

我曾经想过

  • 创建一个构造函数:AuthService(TokenManager mgr)

但后来我怎么在配置文件中添加一个对象(非原始)到<unity>节点?

  • 我怎样才能仍然使用unity运行参数到TokenManager ctor?

回答

0

我的想法:

AuthService将有两个构造函数 - 参和AuthService(bool initFromContainer)

在测试工具,我会打电话给新AuthService(假的),然后使用重写TokenManager

AuthService_Accessor

0

TokenManager定义工厂并将工厂注入AuthService。使工厂接受用户名和密码的参数。

只要您需要incorporate runtime parameters,注射工厂就能很好地工作。但不要被这种模式带走,否则你可能遇到factory overload

相关问题