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. Android-解决Fail to post notification on channel "null"的方法

    原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...

  2. python 基础 9.2 mysql 事务

    一. mysql 事务    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如,你操作一个数据库,公司的一个员工离职了,你要在数据库中删除它的资料,也要删除该人员相关的,比如邮箱,个人资产等 ...

  3. Awesome Vue.js vue.js学习资源链接大全 中文

    https://blog.csdn.net/caijunfen/article/details/78216868

  4. .NET Winform 将引用的dll文件集成到exe中(转)

    Winform程序经常需要引用一些第三方dll文件,这些dll在发布后与exe文件保存在同一目录下,虽然将dll文件集成到exe中会增大文件尺寸,但程序目录会相对整洁. 下面介绍一种比较简单的集成方法 ...

  5. Django 初始化数据库

    django 初始化数据库 刷新数据库guoguos-MacBook-Pro:mysite guoguo$ python manage.py sqlflushBEGIN;SET FOREIGN_KEY ...

  6. iOS Dev (53) 修复UIImagePickerController偷换StatusBar颜色的问题

    版权声明:本文为 CSDN 博主 大锐哥(ID 为 prevention)原创文章,未经博主同意不得转载. https://blog.csdn.net/prevention/article/detai ...

  7. 【linux】crontab的环境变量问题

    今天遇到一个奇怪的问题,同样一个脚本,手动执行没问题,加入到crontab中,就出现无法运行的情况,第一反应是环境变量问题 环境说明: 操作系统:centos 用户:test用户通过sudo su切换 ...

  8. /etc/init.d/nginx

    #! /bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon ...

  9. 山东省第四届ACM程序设计竞赛A题:Rescue The Princess(数学+计算几何)

    Rescue The Princess Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 168[Submit][Status][ ...

  10. 在PyCharm上创建Django项目

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 首先我们在Pycharm上新建一个Dja ...