Lab 2: Creating a Service

本文介绍如何使用WebClient软件工厂创建并注册一个电子资金转账服务,包括实现依赖注入、单元测试等步骤。

Purpose

Estimated time to complete this lab: 25 minutes

A Composite Web Application Block service is a supporting class that provides functionality to other components (the components can be in the same module or different modules) in a loosely coupled way, such as services that provide authorization, logging, or hardware communication functionality. The software factory includes a set of basic services that you can use in your applications. You can also build your own services that provide infrastructure capabilities specific to your applications.

In this exercise, you will learn how to create and register a service to be consumable by the module, and that will be automatically injected using ObjectBuilder. In this case, you will be implementing a simple electronic funds transfer (EFT) processing service.

Preparation

Before proceeding with this lab, you must install and configure the prerequisite software. For more information, see Web Client Software Factory Hands-On Labs.

Open the solution for the previous lab (either the one that you created or the end solution for that lab.) If you use the provided end solution, you must enable the guidance package for that solution.

To enable the Web Client Development guidance package with the Guidance Package Manager

1.       Using Visual Studio, open the solution.

2.       On the Tools menu, click Guidance Package Manager.

3.       In the Guidance Package Manager dialog box, click Enable / Disable Packages.

4.       In the Enable and Disable Packages dialog box, select the Web Client Development check box.

5.       Click OK.

Procedures

This lab includes the following tasks:

·         Task 1: Create Application Classes Used by the Service

·         Task 2: Create the Service Interface

·         Task 3: Add a Service Implementation to Your Solution

·         Task 4: Get Familiar with the Dependency Injection Pattern

·         Task 5: Register the Service

·         Task 6: Consume the Service

The next sections describe these tasks.

Task 1: Create Application Classes Used by the Service

In this task, you will create application-specific classes used by the sample service.

To create application-specific classes

1.       In Solution Explorer, create a folder named BusinessEntities inside the EFT project.

2.       Add a new code file named Transfer.cs to the BusinessEntities folder. To do this, right-click the BusinessEntities folder in Solution Explorer, point to Add, and then click Class.

3.       Replace the contents of the Transfer.cs file with the following code.

using System;

 

namespace GlobalBank.EFT.BusinessEntities

{

    public class Transfer

    {

        private string _sourceAccount;

        private string _targetAccount;

        private decimal _amount;

        private string _status;

        private Guid _id;

 

        public Transfer()

        {

        }

 

        public Transfer(string sourceAccount, string targetAccount, decimal amount, string status)

        {

            _sourceAccount = sourceAccount;

            _targetAccount = targetAccount;

            _amount = amount;

            _status = status;

        }

 

        public Guid Id

        {

            get { return _id; }

            set { _id = value; }

        }

 

        public string Status

        {

            get { return _status; }

            set { _status = value; }

        }

 

        public decimal Amount

        {

            get { return _amount; }

            set { _amount = value; }

        }

 

        public string TargetAccount

        {

            get { return _targetAccount; }

            set { _targetAccount = value; }

        }

 

        public string SourceAccount

        {

            get { return _sourceAccount; }

            set { _sourceAccount = value; }

        }

    }

}

Task 2: Create the Service Interface

In this task, you will define the interface for a service that performs an electronic funds transfer between two bank accounts.

To create the service interface

1.       Create a folder named Interface in the EFT project. To do this, right-click the EFT project, point to Add, and then click New Folder.

2.       Add a new interface named ITransferService.cs to the Interface folder. To do this, right-click the Interface folder, point to Add, and then click New Item. In the Add New Item dialog box, select the Interface template, enter ITransferService.cs as the name, and then click Add.

3.       Copy and paste the following code to the file (this will be the service interface).

using GlobalBank.EFT.BusinessEntities;

using System;

 

namespace GlobalBank.EFT.Interface

{

    public interface ITransferService

    {

        Transfer[] ProcessTransfers(Transfer[] transfers);

        DateTime GetLastTransferDate();

    }

}

Task 3: Add a Service Implementation to Your Solution

In this task, you will create an implementation of the electronic funds transfer (ETF) service.

To create an ETF service implementation

1.       Add a new class named TransferService to the Services folder of the EFT project.

