Giter VIP home page Giter VIP logo

domain_patterns's Introduction

crates.io MIT licensed Released API docs

Domain Patterns

This project provides patterns from the world of Domain Driven Design.

Repository Trait

This trait defines characteristics of a repository, which is a collection like abstraction over database access. This trait is modeled very closely to function signatures used by the standard libraries HashMap because that is the closest analog. There are some key differences though, largely around ownership. The standard library wants to own it's values, but in the case of a collection "like" abstraction over database access, it doesn't make sense for a repository to own it's data. The database owns the data and that data is passed to the repository which constructs an entity and returns that entity to the caller.

Due to the nature of the abstraction, it makes more sense for the Repository to take in references (because that's all it needs to persist the data to an underlying storage system) and return owned values.

Unlike the standard libraries HashMap api, the insert does not update the value at the key, if the key already exists. This is to prevent misuse of the repository. The logic is flipped from HashMap's insert method. If the key already exists, then None is returned. If the key does not exist, then the entity itself is returned. This is useful for cases in which we want to update an entity with computed data from a database and return that to the caller.

The other way in which this differs from the API for the standard libraries HashMap is that all methods return a Result. This is due to the fact that we might have a failure to communicate with the underlying storage mechanism, or a concurrency related error that needs to be communicated back to the caller. The success case very closely matches what you get from the standard library HashMap while the failure case communicates an issue with the underlying storage mechanism.

Entity Trait

The entity trait simply defines that an entity must have some sort of persistent identity. This is established with a single function signature that ensures any Entity must have an id() method that returns a globally unique id of some kind.

ValueObject Trait

The ValueObject trait defines characteristics of a value object, which is an object that holds some immutable value, and validates incoming data to make sure it conforms to certain requirements. An example would be if you have an Email struct. At all times that struct should only hold valid email addresses. If Email implements ValueObject trait, then the implementor will be required to write a try_from implementation, which should in turn call their implementation of validate and essentially return an error if validation fails, or create a value object upon success. Some rules for value objects are:

  1. Value objects are immutable.
  2. Value objects should validate data that is used to construct them (the "value" they hold after successful validation).
  3. Value objects do not have globally unique identity.

License: MIT

domain_patterns's People

Contributors

prismaphonic 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.