Working with ObjectBuilder

This topic has not yet
been rated - Rate
this topic





Retired Content

This content is outdated and is no longer being maintained. It is
provided as a courtesy for individuals who are still using these
technologies.
This page may contain URLs that were valid when originally
published, but now link to sites or pages that no longer exist.



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.




moduleContainer.Services.AddNew();




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…的更多相关文章

  1. 微软企业库Unity依赖注入

    Unity Application Block 1.0系列(4): 方法调用注入(Method Call Injection ) http://www.cnblogs.com/inrie/archiv ...

  2. [EntLib]微软企业库5.0 学习之路——第一步、基本入门

    话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...

  3. 微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证

    前端时间花了1个多星期的时间写了使用jQuery.Validate进行客户端验证,但是那仅仅是客户端的验证,在开发项目的过程中,客户端的信息永远是不可信的,所以我们还需要在服务器端进行服务器端的验证已 ...

  4. 微软企业库5.0 学习之路——第四步、使用缓存提高网站的性能(EntLib Caching)

    首先先补习下企业库的Caching Application Block的相关知识: 1.四大缓存方式,在Caching Application Block中,主要提供以下四种保存缓存数据的途径,分别是 ...

  5. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  6. 微软企业库5.0学习-Security.Cryptography模块

    一.微软企业库加密应用模块提供了两种加密: 1.Hash providers :离散加密,即数据加密后无法解密 2.Symmetric Cryptography Providers:密钥(对称)加密法 ...

  7. 使用微软企业库5.0提供的unity配置解藕系统demo(源码)

    最近公司集50多号开发人员的人力围绕一个系统做开发,框架是免不了要统一的,公司提供的架构,利于分工合作,便于维护,扩展,升级,其中使用了到微软的企业库来解藕系统,只是因为框架封装,于是在网上学习了一个 ...

  8. 微软企业库Microsoft Enterprise Library的相关文章链接

    微软企业库4.1学习笔记 http://blog.csdn.net/anyqu/article/category/1228691/3 黄聪:Enterprise Library 5.0 系列教程 ww ...

  9. 微软企业库的Cache

    微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSamp ...

随机推荐

  1. windows 10 python 2.7和python3.6共存解决方法和pip安装

    一.首先去python官网将两个版本下载并安装: 然后进入windows的环境变量,检查下面4个变量: 1.C:\Python272.C:\Python27\Scripts3.D:\software\ ...

  2. Linux系统初始化流程

    POST-->BIOS(Boot Sequence)-->MBR(bootloader)-->Kernel(initrd)-->/sbin/init(/etc/inittab) ...

  3. c++ 通过数据流方式实现文件拷贝

    #include "stdafx.h"#include <iostream>#include<fstream>using namespace std;voi ...

  4. Linux 命令汇总总结相关

    玩了linux快一年,简单总结下网络相关的命令,具体每个命令的参数可以用到再细看. 1.ifconfig:查询.设置网卡和IP网段等相关参数,包括MTU.2.ifup.ifdown:这两个命令就是一个 ...

  5. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

    [BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...

  6. 实现RTSP网站微信直播方案EasyNVR(linux版)部署问题之:ERR_CONTENT_LENGTH_MISMATCH

    发现问题: 想要优化一下EasyNVR相关功能,内部测试软件,于是在linux系统中部署了一台EasyNVR.当部署好,运行起来发现问题: EasyNVR的配置页面数据出不来. 分析问题: 基于是we ...

  7. Tomcat Server 配置

    Tomcat报错: The JRE could not be found. Edit the server and change the JRE location. EClipse -> win ...

  8. 【题解】[SCOI2010]股票交易

    十分普通的DP+不平凡的转移 传送门 这道题状态十分明显.转移是\(O(n^4)\)的,过不去,我们需要优化. 一个十分显然的DP是\(f(i,j)\)表示第\(i\)天时候拥有\(j\)单位股票的最 ...

  9. ubuntu 网络配置及ssh文件传输

    一.ubuntu网路配置 参考http://www.cnblogs.com/rusty/archive/2011/04/06/2007139.html /etc/network/interfaces ...

  10. LightOJ - 1274 Beating the Dataset —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1274 1274 - Beating the Dataset    PDF (English) Statistics ...