2.       Add the following using statements to the top of the file.

using GlobalBank.EFT.Interface;

using GlobalBank.EFT.BusinessEntities;

3.       Replace the class definition with the following code. This will be the concrete implementation of the service.

public class TransferService : ITransferService

{

    private static DateTime _lastTransferDate = DateTime.MinValue;

 

    public Transfer[] ProcessTransfers(Transfer[] transfers)

    {

        foreach (Transfer transfer in transfers)

        {

           transfer.Status = "Completed";

        }

        _lastTransferDate = DateTime.Now;

        return transfers;

    }

 

    public DateTime GetLastTransferDate()

    {

        return _lastTransferDate;

    }

}

Your solution should be similar to the solution shown in Figure 1.

Figure 1
Solution Explorer view showing the EFT service

Task 4: Get Familiar with the Dependency Injection Pattern

The Dependency Injection pattern promotes loose coupling between a class and the objects it depends on. With the Dependency Injection pattern, you do not instantiate the dependencies explicitly in your class. Instead, a class gets the objects it requires from an outside source. With the Web Client Software Factory, you express dependencies declaratively in your class definition. In the declaration, you specify an interface; you do not specify a concrete implementation. This means you can easily replace the implementation of an object that a class depends on, without changing the source code of the class. This is especially useful if you want to be able to test your classes in isolation. The Composite Web Application Block includes a dependency injection framework based on ObjectBuilder that you can use to implement the Dependency Injection pattern in your application; this framework is used frequently throughout the software factory.

For more information about the Dependency Injection pattern, see “Dependency Injection” in Web Client Software Factory Help.

Task 5: Register the Service

In this task, you will register the EFT service that you implemented in earlier tasks. You must register the service before another object can use dependency injection to obtain a reference to the service. You can register services either as module services or as global services. Module services are services that can be consumed only by the module that registers it. Global services are services that are available to all the modules in the solution. In this task, you will register the electronic funds transfer service as a module service.

To register module services, you implement the AddModuleServices method of the module initialization class of your business module.

A module initialization class is a class that implements the IModuleInitializer interface. This class contains initialization code that runs when the Composite Web Application Block loads the module. This interface defines two methods, Load and Configure. You implement these methods to perform module initialization tasks, such as registering services or adding site map nodes to the site map. For more information about module loading and the IModuleInitializer interface, see “Modules” in Web Client Software Factory Help.

To register the service

1.       Open the EFT module initialization class (EFTModuleInitializer). This class is located in the EFTModuleInitializer.cs file located in the root of the EFT project.

2.       Add the following using statements to the top of the file.

using GlobalBank.EFT.Interface;

using GlobalBank.EFT.Services;

3.       Add the following code to the AddModuleServices method.

moduleServices.AddNew<TransferService, ITransferService>();

Note: moduleServices is a services collection. In this particular case, it is the collection of services of the EFT module. You use the AddNew method to have ObjectBuilder create a new instance of the service and add it to the services collection. By adding a service to the services collection of the EFT module, you register it as a module service, and it will always be available at run time to be injected by the ObjectBuilder in the current module.

The AddNew method allows you to specify the type that is used to register the service (this is the second generic parameter in the line of code you added in step 3). This is the type developers use to obtain a reference to the service when using the Dependency Injection pattern. By specifying the service’s interface as the registration type, you enable developers to write generic code that is not aware of the concrete service implementation being used. This is especially useful when testing your classes because you can easily replace dependencies with mock implementations to test your classes in isolation.

For more information about registering services, see “How to: Register and Use Services” in Web Client Software Factory Help.

Task 6: Consume the Service

In this task, you will use dependency injection to obtain a reference to the EFT service.

To consume the service

1.       Open the EFT module controller class file (EFTController.cs).

2.       Add the following using statements to the top of the file.

using Microsoft.Practices.ObjectBuilder;

using GlobalBank.EFT.Interface;

3.       Add the following member variable declaration.

private ITransferService _transferService;

4.       Add a second constructor that uses dependency injection to obtain a reference to an implementation of the ITransferService. Use the InjectionConstructor attribute to specify this as the constructor ObjectBuilder will use when it constructs the object.

[InjectionConstructor]

