Skip to content

Commit 45e1915

Browse files
committed
Updated readme
1 parent 5548e9e commit 45e1915

File tree

1 file changed

+104
-8
lines changed

1 file changed

+104
-8
lines changed

README.md

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,128 @@ Features:
222222
* Roslyn Analyzers and CodeFixes help the developers to implement the Value Objects correctly
223223
* Allows custom properties and methods
224224
* Provides appropriate factory methods for creation of new value objects based on the specified properties/fields
225-
* Allows custom validation of [constructor](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#validation-of-the-constructor-arguments) and [factory method](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#validation-of-the-factory-method-arguments) arguments
225+
* Allows custom [validation](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#validation) of [constructor](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#validation-of-the-constructor-arguments) and [factory method](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#validation-of-the-factory-method-arguments) arguments
226226
* Additional features for [simple Value Objects (1 "key"-property/field)](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#simple-value-objects) and [complex Value Objects (2 properties/fields or more)](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#complex-value-objects)
227227
* Simple Value Objects: allows cast and type conversion from key-type to Value Object and vice versa
228-
* Simple Value Objects: provides an implementation of `IComparable<T>` if the key-property/field is an `IComparable<T>` or has an `IComparer<T>`
229228
* Simple Value Objects: provides an implementation of `IFormattable` if the key-property/field is an `IFormattable`
230229
* Provides proper implementation of `Equals`, `GetHashCode`, `ToString` and equality comparison via `==` and `!=`
231-
* [Allows custom equality comparison](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#custom-comparer)
230+
* Provides implementation of `IComparable`, `IComparable<T>`, `IFormattable`, `IParsable<T>` and comparison operators `<`, `<=`, `>`, `>=`
231+
* [Allows custom equality comparison](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#custom-comparer) and [custom comparer](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#custom-comparer)
232232
* Handling of [null](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#null-in-factory-methods-yields-null) and [empty strings](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#empty-string-in-factory-methods-yields-null)
233233
* [JSON support](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#json-serialization) (`System.Text.Json` and `Newtonsoft.Json`)
234-
* [ASP.NET Core support](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#support-for-aspnet-core-model-binding) (model binding and model validation)
234+
* [Support for Minimal Web Api Parameter Binding and ASP.NET Core Model Binding](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#support-for-minimal-web-api-parameter-binding-and-aspnet-core-model-binding)
235235
* [Entity Framework Core support](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#support-for-entity-framework-core) (`ValueConverter`)
236236
* [MessagePack support](https://github.com/PawelGerr/Thinktecture.Runtime.Extensions/wiki/Value-Objects#messagepack-serialization) (`IMessagePackFormatter`)
237237

238-
Definition of a value object with 1 custom property `Value`. All other features mentioned above are generated by the [Roslyn Source Generators](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview) in the background.
238+
## Simple Value Object
239+
240+
Definition of 2 value objects, one with 1 `string` property `Value` and the other with an `int`.
239241

240242
```C#
241243
[ValueObject]
242-
public partial class ProductName
244+
public sealed partial class ProductName
243245
{
244246
public string Value { get; }
247+
}
245248

246-
// The member can be a private readoly field as well
247-
//private readonly string _value;
249+
[ValueObject]
250+
public sealed partial class Amount
251+
{
252+
private readonly int _value;
248253
}
249254
```
250255

256+
After the implementation of the `ProductName`, a Roslyn source generator kicks in and implements the rest. Following API is available from now on.
257+
258+
```C#
259+
// Factory method for creation of new instances.
260+
// Throws ValidationException if the validation fails
261+
ProductName bread = ProductName.Create("Bread");
262+
263+
// Alternatively, using an explicit cast (behaves the same as with Create)
264+
ProductName bread = (ProductName)"Bread"; // is the same as calling "ProductName.Create"
265+
266+
-----------
267+
268+
// the same as above but returns a bool instead of throwing an exception (dictionary-style)
269+
bool created = ProductName.TryCreate("Milk", out ProductName milk);
270+
271+
-----------
272+
273+
// similar to TryCreate but returns a ValidationResult instead of a boolean.
274+
ValidationResult? validationResult = ProductName.Validate("Milk", out var milk);
275+
276+
if (validationResult == ValidationResult.Success)
277+
{
278+
logger.Information("Product name {Name} created", milk);
279+
}
280+
else
281+
{
282+
logger.Warning("Failed to create product name. Validation result: {ValidationResult}", validationResult.ErrorMessage);
283+
}
284+
285+
-----------
286+
287+
// implicit conversion to the type of the key member
288+
string valueOfTheProductName = bread; // "Bread"
289+
290+
-----------
291+
292+
// Equality comparison with 'Equals'
293+
// which compares the key members using default or custom 'IEqualityComparer<T>'
294+
bool equal = bread.Equals(bread);
295+
296+
-----------
297+
298+
// Equality comparison with '==' and '!='
299+
bool equal = bread == bread;
300+
bool notEqual = bread != bread;
301+
302+
-----------
303+
304+
// Hash code
305+
int hashCode = bread.GetHashCode();
306+
307+
-----------
308+
309+
// 'ToString' implementation
310+
string value = bread.ToString(); // "Bread"
311+
312+
------------
313+
314+
// Implements IParsable<T> which is especially helpful with minimal web apis.
315+
// This feature can be disabled if it doesn't make sense (see ValueObjectAttribute).
316+
bool success = ProductName.TryParse("New product name", null, out var productName);
317+
318+
------------
319+
320+
// Implements "IFormattable" if the key member is an "IFormattable".
321+
// This feature can be disabled if it doesn't make sense (see ValueObjectAttribute).
322+
var amount = Amount.Create(42);
323+
string formattedValue = amount.ToString("000", CultureInfo.InvariantCulture); // "042"
324+
325+
------------
326+
327+
// Implements "IComparable<ProductName>" if the key member is an "IComparable"
328+
// This feature can be disabled if it doesn't make sense (see ValueObjectAttribute).
329+
var amount = Amount.Create(1);
330+
var otherAmount = Amount.Create(2);
331+
332+
var comparison = amount.CompareTo(otherAmount); // -1
333+
334+
// Implements comparison operators (<,<=,>,>=) if the key member has comparison operators itself.
335+
// This feature can be disabled if it doesn't make sense (see ValueObjectAttribute).
336+
var isBigger = amount > otherAmount;
337+
338+
------------
339+
340+
// Implements addition / subtraction / multiplication / division if the key member supports operators
341+
// This feature can be disabled if it doesn't make sense (see ValueObjectAttribute).
342+
var sum = amount + otherAmount;
343+
```
344+
345+
## Complex Value Object
346+
251347
Definition of a complex value object with 2 properties and a custom validation of the arguments.
252348

253349
```C#

0 commit comments

Comments
 (0)