Your first Value Object
In this tutorial, we'll create and use a Value Object.
Install the package, and then, in your project, create a new Value Object that represents a Customer ID:
[ValueObject<int>]
public partial struct CustomerId { }
If you're not using generics, you can use typeof
instead:
[ValueObject(typeof(int))]
public partial struct CustomerId { }
Now, create a new instance by using the From
method that the source generator generates:
var customerId = CustomerId.From(42);
If you try to use a constructor, then the analyzer rules will catch this and stop you.
You can now be more explicit in your methods with signatures such as:
public void HandlePayment(
CustomerId customerId,
AccountId accountId,
PaymentAmount paymentAmount)
Last modified: 23 November 2024