public EFTController

    ([ServiceDependency] ITransferService transferService)

{

    _transferService = transferService;

}

5.       Implement a method to retrieve the last transfer date. To do this, add the following virtual method to the controller class.

public virtual DateTime GetLastTransferDate()

{

    return _transferService.GetLastTransferDate();

}

Notice that there is no reference to the concrete implementation of ITransferService. This is because you used the overloaded version of the AddNew<TService, TRegisterAs>() method in Task 4 that registers the service by an interface type. ObjectBuilder will automatically inject the registered implementation for that interface type when ObjectBuilder instantiates the controller (notice the ServiceDependency attribute applied to the parameter in the constructor). This would allow changing the implementation of the service with another class and keeping the source code changes to a minimum (only the registration would change).

Verification

In this section, you will create a unit test to validate that the controller successfully consumes the electronic funds transfer service.

Verification involves performing the following tasks:

·         Task 1: Create a Mock Service

·         Task 2: Add a Unit Test

The next sections describe these tasks.

Task 1: Create a Mock Service

In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects. Mock objects are useful when a real object is difficult (or impossible) to incorporate into a unit test and when you want to test a component in isolation. In this task, you will create a mock EFT service so you can test the EFTController class in isolation.

To create a mock service

1.       Add a new class named MockTransferService to the Mocks folder of the EFT.Tests project.

2.       Add the following using statements to the top of the MockTransferService.cs file.

using GlobalBank.EFT.Interface;

using GlobalBank.EFT.BusinessEntities;

3.       Replace the class definition with the following code.

public class MockTransferService : ITransferService

{

    public DateTime LastTransferDate = new DateTime(2000, 1, 1);

    public DateTime GetLastTransferDate()

    {

        return LastTransferDate;

    }

    public Transfer[] ProcessTransfers(Transfer[] transfers)

    {

        return transfers;

    }

}

Task 2: Add a Unit Test

In this task, you will add a unit test to validate that the EFTController successfully consumes the EFT service.

To add a unit test

1.       Open the EFTModuleControllerFixture class file.

2.       Add the following using statements to the top of the file.

using Microsoft.Practices.ObjectBuilder;

using Microsoft.Practices.ObjectBuilder.WCSFExtensions;

using GlobalBank.EFT.Tests.Mocks;

using GlobalBank.EFT;

using GlobalBank.EFT.Interface;

3.       Add the following test method.

 [TestMethod]

public void GetLastTransferDateConsumesITransferService()

{

    MockTransferService service = new MockTransferService();

    EFTController controller = new EFTController(service);

 

    DateTime transferDate = controller.GetLastTransferDate();

 

    Assert.AreEqual<DateTime>(service.LastTransferDate, transferDate);

}

The test sets up an instance of the EFTController that uses the mock EFT service. It calls the GetLastTransferDate method of the controller, and then it asserts that the date returned is the last transfer date of the transfer service

4.       Use Test Manager to run the test. To do this, perform the following steps:

a. In Solution Explorer, click EFT.Tests.

b.      On the Test menu, click Start Selected Tests Project without Debugger (or press CTRL+SHIFT+X).

The results will display in the Test Results window.

Note: To see how to unit test a module initialization class to verify that it registers a service, see the class NavigationModuleInitializerTest in the Navigation.Tests project of the Module QuickStart.

