微软企业库的 注入和依赖&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 ...
随机推荐
- 基于红帽5裁剪一个简单的Linux
HOST:宿主机 Target:目标机 1.基于HOST制作一个简单的可启动的Linux 1.给目标磁盘分区 两个: 在宿主机上:/dev/sdb1,/dev/sdb2 /dev/sdb1挂载到 /m ...
- c# combobox 绑定枚举方式
建立一个类 : using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- python 基础 4.5 用函数实现九九乘法表
#/usr/bin/python #coding=utf-8 #@Time :2017/11/3 10:44 #@Auther :liuzhenchuan #@File :用函数实现九九乘法口 ...
- 用apache做为代理下载本地pdf文件
有一些公司会用apache做为代理,下载服务器上的pdf文件.以下是apache做为代理的配置 一. 环境 centos6.5 192.168.69.3 二. yum安装apache 服务 [zxj ...
- Netty聊天室-源码
目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...
- Wix Burn:如何将32位和64位的安装包制作成一个安装包
由于Windows Installer不是平台独立的(即区分32-bit和64-bit),因此用Wix制作的安装包在编译不能像.net应用那样采用Any CPU编译,而必须制定是目标Platform是 ...
- Flask的Debug功能非常酷
Flask是一个Python开发框架.在试用的过程中发现它的debug功能非常cool.如下图所示,在出错的页面每条栈新的右边都有一个按钮,点击之后我们可以执行Python代码,而且非常重要的一点是通 ...
- 利用wxpython显示OpenCV图像
核心代码 import wx, cv2 import numpy as np # Start with a numpy array style image I'll call "source ...
- Git如何强制拉取一个远程分支到本地分支(转载)
有时候,我们在使用git pull指令想把一个远程分支拉取到本地分支的时候,老是会拉取失败,这一般是因为某种原因,本地分支和远程分支的内容差异无法被git成功识别出来,所以git pull指令什么都不 ...
- ceres求解BA第10章
1.前言g2o是根据边来保存每一个代价函数,它是在边类中构造误差函数,构造边的时候,会设置顶点.测量值.协方差矩阵等.而在ceres中,用problem类型来构造最终的目标函数.先是使用AddResi ...