微软企业库的 注入和依赖&nbs…

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.

C#
[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.

C#
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.

C#
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.

C#
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.)

C#
  
    "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.

C#
public class CustomersListViewPresenter
{
  private CustomersController _controller;

  [InjectionConstructor]
  public CustomersListViewPresenter
    (
      [ServiceDependency] CustomersController controller
    )
  {
    _controller = controller;
  }
  ...
}
答)机器人运动学是研究机器人的运动特性和运动方式的学科。它主要关注机器人在空间中的位置、速度和加速度等因素,以及机器人的关节角度和关节速度等运动参数。机器人运动学的研究对象就是机器人本身。 机器人是一种能够自主执行任务的自动化设备,它们可以通过各种机械装置和传感器来感知环境,并通过计算和控制系统来对环境作出响应。机器人具有多个自由度,可以进行各种运动,如平移、旋转、弯曲等,机器人运动学的研究正是探索和分析这些运动特性。 机器人运动学的研究对象主要包括机器人的结构和构造、机器人的自由度、机器人运动的规律与方式等。具体而言,它研究机器人的关节角度、关节速度,以及如何通过控制这些参数来实现所需的运动路径和动作。例如,通过研究机器人的运动学可以确定机器人的轨迹和姿态,进而优化机器人的运动控制算法和路径规划。 机器人运动学的研究对于机器人的设计和控制具有重要意义。它可以帮助人们更好地理解和解释机器人的运动特性,为机器人的操作和任务执行提供理论指导。同时,机器人运动学的研究还为机器人的仿真和虚拟现实技术提供了基础,有助于提高机器人在各个领域的应用效果和可靠性。 总之,机器人运动学是研究机器人运动特性和方式的学科,它研究的对象就是机器人本身。通过机器人运动学的研究,可以深入理解机器人的运动规律,提高机器人的运动控制能力,为机器人的设计和应用提供重要支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值