参考VS依赖 [英] Reference vs Dependency

查看:160
本文介绍了参考VS依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的,我现在有以下结构:

Following on from this question, I now have the following structure:

Wolfie.Core - 包含业务逻辑和放大器;实体,还包含使用构造器注入储存库接口需要访问存储库(如IUserRepository)类。

Wolfie.Core - Contains business logic & entities, also contains repository interfaces (eg IUserRepository) Classes that need to access the repository are using constructor injection.

Wolfie.Data - 参考Wolfie.Core并具有实施IUserRepository一个UserRepository

Wolfie.Data - References Wolfie.Core and has a UserRepository implementing IUserRepository

所以我很满意这个至今。核心并不知道有关数据实施任何事情,因此不依赖于任何东西。

So I'm happy with this so far. Core doesn't know anything about data implementation and therefore isn't dependent on anything.

绊脚石我可以在我的Web层。

The stumbling block I get to is in my Web layer.

我的Web项目引用我的核心项目。那么我就可以了新的核心类,说用户,但我有一个具体的实施IUserRepository的传递到它。所以,我需要引用我的数据项目在我的Web项目,这似乎是错误的。现在看起来也像网络依赖于数据,它不应该。

My Web project references my Core project. I can then new up a Core class, say User, but I have to pass a concrete implementation of IUserRepository into it. So I need to reference my Data project in my Web project, which seems wrong. It now also looks like Web is dependent upon Data, which it shouldn't be.

所以,我怎么能注入与数据类我的核心用户类别withouth的直接引用数据?

So, how can I inject my Core User class with the Data class withouth directly referencing Data?

期待您的帮助。

推荐答案

您可以绑定到主应用程序code接口,只引用了合同组件(核心部件)。然后使用反射在运行时加载的依赖关系,并注入了具体的类。

You can bind to interfaces in your main application code, referencing only the contract assembly (the "Core" assembly). Then use reflections to load your dependencies at runtime and inject the concrete classes.

有需要解决的几个问题:

There are a few problems that need to be solved:


  • 您如何找到需要的组件?

  • 您应该使用什么具体的类,一旦你发现你的组件?

  • 您将如何松散配置您的应用程序使用这些组件和那些具体的类?你不想简单地改变你的硬件codeD集引用和具体类的引用是硬codeD组装和类名字符串...

这些问题可以在许多不同的方式来解决,并且是不同的依赖注入库之间的主要区别因素之一。

These problem can be solved in many different ways, and are one of the main differentiating factors between different Dependency Injection libraries.

有些使用的顶级配置code或配置code模块(例如 NInject )一些使用XML配置(例如团结),有的采用的自动发现机制的基础属性/公约(例如 MAF /的 MEF ),以及一些使用所有这些的组合(例如的 Castle.Windsor )。

Some use top-level configuration code or configuration code modules (for example NInject), some use XML configuration (for example Unity), some use an automatic discovery mechanism based attributes/conventions (for example MAF/MEF), and some use a combination of all of these (for example Castle.Windsor).

大部分或所有这些库支持的每个机制,但我已经提到的最突出的焦点。

Most or all those libraries support each mechanism, but I've mentioned the most prominent focus.

这篇关于参考VS依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