Giter Club home page Giter Club logo

entity_collection's Introduction

Provides an EntityMap that holds a collection of Entity classes with id and Immutable operations Provider

Features

It provides an Entity abstract class with an id property of type Id and an EntityMap class that manages a collection of Entities.

The motivation for this package is to provide a way to operate on Entity collections in an Immutable way.

To add Entity/Id pairs in Dart's Map class in an Immutable way, one must write

/// put entity
final putEntityMap = {...entityMap, id: entity}

/// remove entity
final removeEntityMap = {...entityMap}..removeWhere((id, _) => id == entity.id);

In this package, the EntityMap class, which wraps the Map class, can be used to perform Immutable operations on Entity in a readable.

/// put entity
final putEntityMap = entityMap.put(entity);

/// remove entity
final removeEntityMap = entityMap.remove(entity);

Usage

Define Entity

As an example, we define the User class. The User class is an Entity with String type id and name as properties.

class User implements Entity<String> {
  User(this.id, this.name);

  @override
  final String id;
  final String name;
}

Declare EntityMap<String, User> to manage a collection of User classes. Since EntityMap<String, User> is long, define Users as an alias.

typedef Users = EntityMap<String, User>;

This completes the preparation. Now we can use the API of the EntityMap class to operate on the User collection Immutably.

final user1 = User("1", "Anya");
final user2 = User("2", "Loid");
final user3 = User("3", "Yor");

// create Empty Users
final users = Users.empty();

// putUsers.entities: [user1]
final putUsers = users.put(user1);

// removeUsers.entities: []
final removeUsers = putUsers.remove(user1);

// putAllUsers.entities: [user1, user2]
final putAllUsers = removeUsers.putAll([user1, user2]);

// findUser: user1
final findUser = putAllUsers.byId(user1.id);

// removeAllUsers: []
final removeAllUsers = putAllUsers.removeAll([user1, user2]);

// whereUsers: [user1]
final whereUsers = putAllUsers.where((e) => e.name == user1.name);

// sorted: [user3, user2, user1]
final spyFamily = Users.fromIterable([user1, user2, user3]);
final sorted = spyFamily.sorted((a, b) => b.id.compareTo(a.id));

Extension

If desired, a method can be added to the Users class using extension. This can be a very effective way to add User-specific processing.

/// names: (Anya, Loid, Yor)
final spyFamily = Users.fromIterable([user1, user2, user3]);
final names = spyFamily.names;

entity_collection's People

Contributors

soraef avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.