Flutter通用服务插件pip_services3_commons的使用
Flutter通用服务插件pip_services3_commons的使用
Portable Abstractions and Patterns for Dart #
此模块是Pip.Services多语言微服务工具包的一部分。它提供了在微服务或后端服务中使用的常见模式集。此外,该模块还为工具包支持的所有语言实现了合理薄层的抽象层,以促进对称实现。
该模块包含以下包:
- Commands - 命令与事件模式
- Config - 配置框架
- Convert - 软值转换器
- Data - 数据模式
- Errors - 应用程序错误
- Random - 随机数据生成器
- Refer - 定位器(IoC)模式
- Reflect - 反射框架
- Run - 执行框架
- Validate - 验证框架
快速链接:
使用 #
在 pubspec.yaml
文件中添加以下依赖项:
dependencies:
pip_services3_commons: version
然后从命令行安装包:
pub get
这样你就可以开始使用 Pip.Services 模式来增强你的后端代码了。
例如,这里是如何实现一个组件,该组件接收配置、分配引用并能够通过此模块中的模式打开和关闭。
class MyComponentA implements IConfigurable, IReferenceable, IOpenable {
MyComponentA();
String _param1 = 'ABC';
int _param2 = 123;
MyComponentB _anotherComponent;
bool _opened = true;
@override
void configure(ConfigParams config) {
this._param1 = config.getAsStringWithDefault('param1', this._param1);
this._param2 = config.getAsIntegerWithDefault('param2', this._param2);
}
@override
void setReferences(IReferences refs) {
this._anotherComponent = refs.getOneRequired<MyComponentB>(
Descriptor('myservice', 'mycomponent-b', '*', '*', '1.0')
);
}
@override
bool isOpen() {
return this._opened;
}
@override
Future open(String? correlationId) {
return Future(() {
this._opened = true;
print('MyComponentA has been opened.');
});
}
@override
Future close(String? correlationId) {
return Future(() {
this._opened = false;
print('MyComponentA has been closed.');
});
}
}
然后这里是组件如何在代码中使用:
import 'package:pip_services3_commons/src/config/ConfigParams.dart';
import 'package:pip_services3_commons/src/refer/References.dart';
import 'package:pip_services3_commons/src/refer/DependencyResolver.dart';
var myComponentA = MyComponentA();
// 配置组件
myComponentA.configure(ConfigParams.fromTuples([
'param1', 'XYZ',
'param2', 987
]));
// 为组件设置引用
myComponentA.setReferences(References.fromTuples([
Descriptor('myservice', 'mycomponent-b', 'default', 'default', '1.0'), myComponentB
]));
// 打开组件
myComponentA.open('123');
开发 #
对于开发,你需要安装以下前提条件:
- Dart SDK 2
- Visual Studio Code 或其他你选择的 IDE
- Docker
安装依赖项:
pub get
运行自动化测试:
pub run test
生成 API 文档:
./docgen.ps1
在提交更改之前,可以运行 Docker 化构建和测试:
./build.ps1
./test.ps1
./clear.ps1
更多关于Flutter通用服务插件pip_services3_commons的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter通用服务插件pip_services3_commons的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
pip_services3_commons
是一个用于构建可复用和模块化服务的通用插件,适用于 Flutter 和其他多种编程语言。它提供了一组工具和模式,帮助开发者在构建微服务、组件化应用程序时遵循最佳实践。
主要功能
- 配置管理: 支持从多种来源加载配置,如环境变量、配置文件、命令行参数等。
- 组件生命周期管理: 提供统一的组件生命周期管理,包括初始化、启动、停止和关闭。
- 日志记录: 提供统一的日志记录接口,支持多种日志级别和输出格式。
- 数据验证: 提供数据验证工具,确保输入数据的有效性。
- 依赖注入: 支持依赖注入模式,简化组件之间的依赖关系管理。
- 错误处理: 提供统一的错误处理机制,支持自定义错误类型和错误处理逻辑。
- 性能监控: 支持性能监控和统计,帮助开发者优化应用程序性能。
安装
在 pubspec.yaml
文件中添加依赖:
dependencies:
pip_services3_commons: ^3.0.0
然后运行 flutter pub get
安装依赖。
使用示例
1. 配置管理
import 'package:pip_services3_commons/pip_services3_commons.dart';
void main() {
var config = ConfigParams.fromTuples([
'host', 'localhost',
'port', 8080
]);
var host = config.getAsString('host');
var port = config.getAsInteger('port');
print('Host: $host, Port: $port');
}
2. 组件生命周期管理
import 'package:pip_services3_commons/pip_services3_commons.dart';
class MyComponent implements IConfigurable, IReferenceable, IOpenable {
String _status = 'closed';
@override
void configure(ConfigParams config) {
print('Configuring component...');
}
@override
void setReferences(IReferences references) {
print('Setting references...');
}
@override
Future<void> open(String? correlationId) async {
_status = 'open';
print('Component opened.');
}
@override
Future<void> close(String? correlationId) async {
_status = 'closed';
print('Component closed.');
}
@override
bool isOpen() {
return _status == 'open';
}
}
void main() async {
var component = MyComponent();
await component.open(null);
print('Component is open: ${component.isOpen()}');
await component.close(null);
print('Component is open: ${component.isOpen()}');
}
3. 日志记录
import 'package:pip_services3_commons/pip_services3_commons.dart';
void main() {
var logger = ConsoleLogger();
logger.setLevel(LogLevel.Debug);
logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error(null, 'This is an error message');
}
4. 数据验证
import 'package:pip_services3_commons/pip_services3_commons.dart';
void main() {
var schema = Schema()
.withRule(AtLeastOneExistsRule(['field1', 'field2']))
.withRule(ValueComparisonRule('field1', 'EQ', 'value1'));
var result = schema.validate({'field1': 'value1'});
if (result.isEmpty) {
print('Validation succeeded');
} else {
print('Validation errors: ${result.join(', ')}');
}
}
5. 依赖注入
import 'package:pip_services3_commons/pip_services3_commons.dart';
class MyService {
void doSomething() {
print('Doing something...');
}
}
void main() {
var container = Container();
container.registerAsType(Descriptor('my', 'service', '*', '*', '1.0'), MyService);
var service = container.getOneRequired<MyService>(Descriptor('my', 'service', '*', '*', '1.0'));
service.doSomething();
}