我正在使用StructureMap来满足我的IoC需求.
为了使事情愉快地测试,我IContainer尽可能地传递实例,通常作为构造函数参数.为方便起见,我希望能够回归使用ObjectFactory无参数构造函数.
要做到这一点最简单的方法(我认为)是简单地得到IContainer的ObjectFactory类包装,并传递到其他构造.不幸的是,我找不到这个实例被公开曝光的任何地方.
问题是:
有没有办法获得IContainer内部ObjectFactory所以我可以像处理用户提供的实例一样处理它?
或者,有没有办法将配置复制ObjectFactory到新Container实例?
例:
我希望能够做到以下几点:
public class MyClass
{
public MyClass()
{
Container = ... // The ObjectFactory container instance.
}
public MyClass(IContainer container)
{
Container = container;
}
public IContainer Container { get; private set; }
}
Run Code Online (Sandbox Code Playgroud) 我正在使用structuremap作为IOC容器进行MVC项目.我们正在进行TDD,我想设置我的依赖项,以便它易于使用,并且易于测试.
我应该如何最好地为下面虚构的插图图形设置依赖图?
您是否在控制器上注入了userrepository,并且进一步从身份验证服务中注入?如果图表更深入怎么办?你不会从控制器开始获得很多依赖吗?
如果您依赖于您的applicationcontroller,那么您是否也将它注入到控制器上,然后在基础上注入?
如果我让容器解析图中间某处的实例,我将不得不设置容器进行测试?这是一件好事还是最好避免的?
还有另一种方式,我没有看到?
.net structuremap dependency-injection ioc-container inversion-of-control
所以我想要注册特定查找类型的n个映射.IE:
x.For<IWidget>().Add<SquareWidget>();
x.For<IWidget>().Add<YellowWidget>();
Run Code Online (Sandbox Code Playgroud)
我想让SM将一个枚举(或数组)注入到类的构造函数中:
public class Machine
{
public Machine(IEnumerable<IWidget> widgets) { ... }
}
ObjectFactory.GetInstance<Factory>()
Run Code Online (Sandbox Code Playgroud)
我没有办法做到这一点,但我可能会遗漏一些东西.
TIA,
米
PS:请不要回答"你为什么这样做"或其他不相关的评论.我意识到这可以通过其他方法来实现.如果这种特殊方法可行,我真的很好奇.:)
我在ASP.NET MVC网站上有这个代码:
x.For<AccountController>().TheDefault.Is.ConstructedBy(() => new AccountController());
Run Code Online (Sandbox Code Playgroud)
此代码抛出一个警告,似乎非常自我解释,但由于某些原因,当我使用"使用"方法时它似乎不起作用.我知道我做错了什么,并希望得到一些帮助.
警告是:
警告1'StructureMap.Configuration.DSL.Expressions.CreatePluginFamilyExpression.TheDefault'已过时:'"首选Use()方法"
谢谢.
我怎么能转换这个:
For<ISession>().Use(ctx => {
var uow = (INHibernateUnitOfWork)ctx.GetInstance<IUnitOfWork>();
return uow.Session;
});
Run Code Online (Sandbox Code Playgroud)
对Ninject?
我能够转换这个:
For<ISessionSource>().Singleton().Use<NHibernateSessionSource>();
Run Code Online (Sandbox Code Playgroud)
至
Bind<ISessionSource>()
.To<NHibernateSessionSource>()
.InSingletonScope()
Run Code Online (Sandbox Code Playgroud)
我想我也可以转换这个:
For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<NHibernateUnitOfWork>();
Run Code Online (Sandbox Code Playgroud)
至
Bind<IUnitOfWork>().To<NHibernateUnitOfWork>().InThreadScope();
Run Code Online (Sandbox Code Playgroud)
我知道这些并非都是一对一的,但我可以得到一些帮助吗?
我有一个带有一些古迹的构造函数
public class AppEngine:IAppEngine
{
private IGreeter _greeter;
private string _str;
public AppEngine(IGreeter greeter,string str)
{
_greeter = greeter;
_str = str;
}
public string Run()
{
return _greeter.Greeting()+":"+_str;
}
}
Run Code Online (Sandbox Code Playgroud)
这里从工厂我想得到实例
var obj = ObjectFactory.GetInstance<IAppEngine>();
Run Code Online (Sandbox Code Playgroud)
在这里,我想传递构造函数正在接受的Arguments.我怎么能这样做
问候
我想配置structuremap以使用工厂类创建服务.工厂本身具有需要填充的依赖性.目前我的Registry类中有以下内容:
For<IDoStuffWebService>().Singleton().Use(() =>
new DoStuffWebServiceClientFactory(new ConfigProvider()).Create()
);
Run Code Online (Sandbox Code Playgroud)
而不是必须硬编码具体类型DoStuffWebServiceClientFactory并手动填充它的依赖项,我希望structuremap为我得到它(它实现了IDoStuffWebServiceClientFactory).看起来IContext可能有所帮助(http://docs.structuremap.net/UsingSessionContext.htm),但我很难弄清楚这是如何适合的.
任何帮助非常感谢.罗杰.
在Global.asax的Application_Start中,我有以下内容
ObjectFactory.Initialize(cfg => {
cfg.For<IDependencyResolver>().Singleton().Add<StructureMapDependencyResolver> ();
});
Run Code Online (Sandbox Code Playgroud)
我的Hub接口是
public interface IDashboardHub
{
void Initialize();
}
Run Code Online (Sandbox Code Playgroud)
我的中心如下:
public class DashboardHub : Hub, IDashboardHub
{
private readonly ICpeAccountService _accountService;
public DashboardHub(ICpeAccountService service)
{
_accountService = service;
}
[Authorize]
public void Initialize()
{
Clients.All.UpdateStatus("Hello World!!!");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我删除注入的构造函数和解析器,那么我得到"Hello World"信号,JavaScript显示值.如果我只是删除解析器,那么signalR不再找到无参数构造函数,并且不会调用Initialize方法.
如果我包含StructureMap依赖项解析器(它正在工作并立即注入大约40个其他类),那么我得到以下异常消息
StructureMap configuration failures: Error: 104
Source: Registry: StructureMap.Configuration.DSL.Registry, StructureMap,
Version=2.6.4.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223
Type Instance '87da3c00-4deb-4334-b189-021d445d95ec'
(Configured Instance of App.DependencyResolution.StructureMapDependencyResolver,
App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null)
Cannot be plugged into type Microsoft.AspNet.SignalR.IDependencyResolver,
Microsoft.AspNet.SignalR.Core, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35 …Run Code Online (Sandbox Code Playgroud) 我在注册这样的依赖项时遇到了问题.
No default Instance is registered and cannot be automatically determined for type 'IUserStore<ApplicationIdentityUser, Int32>'
There is no configuration specified for IUserStore<ApplicationIdentityUser, Int32>
1.) new UserManager`2(*Default of IUserStore<ApplicationIdentityUser, Int32>*)
2.) UserManager<ApplicationIdentityUser, Int32>
3.) Instance of UserManager<ApplicationIdentityUser, Int32>
4.) new ApplicationUserManager(*Default of UserManager<ApplicationIdentityUser, Int32>*, *Default of IAuthenticationManager*)
5.) MyClinic.Infra.Data.Identity.ApplicationUserManager
6.) Instance of MyClinic.Core.Identity.IApplicationUserManager (MyClinic.Infra.Data.Identity.ApplicationUserManager)
7.) new AccountController(*Default of IApplicationUserManager*)
8.) MyClinic.Web.Controllers.AccountController
9.) Instance of MyClinic.Web.Controllers.AccountController
10.) Container.GetInstance(MyClinic.Web.Controllers.AccountController)
Run Code Online (Sandbox Code Playgroud)
有人能帮助我吗?
我的帐户控制器是:
public class AccountController : Controller
{
private IApplicationUserManager _userManager;
public AccountController(IApplicationUserManager userManager) …Run Code Online (Sandbox Code Playgroud) 我现在正试图弄清楚结构图,因为ObjectFactory静态函数已被标记为过时.
从长远来看,我必须在MVC和WebApi应用程序中使用它.以前使用时,在global.asax中放置一行到静态方法,以使用ObjectFactory初始化所有内容.
ObjectFactory.Initialize{
container.For .....
}
Run Code Online (Sandbox Code Playgroud)
试图将其转换为新的IContainer方法我已经提出以下内容但是我想知道我是否实际上无意中在我的方法中实现了这个经常提到的反模式.
返回容器的静态方法:
public class StructureMapConfig
{
public static IContainer GetContainer()
{
return new Container(container =>
{
container.For<IUserService>().Use<UserService>();
container.For<IStringService>().Use<StringService>();
container.For<IUserRepository>().Use<UserRepository>();
});
}
}
Run Code Online (Sandbox Code Playgroud)
Userservice的contstructor看起来像这样:
public class UserService : IUserService
{
private readonly IUserRepository _userRepository;
private readonly IStringService _stringService;
public UserService(IUserRepository userRepository, IStringService stringService)
{
_userRepository = userRepository;
_stringService = stringService;
}
Run Code Online (Sandbox Code Playgroud)
最后,初始化(在控制台应用程序中的这个实例)看起来像这样:
private static IUserService _userService;
private static IContainer _container;
static void Main(string[] args)
{
_container = StructureMapConfig.GetContainer();
_userService = _container.GetInstance<IUserService>();
}
Run Code Online (Sandbox Code Playgroud)
所以对我的问题.