Mar*_*ulz 3 asp.net-mvc dependency-injection castle-windsor asp.net-mvc-3
我想使用Castle Windsor作为我的解决方案的依赖注入,包括以下项目:
在业务层中,有一个名为PostServiceimplementation 的类IPostService来管理博客帖子.在PostsController对中的mvc项目依赖IPostService.但是,PostService(相应的具体实现)本身依赖于IPostRepository.
我在哪里配置Castle Windsor以返回PostRepository要解决的实例IPostRepository?该的mvc项目不知道的数据访问项目.因此,我无法在global.asax或Mvc中的其他位置配置组件绑定.
[更新]依赖关系图
现在我找到了解决方案(再次感谢Darin Dimitrov!)我想与您分享当前的依赖关系图.

在类库中创建和放置Windsor安装程序类(IWindsorInstaller接口实现),例如:
public class PostRepositoryInstaller: IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
var allTypesFromBinDir = AllTypes
.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory));
container.Register(allTypesFromBinDir
.BasedOn<IPostRepository>()
.WithService.FromInterface()
.Configure(c => c.LifeStyle.Transient));
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的Global.asax中安装依赖项:
protected virtual void Application_Start()
{
new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4036 次 |
| 最近记录: |