微软企业库的 注入和依赖&nbs…
Working with ObjectBuilder
been rated - Rate
this topic
| Retired Content |
|---|
|
This content is outdated and is no longer being maintained. It is |
This topic discusses the following tasks you can perform with
ObjectBuilder:
- Creating new objects
- Locating a service
- Registering a service
- Registering a constructor
Creating New Objects
Frequently, an object in your application requires an instance of
another object. For example, consider the NewTransferView in
the reference implementation. The reference implementation uses the
MVP pattern to decouple business logic from the user interface
code. This means that the NewTransferView requires
a reference to an instance of a presenter object (in this case, an
instance of the NewTransferViewPresenter class).
The NewTransferView class
requires an instance of the NewTransferViewPresenter class
but does not contain code to instantiate an instance. Instead,
the NewTransferView class
uses the CreateNew attribute.
[CreateNew]
public NewTransferViewPresenter Presenter
{
get { return _presenter; }
set
{
_presenter = value;
_presenter.View = this;
}
}
The CreateNew attribute
instructs ObjectBuilder to instantiate and initialize an instance
of a NewTransferViewPresenter when
the NewTransferView is
created. When the property is set, the View property
of the presenter is used to connect this implementation of
the INewTransferView interface
to the presenter (the View property
is defined in the Presenter base
class).
Locating a Service
You can add the ServiceDependency attribute
to a property in your class to declaratively obtain a reference to
a service. The property specifies the type of service or interface
you require, as shown in the following code. When this attribute is
present, ObjectBuilder locates an instance of the service and
passes back a reference to it. To locate the service, ObjectBuilder
first looks in the current CompositionContainer object.
If the service is not found, ObjectBuilder then looks at the
services in the parent CompositionContainer object.
If the service is not found, ObjectBuilder throws an exception.
private IAccountServices _accountServices; [ServiceDependency]
public IAccountServices AccountServices
{
set { _accountServices = value; }
}
Frequently, the ServiceDependency attribute
is used for the arguments in a constructor. This means that
ObjectBuilder will instantiate the required services when it
creates the dependent object.
public class ElectronicFundsTransferController
{
private IAccountServices _accountServices; public ElectronicFundsTransferController
(
[ServiceDependency] IAccountServices accountServices
)
{
_accountServices = accountServices;
}
...
}
Registering a Service
You can programmatically register a service or register a service
through configuration. To programmatically register a service, call
the Add method
or AddNew method
of the Servicescollection
of the CompositionContainer within
which you want to use the service. This can be the root CompositionContainer or
a module CompositionContainer.
To use an existing service instance you have already created, use
the Add method.
To register a service through configuration, add a service configuration
element to a Web.config file. In the following XML, the OrdersRepository.Services.CustomerService service
is registered as a global service. (To register a service as a
module service, change the scope attribute to Module.)
"Customers" assemblyName="Customers" virtualPath="~/Customers">
. . .
"OrdersRepository.Interfaces.Services.ICustomerService, OrdersRepository.Interfaces" type="OrdersRepository.Services.CustomerService, OrdersRepository.Services" scope="Global" />
. . .
Registering a Constructor
A class can contain more than one constructor. ObjectBuilder first
looks for any constructor decorated with the [InjectionConstructor] attribute
(there can be only one of these) and uses this constructor if
found. If there is no decorated constructor, yet there is only one
constructor, it will use that constructor.
public class CustomersListViewPresenter
{
private CustomersController _controller; [InjectionConstructor]
public CustomersListViewPresenter
(
[ServiceDependency] CustomersController controller
)
{
_controller = controller;
}
...
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
微软企业库的 注入和依赖&nbs…的更多相关文章
- 微软企业库Unity依赖注入
Unity Application Block 1.0系列(4): 方法调用注入(Method Call Injection ) http://www.cnblogs.com/inrie/archiv ...
- [EntLib]微软企业库5.0 学习之路——第一步、基本入门
话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...
- 微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证
前端时间花了1个多星期的时间写了使用jQuery.Validate进行客户端验证,但是那仅仅是客户端的验证,在开发项目的过程中,客户端的信息永远是不可信的,所以我们还需要在服务器端进行服务器端的验证已 ...
- 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)
首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...
- 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持
在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...
- 微软企业库5.0学习-Security.Cryptography模块
一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...
- 使用微软企业库5.0提供的unity配置解藕系统demo(源码)
最近公司集50多号开发人员的人力围绕一个系统做开发,框架是免不了要统一的,公司提供的架构,利于分工合作,便于维护,扩展,升级,其中使用了到微软的企业库来解藕系统,只是因为框架封装,于是在网上学习了一个 ...
- 微软企业库Microsoft Enterprise Library的相关文章链接
微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...
- 微软企业库的Cache
微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSamp ...
随机推荐
- Yii的权限管理rbac
1.首先我们要在配置文件的组件(component)里面配置一下 Rbac 在对应项目下的config/main.php或者config/main-local.php下添加 'authManager' ...
- CGI的基本原理
一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Webserver主机提供信息服务的标准接口.通过CGI接口,Webserver就行获取client提交的信 ...
- gridcontrol 之标题 GroupPanel设置 (标题设置,屏蔽右键)
GroupPanel设置 例如gridcontrol显示标题:“gridcontrol小例子” gridView1.GroupPanelText="gridcontrol小例子"; ...
- iOS 转场动画核心内容
CATransition——转场动画 CATransition是CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点. ...
- 我的Android进阶之旅------>解决Your project contains error(s),please fix them
在使用eclipse写好Android的代码,代码没有报错.然后想在AVD中运行测试时,弹出错误框,提示信息为: "Your project contains error(s),pleas ...
- 项目发布之后 总提示有一个.DLL找不到或不匹配
最近发布项目(.net,winform)总提示有一个.dll文件找不到或者不匹配 但是在本地调试是正常的 这个.dll,原来是从.net组件中引用到项目的,后来我将此.dll文件从网上下载,然后在项目 ...
- CSS3实现水位充满文字特效
CSS3实现水位充满文字特效是一款既是Loading特效也是文字特效,Loading动画开始时,文字中的水位渐渐上升,为了模拟水位上升的真实效果,水面还会波浪浮动,当Loading动画结束时,文字中的 ...
- 存储过程之rowtype 使用
CREATE OR REPLACE PROCEDURE "DYLYLQX_SC_BA_1" (YWID IN VARCHAR2, FLAG OUT VARCHAR2) IS V_R ...
- ZOJ 3805 Machine(二叉树,递归)
题意:一颗二叉树,求 “ 宽度 ” 思路:递归,貌似这个思路是对的,先记下,但是提交时超时, 1.如果当前节点只有左孩子,那么当前宽度等于左孩子宽度 2.如果当前节点只有右孩子,那么当前宽度等于 ...
- hdu-5656 CA Loves GCD(dp+数论)
题目链接: CA Loves GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...