• Stars
    star
    758
  • Rank 59,918 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

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

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

...

EmailAddress emailAddress = EmailAddress.From("[email protected]");

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> { }

Validation

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

public class ValidatedClientRef : ValueOf<string, ValidatedClientRef>
{
    protected override void Validate()
    {
        if (string.IsNullOrWhiteSpace(Value))
            throw new ArgumentException("Value cannot be null or empty");
    }
}	

See Also

If you liked this, you'll probably like another project of mine OneOf which provides Discriminated Unions for C#, allowing stronger compile time guarantees when writing branching logic.

More Repositories

1

OneOf

Easy to use F#-like ~discriminated~ unions for C# with exhaustive compile time matching
C#
2,718
star
2

FormFactory

MVC5, Core or standalone - Generate rich HTML5 forms from your ViewModels, or build them programatically
C#
304
star
3

mvc.jquery.datatables

ASP MVC Helpers for connecting IQueryables with the amazing DataTables.net plugin
JavaScript
185
star
4

LinqToAnything

Turn any data source into an IQueryable
C#
87
star
5

EmbeddedResourceVirtualPathProvider

A custom VirtualPathProvider for IIS - load views and assets from Embedded Resources in referenced assemblies
C#
62
star
6

JsonDiffPatch

C#
45
star
7

PieDb

MIT licenced .NET document db with IQueryable support
C#
22
star
8

WebAPI-Proxy

An HTTP proxy built on ASP.NET Web API
C#
20
star
9

metalinq

serialize, modify, and deserialize LINQ Expressions
C#
19
star
10

Harden

Harden your .NET objects with dynamic access controls. Tie domain behaviour to objects not classes!
C#
12
star
11

Noodles

a .NET library for building model driven web/API/mobile* applications on top of ASP MVC or WebApi - SEE BELOW!
JavaScript
9
star
12

Giveth

The less annoying BDD / Gherkin library for .NET
C#
9
star
13

jquery-datatables-column-filter

JavaScript
7
star
14

SimpleScriptRunner

Very simple way to execute MSSQL change scripts
C#
7
star
15

Linqdex

lucene indexing for in-memory collection objects (a proof of concept)
C#
6
star
16

SirenDotNet

Classes for working with Siren hypermedia format
C#
5
star
17

RangeQueryDotNet

Middleware to allow use of custom range headers for filtering and transforming HTTP responses
C#
4
star
18

MigrationScriptGenerator

C#
4
star
19

Handlebars.Net.Mvc

CSS
3
star
20

NTraits

A runtime trait/mixin library for .NET
C#
3
star
21

NhCodeFirst

Code first NHibernate mappings - create your domain model first and generate your database
C#
3
star
22

HyperMapper

A hypermedia (REST) endware for Owin
C#
2
star
23

DependencySort

c# Library for sorting objects in a dependency heirarchy
C#
2
star
24

MiniProfilerDashboard

A portable area to show you your miniprofiler stats
JavaScript
2
star
25

AspNetCoreJTokenModelBinder

ASP.NET Core ModelBinder for NewtonSoft JSON.NET JToken
C#
2
star
26

ObservableCollectionHelpers

C#
2
star
27

Sourcery

Black magic automatic event sourcing in memory database
C#
1
star
28

Walkies

C#
1
star
29

express-jmesrange

TypeScript
1
star
30

SirenToHtmlClientProxy

C#
1
star
31

JsViewEngine

A prototype server side Javascript ASP MVC view engine
JavaScript
1
star
32

Siggs

Generate types at runtime for method signatures for model binding
C#
1
star
33

PiwikTrackerSharp

C#
1
star