您好im使用温莎作为一个DI容器,
Hi there Im using windsor as a DI container,
我的code低于
public static class ContainerBuilder { public static IWindsorContainer Build() { var container = new WindsorContainer("Configuration\\Windsor.config"); // automatically register controllers container.Register(AllTypes .Of<Controller>() .FromAssembly(Assembly.GetExecutingAssembly()) .Configure(c => c.LifeStyle.Transient.Named(c.Implementation.Name.ToLower()))); container.Register( Component.For<IServiceLocator>().Instance(new WindsorServiceLocator(container)), Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)).LifeStyle.Transient ); return container; } }我需要从一个测试项目的话,这个问题是,当我这样做的windsor.config从未发现和试验似乎总是失败,这里是把这个配置文件的最好方法还是有更好的方法来这样做呢?谢谢
I need to call this from a test project , the problem is that when I do this the windsor.config is never found and the test seems to always fail, where is the best way to place this config file or is there a better approach to doing this? Thanks
推荐答案只要在配置路径配置,例如
Just make the config path configurable, e.g.
public static IWindsorContainer Build(string configPath) { var container = new WindsorContainer(configPath); ... }在您的应用程序的configPath来配置\\ Windsor.config,而在你的测试,你就会有一个像路径.... \\配置\\ Windsor.config。
In your app the configPath is "Configuration\Windsor.config" while in your tests you'll have a path like "....\Configuration\Windsor.config".
请注意,你不应该通常取决于你的测试容器上,除非你正在运行的一些集成测试。
Note that you shouldn't generally depend on the container in your tests, unless you're running some integration tests.
另外一个静态的集装箱制造商似乎并不像一个好主意,看看温莎安装的模块化的方式来注册你的组件。
Also a static container builder doesn't seem like a good idea, take a look at Windsor Installers to register your components in a modular way.
更多推荐
asp.net mvc的测试项目无法找到文件温莎
发布评论