Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

TechNobre/PowerUtils.BuildingBlocks.Domain

PowerUtils.BuildingBlocks.Domain

⚠️ DEPRECATED

This package has been discontinued because it never evolved, and the code present in this package does not justify its continuation. It is preferable to implement this code directly in the project.

Logo

Building Blocks to work with domain layers

License: MIT

Support to

  • .NET 7.0
  • .NET 6.0
  • .NET 5.0
  • .NET 3.1

How to use

Install NuGet package

This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.BuildingBlocks.Domain

Nuget

Install-Package PowerUtils.BuildingBlocks.Domain

.NET CLI

dotnet add package PowerUtils.BuildingBlocks.Domain

Interfaces

  • IAggregateRoot to set an entity as root Entity;
  • IEntityBase to define a class as an entity;
  • IValueObjectBase to define a class or record as an ValueObject;

EntityBase

  • The setter for Id to .NET 5 or more is init - public virtual TId Id { get; init; }
  • The setter for Id to versions below .NET 5 is protected - public virtual TId Id { get; protected set; }

Entities are compared by identity (Id)

public class Client : EntityBase<Guid>
{
    public string Name { get; init; }

    public Client(string name)
        : base(Guid.NewGuid())
        => Name = name;
}

EntityBase.Equals()

var client1 = new Client("Nelson Nobre"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a
var client2 = new Client("John smith"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a

// result = true
var result = client1.Equals(client2);

EntityBase.operator ==

var client1 = new Client("Nelson Nobre"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a
var client2 = new Client("John smith"); // Id = b995cd6c-d2eb-471b-92ad-5c641b90fa82

// result = false
var result = client1 == client2;

EntityBase.operator !=

var client1 = new Client("Nelson Nobre"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a
var client2 = new Client("John smith"); // Id = b995cd6c-d2eb-471b-92ad-5c641b90fa82

// result = true
var result = client1 != client2;

EntityComparer

var comparer = EntityComparer<Guid>.Default;
var list = new Dictionary<Client, int>(comparer);

var client1 = new Client("Nelson Nobre"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a
var client2 = new Client("John smith"); // Id = 14e3d0d4-e472-4de4-96d2-2992c537c73a

list.Add(client1, 1);
list.Add(client2, 1);

ValueObjectBase

  • For older .NET versions is available ValueObjectBase based on class;

⚠️ DEPRECATED

This abstraction for ".NET 5 or later" doesn't provide any additional benefit as it simply restricts the implementation of value objects to records. It's commonly recommended to use structs, record structs, and similar constructs for value object implementations. Therefore, utilizing the IValueObjectBase interface to indicate the implementation as a value object is more than sufficient and isn't restricted to specific implementation types. The implementation will be completely removed after 2024/01/14.

ValueOjects are compared by all properties

public record Address : ValueObjectBase
{
    public string Country { get; init; }
    public string Region { get; init; }

    public Address(string country, string region)
    {
        Country = country;
        Region = region;
    }
}

ValueObjectBase.Equals()

var address1 = new Address("Lisbon", "Portugal");
var address2 = new Address("Algarve", "Portugal");

// result = false
var result = address1.Equals(address2);

ValueObjectBase.operator ==

var address1 = new Address("Lisbon", "Portugal");
var address2 = new Address("Lisbon", "Portugal");

// result = true
var result = address1 == address2;

ValueObjectBase.operator !=

var address1 = new Address("Lisbon", "Portugal");
var address2 = new Address("Lisbon", "Portugal");

// result = false
var result = address1 != address2;

Specification

public class AvailableProductSpec : Specification<Product>
{
    public override Expression<Func<Product, bool>> ToExpression()
        => product => !product.Deleted
                    && product.Quantity > 0;
}
...
...
var product = new Product();

if(AvailableProductSpec().IsSatisfiedBy(product))
{
    ...
}
...
...
if(AvailableProductSpec().IsNotSatisfiedBy(product))
{
    ...
}

Contribution

If you have any questions, comments, or suggestions, please open an issue or create a pull request

Packages

No packages published

Contributors 3

  •  
  •  
  •