sinew_models 1.0.0
sinew_models: ^1.0.0 copied to clipboard
Domain, Remote, Entity, envelope bases, paging shapes, ViewState and Result.
sinew_models #
The shapes every Sinew app shares: domain models, envelopes, Result and ViewState.
Sinew keeps three kinds of model apart, so a backend that renames a field touches one file. Domain is what the app needs, in business language. Remote is what the backend sends, shaped like its JSON. Entity is what is stored on the device. Remote and Entity each map to a Domain with toDomain().
Features #
Domain,RemoteandEntity, with value equality fromprops(noequatabledependency)- Envelope bases (
ObjectEnvelope,ListEnvelope,PagedEnvelope,VoidEnvelope) that map any backend's body to one domain shape, and report a failure inside a 2xx body Result<T>: aSuccessthat carries the backend's message, or a typedFailure, read withwhenViewState<T>:Loading,DoneandFailed, whereLoadingandFailedcan keep the previous dataPagingDomainandPagingMetaDomain: one page shape for every backend
Getting started #
dart pub add sinew_models
Usage #
Map a response body to the domain, and read the outcome:
final class Order extends Domain {
const Order(this.id, this.title);
final int id;
final String title;
@override
List<Object?> get props => [id, title];
}
final class OrderRemote extends Remote<Order> {
const OrderRemote(this.id, this.title);
final int id;
final String title;
@override
Order toDomain() => Order(id, title);
}
final class OrderEnvelope extends ObjectEnvelope<OrderRemote, Order> {
const OrderEnvelope(this.data);
@override
final OrderRemote? data;
}
final result = Result.success(const OrderEnvelope(OrderRemote(7, 'Two coffees')).toDomain());
final text = result.when(
success: (order, message) => 'Loaded ${order.title}',
failure: (error, stackTrace) => 'Failed: ${error.code}',
);
The full program is in example/main.dart.
Related packages #
Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.
Documentation #
- Usage docs: sinew.srctool.com
- API reference: pub.dev/documentation/sinew_models
- Source and issues: srctool/sinew-dart
- Design and decisions: the Guide
License #
Apache 2.0, see LICENSE.