(venv) gapinyc@DESKTOP-9QS7RL5:~/superset$ export SUPERSET_CONFIG_PATH=/home/gapinyc/superset/superset_config.py superset db upgrade Loaded your LOCAL configuration at [/home/gapinyc/superset/superset_config.py] 2025-10-25 13:35:48,420:INFO:superset.initialization:Setting database isolation level to READ COMMITTED INFO [alembic.env] Starting the migration scripts. INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume transactional DDL. INFO [alembic.env] Migration scripts completed. Duration: 00:00:00 (venv) gapinyc@DESKTOP-9QS7RL5:~/superset$ superset init Loaded your LOCAL configuration at [/home/gapinyc/superset/superset_config.py] 2025-10-25 13:38:11,338:INFO:superset.initialization:Setting database isolation level to READ COMMITTED 2025-10-25 13:38:13,070:INFO:superset.security.manager:Syncing role definition 2025-10-25 13:38:13,103:INFO:superset.security.manager:Syncing Admin perms 2025-10-25 13:38:13,107:INFO:superset.security.manager:Syncing Alpha perms 2025-10-25 13:38:13,113:INFO:superset.security.manager:Syncing Gamma perms 2025-10-25 13:38:13,118:INFO:superset.security.manager:Syncing sql_lab perms 2025-10-25 13:38:13,123:INFO:superset.security.manager:Fetching a set of all perms to lookup which ones are missing 2025-10-25 13:38:13,127:INFO:superset.security.manager:Creating missing datasource permissions. 2025-10-25 13:38:13,131:INFO:superset.security.manager:Creating missing database permissions. 2025-10-25 13:38:13,133:INFO:superset.security.manager:Cleaning faulty perms (venv) gapinyc@DESKTOP-9QS7RL5:~/superset$
10-26
内容概要:本文档是一份关于“超声谐波成像中幅超声谐波成像中幅度调制聚焦超声引起的全场位移和应变的分析模型(Matlab代码实现)度调制聚焦超声引起的全场位移和应变的分析模型”的Matlab代码实现研究资料,重点构建了一个用于分析在超声谐波成像过程中,由幅度调制聚焦超声所引发的生物组织全场位移与应变的数学模型。该模型通过Matlab仿真手段实现了对声场激励下组织力学响应的精确计算与可视化,有助于深入理解超声激励与组织变形之间的物理机制,提升超声弹性成像的精度与可靠性。文档还附带多个相关科研领域的Matlab/Simulink代码实例,涵盖无人机控制、路径规划、电力系统仿真、信号处理、机器学习等多个方向,展示了强大的技术支撑与应用拓展能力。; 适合人群:具备Matlab编程基础,从事医学超声成像、生物力学建模、信号与图像处理等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①用于超声弹性成像中组织力学响应的仿真与分析;②为开发新型超声诊断技术提供理论模型与算法支持;③作为多物理场耦合仿真的教学与研究案例,促进跨学科技术融合。; 阅读建议:建议读者结合Matlab代码逐行理解模型实现细节,重点关注声场建模、组织力学方程求解及位移应变后处理部分。同时可参考文档中提供的其他仿真案例,拓宽研究思路,提升综合科研能力。
标题基于SpringBoot的高校餐饮档口管理系统设计与实现AI更换标题第1章引言介绍高校餐饮档口管理系统的研究背景、意义、国内外现状及论文方法与创新点。1.1研究背景与意义阐述高校餐饮档口管理现状及系统开发的重要性。1.2国内外研究现状分析国内外高校餐饮管理系统的研究与应用进展。1.3研究方法及创新点概述本文采用的研究方法及系统设计的创新之处。第2章相关理论总结与高校餐饮档口管理系统相关的现有理论。2.1SpringBoot框架理论阐述SpringBoot框架的原理、优势及其在Web开发中的应用。2.2数据库设计理论介绍数据库设计的基本原则、方法和步骤。2.3系统安全理论讨论系统安全设计的重要性及常见安全措施。第3章系统需求分析对高校餐饮档口管理系统的功能需求、性能需求等进行详细分析。3.1功能需求分析列举系统需实现的主要功能,如档口管理、订单处理等。3.2性能需求分析分析系统对响应时间、并发处理能力等性能指标的要求。3.3非功能需求分析阐述系统对易用性、可维护性等非功能方面的需求。第4章系统设计详细描述高校餐饮档口管理系统的设计过程。4.1系统架构设计给出系统的整体架构,包括前端、后端和数据库的设计。4.2模块设计详细介绍各个功能模块的设计,如用户管理、档口信息管理等。4.3数据库设计阐述数据库表结构的设计、数据关系及索引优化等。第5章系统实现与测试介绍高校餐饮档口管理系统的实现过程及测试方法。5.1系统实现系统各模块的具体实现过程,包括代码编写和调试。5.2系统测试方法介绍系统测试的方法、测试用例设计及测试环境搭建。5.3系统测试结果与分析从功能、性能等方面对系统测试结果进行详细分析。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括高校餐饮档口管理系统的设计与实现成果。6.2展望指出系统存在的不足及未来改进和扩展的方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值