Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the readme more engaging #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
# ValueOf

Deal with Primitive Obsession - define ValueObjects in a single line (of C#).

---

[![mcintyre321 - ValueOf](https://img.shields.io/static/v1?label=mcintyre321&message=ValueOf&color=blue&logo=github)](https://github.com/mcintyre321/ValueOf "Go to GitHub repo")
[![License: BSD-3-Clause](https://img.shields.io/badge/License-BSD--3--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![stars - ValueOf](https://img.shields.io/github/stars/mcintyre321/ValueOf?style=social)](https://github.com/mcintyre321/ValueOf)
[![forks - ValueOf](https://img.shields.io/github/forks/mcintyre321/ValueOf?style=social)](https://github.com/mcintyre321/ValueOf)

[![issues - ValueOf](https://img.shields.io/github/issues/mcintyre321/ValueOf)](https://github.com/mcintyre321/ValueOf/issues)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/mcintyre321/ValueOf)](https://github.com/mcintyre321/ValueOf/pulls)
[![GitHub contributors](https://img.shields.io/github/contributors/mcintyre321/ValueOf)](https://github.com/mcintyre321/ValueOf/graphs/contributors)
[![GitHub last commit](https://img.shields.io/github/last-commit/mcintyre321/ValueOf)](https://github.com/mcintyre321/ValueOf/commits/master)

[![NuGet](https://img.shields.io/nuget/v/ValueOf.svg)](https://www.nuget.org/packages/ValueOf)
[![NuGet](https://img.shields.io/nuget/dt/ValueOf.svg)](https://www.nuget.org/packages/ValueOf)

---

> install-package ValueOf

## What is this library

> The Smell: Primitive Obsession is using primitive data types to represent domain ideas. For example, we use a String to represent a message, an Integer to represent an amount of money, or a Struct/Dictionary/Hash to represent a specific object.
> The Fix: Typically, we introduce a ValueObject in place of the primitive data, then watch like magic as code from all over the system shows FeatureEnvySmell and wants to be on the new ValueObject. We move those methods, and everything becomes right with the world.
> The Smell: Primitive Obsession is using primitive data types to represent domain ideas. For example, we use a String
> to represent a message, an Integer to represent an amount of money, or a Struct/Dictionary/Hash to represent a
> specific
> object.
> The Fix: Typically, we introduce a ValueObject in place of the primitive data, then watch like magic as code from all
> over the system shows FeatureEnvySmell and wants to be on the new ValueObject. We move those methods, and everything
> becomes right with the world.
> - http://wiki.c2.com/?PrimitiveObsession

ValueOf lets you define ValueObject Types in a single line of code. Use them everywhere to strengthen your codebase.

```
```csharp
public class EmailAddress : ValueOf<string, EmailAddress> { }

...

EmailAddress emailAddress = EmailAddress.From("foo@bar.com");

```

The ValueOf class implements `.Equals` and `.GetHashCode()` for you.

You can use C# 7 Tuples for more complex Types with multiple values:

```
public class Address : ValueOf<(string firstLine, string secondLine, Postcode postcode), Address> { }

```csharp
public class Address : ValueOf<(string firstLine, string secondLine, Postcode postcode), Address> { }
```

### Validation

You can add validation to your Types by overriding the `protected void Validate() { } ` method:

```
```csharp
public class ValidatedClientRef : ValueOf<string, ValidatedClientRef>
{
protected override void Validate()
Expand All @@ -41,9 +63,9 @@ public class ValidatedClientRef : ValueOf<string, ValidatedClientRef>
throw new ArgumentException("Value cannot be null or empty");
}
}

```

## See Also

If you liked this, you'll probably like another project of mine [OneOf](https://github.com/mcintyre321/OneOf) which provides Discriminated Unions for C#, allowing stronger compile time guarantees when writing branching logic.
If you liked this, you'll probably like another project of mine [OneOf](https://github.com/mcintyre321/OneOf) which
provides Discriminated Unions for C#, allowing stronger compile time guarantees when writing branching logic.