Minerals.AutoDomain 0.4.0

dotnet add package Minerals.AutoDomain --version 0.4.0                
NuGet\Install-Package Minerals.AutoDomain -Version 0.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Minerals.AutoDomain" Version="0.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Minerals.AutoDomain --version 0.4.0                
#r "nuget: Minerals.AutoDomain, 0.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Minerals.AutoDomain as a Cake Addin
#addin nuget:?package=Minerals.AutoDomain&version=0.4.0

// Install Minerals.AutoDomain as a Cake Tool
#tool nuget:?package=Minerals.AutoDomain&version=0.4.0                

Minerals.AutoDomain

WORK IN PROGRESS...

This package, with the help of an incremental source generator, provides a fast method of defining domain layer components with their specific implementation, such as aggregates, entities and domain events.

Features

  • Reduced boilerplate: Spend less time writing boilerplate code and more time crafting the business logic of your application.
  • Strongly typed identifier: Package has integrated automatic generation of strongly typed identifiers for every entity and aggregate.
  • Domain events: Package gives you the ability to customize arguments for automatically generated domain events.
  • Compatible with .NET Standard 2.0 and C# 7.3+: Works on a wide range of platforms and development environments.

Installation

Add the Minerals.AutoDomain nuget package to your C# project using the following methods:

1. Project file definition

<PackageReference Include="Minerals.AutoDomain" Version="0.4.0" />

2. dotnet command

dotnet add package Minerals.AutoDomain

Usage

Package is used by adding the selected attribute to the suitable object.

Entity

Sample code fragment that is needed to generate Entity, please notice that the class that implements the Entity attribute for correct functioning should have a partial modifier.

// ...
[Minerals.AutoDomain.Entity]
public partial class ExampleEntity
{
    // Your code here...
}
// ...

The code above will generate a two files with the following code:

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class ExampleEntity : global::Minerals.AutoDomain.IEntity, global::System.IEquatable<ExampleEntity>
{
    public ExampleEntityId Id { get; private set; }

    public bool Equals(ExampleEntity other)
    {
        return other.Id.Value.Equals(Id.Value);
    }

    public override bool Equals(object obj)
    {
        return obj is ExampleEntity other && other.Id.Value.Equals(Id.Value);
    }

    public override int GetHashCode()
    {
        return Id.Value.GetHashCode();
    }

    public static bool operator ==(ExampleEntity left, ExampleEntity right)
    {
        return left != null && right != null && left.Id.Value.Equals(right.Id.Value);
    }

    public static bool operator !=(ExampleEntity left, ExampleEntity right)
    {
        return (left != null && right == null) || (left == null && right != null) || !left.Id.Value.Equals(right.Id.Value);
    }
}

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public readonly struct ExampleEntityId : global::System.IEquatable<ExampleEntityId>
{
    public int Value => _value;

    private readonly int _value;

    public ExampleEntityId(int value)
    {
        _value = value;
    }

    public bool Equals(ExampleEntityId other)
    {
        return other.Value.Equals(Value);
    }

    public override bool Equals(object obj)
    {
        return obj is ExampleEntityId other && other.Value.Equals(Value);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }

    public static bool operator ==(ExampleEntityId left, ExampleEntityId right)
    {
        return left.Value.Equals(right.Value);
    }

    public static bool operator !=(ExampleEntityId left, ExampleEntityId right)
    {
        return !left.Value.Equals(right.Value);
    }
}

AggregateRoot

Sample code fragment that is needed to generate AggregateRoot, please notice that the class that implements the AggregateRoot attribute for correct functioning should have a partial modifier.

// ...
[Minerals.AutoDomain.AggregateRoot]
public partial class ExampleAggregateRoot
{
    // Your code here...
}
// ...

The code above will generate a two files with the following code:

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class ExampleAggregate : global::Minerals.AutoDomain.IAggregateRoot, global::System.IEquatable<ExampleAggregate>
{
    public ExampleAggregateId Id { get; private set; }

    public global::System.Collections.Generic.IReadOnlyCollection<global::Minerals.AutoDomain.IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();

    private readonly global::System.Collections.Generic.List<global::Minerals.AutoDomain.IDomainEvent> _domainEvents = new global::System.Collections.Generic.List<global::Minerals.AutoDomain.IDomainEvent>();

    public void AppendDomainEvent(global::Minerals.AutoDomain.IDomainEvent domainEvent)
    {
        _domainEvents.Add(domainEvent);
    }

    public void ClearDomainEvents()
    {
        _domainEvents.Clear();
    }

    public bool Equals(ExampleAggregate other)
    {
        return other.Id.Value.Equals(Id.Value);
    }

    public override bool Equals(object obj)
    {
        return obj is ExampleAggregate other && other.Id.Value.Equals(Id.Value);
    }

    public override int GetHashCode()
    {
        return Id.Value.GetHashCode();
    }

    public static bool operator ==(ExampleAggregate left, ExampleAggregate right)
    {
        return left != null && right != null && left.Id.Value.Equals(right.Id.Value);
    }

    public static bool operator !=(ExampleAggregate left, ExampleAggregate right)
    {
        return (left != null && right == null) || (left == null && right != null) || !left.Id.Value.Equals(right.Id.Value);
    }
}

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public readonly struct ExampleAggregateId : global::System.IEquatable<ExampleAggregateId>
{
    public int Value => _value;

    private readonly int _value;

    public ExampleAggregateId(int value)
    {
        _value = value;
    }

    public bool Equals(ExampleAggregateId other)
    {
        return other.Value.Equals(Value);
    }

    public override bool Equals(object obj)
    {
        return obj is ExampleAggregateId other && other.Value.Equals(Value);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }

    public static bool operator ==(ExampleAggregateId left, ExampleAggregateId right)
    {
        return left.Value.Equals(right.Value);
    }

    public static bool operator !=(ExampleAggregateId left, ExampleAggregateId right)
    {
        return !left.Value.Equals(right.Value);
    }
}

Domain Events

The code below will generate a partial struct with comparison methods.

[Minerals.AutoDomain.DomainEvent]
public readonly partial struct ExampleDomainEvent
{
    public string Property1 { get; }
    public readonly int Field1;
}

The code fragment shown above will generate the following domain events code, please notice that the generated events are in the Events sub-namespace.

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public readonly partial struct ExampleDomainEvent : global::Minerals.AutoDomain.IDomainEvent, global::System.IEquatable<ExampleDomainEvent>
{
    public bool Equals(ExampleDomainEvent other)
    {
        return other.Field1.Equals(Field1) && other.Property1.Equals(Property1);
    }

    public override bool Equals(object obj)
    {
        return obj is ExampleDomainEvent other && other.Field1.Equals(Field1) && other.Property1.Equals(Property1);
    }

    public override int GetHashCode()
    {
        return global::System.HashCode.Combine(Field1, Property1);
    }

    public static bool operator ==(ExampleDomainEvent left, ExampleDomainEvent right)
    {
        return left.Field1.Equals(right.Field1) && left.Property1.Equals(right.Property1);
    }

    public static bool operator !=(ExampleDomainEvent left, ExampleDomainEvent right)
    {
        return !left.Field1.Equals(right.Field1) || !left.Property1.Equals(right.Property1);
    }
}

Generate Domain Events

The code shown below will generate three domain events. The GenerateDomainEvent attribute can be used on classes, structures, records, methods, constructors and properties. The GenerateDomainEvent attribute will inherit arguments from objects or members that will be implementing it.

using Minerals.AutoDomain;

namespace ExampleNamespace
{
    [AggregateRoot, GenerateDomainEvent("ExampleClassUpdatedDomainEvent")]
    public partial class ExampleClass
    {
        [GenerateDomainEvent("ExampleClassCreatedDomainEvent")]
        public ExampleClass(int exampleNumber, string exampleText)
        {
            // ...
            AppendDomainEvent(new ExampleClassCreatedDomainEvent(Id, exampleNumber, exampleText));
        }

        // First argument: Name of the domain event
        // Second argument: Should the domain event have the parent identifier as an argument
        [GenerateDomainEvent("WorkDoneDomainEvent", false)]
        public void DoWork(int arg1, int arg2, int arg3)
        {
            // ...
            AppendDomainEvent(new WorkDoneDomainEvent(arg1, arg2, arg3));
        }
    }
}

The code fragment shown above will generate the following domain events code, please notice that the generated events are in the Events sub-namespace.

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly partial struct ExampleClassUpdatedDomainEvent : global::Minerals.AutoDomain.IDomainEvent, global::System.IEquatable<ExampleClassUpdatedDomainEvent>
    {
        public ExampleClassId ExampleClassId { get; }

        public ExampleClassUpdatedDomainEvent(ExampleClassId exampleClassId)
        {
            ExampleClassId = exampleClassId;
        }

        public bool Equals(ExampleClassUpdatedDomainEvent other)
        {
            return other.ExampleClassId.Equals(ExampleClassId);
        }

        public override bool Equals(object obj)
        {
            return obj is ExampleClassUpdatedDomainEvent other && other.ExampleClassId.Equals(ExampleClassId);
        }

        public override int GetHashCode()
        {
            return global::System.HashCode.Combine(ExampleClassId);
        }

        public static bool operator ==(ExampleClassUpdatedDomainEvent left, ExampleClassUpdatedDomainEvent right)
        {
            return left.ExampleClassId.Equals(right.ExampleClassId);
        }

        public static bool operator !=(ExampleClassUpdatedDomainEvent left, ExampleClassUpdatedDomainEvent right)
        {
            return !left.ExampleClassId.Equals(right.ExampleClassId);
        }
    }
}

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly partial struct ExampleClassCreatedDomainEvent : global::Minerals.AutoDomain.IDomainEvent, global::System.IEquatable<ExampleClassCreatedDomainEvent>
    {
        public ExampleClassId ExampleClassId { get; }
        public int ExampleNumber { get; }
        public string ExampleText { get; }

        public ExampleClassCreatedDomainEvent(ExampleClassId exampleClassId, int exampleNumber, string exampleText)
        {
            ExampleClassId = exampleClassId;
            ExampleNumber = exampleNumber;
            ExampleText = exampleText;
        }

        public bool Equals(ExampleClassCreatedDomainEvent other)
        {
            return other.ExampleClassId.Equals(ExampleClassId) && other.ExampleNumber.Equals(ExampleNumber) && other.ExampleText.Equals(ExampleText);
        }

        public override bool Equals(object obj)
        {
            return obj is ExampleClassCreatedDomainEvent other && other.ExampleClassId.Equals(ExampleClassId) && other.ExampleNumber.Equals(ExampleNumber) && other.ExampleText.Equals(ExampleText);
        }

        public override int GetHashCode()
        {
            return global::System.HashCode.Combine(ExampleClassId, ExampleNumber, ExampleText);
        }

        public static bool operator ==(ExampleClassCreatedDomainEvent left, ExampleClassCreatedDomainEvent right)
        {
            return left.ExampleClassId.Equals(right.ExampleClassId) && left.ExampleNumber.Equals(right.ExampleNumber) && left.ExampleText.Equals(right.ExampleText);
        }

        public static bool operator !=(ExampleClassCreatedDomainEvent left, ExampleClassCreatedDomainEvent right)
        {
            return !left.ExampleClassId.Equals(right.ExampleClassId) || !left.ExampleNumber.Equals(right.ExampleNumber) || !left.ExampleText.Equals(right.ExampleText);
        }
    }
}

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain.Generators
// Version: {Version}
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly partial struct WorkDoneDomainEvent : global::Minerals.AutoDomain.IDomainEvent, global::System.IEquatable<WorkDoneDomainEvent>
    {
        public int Arg1 { get; }
        public int Arg2 { get; }
        public int Arg3 { get; }

        public WorkDoneDomainEvent(int arg1, int arg2, int arg3)
        {
            Arg1 = arg1;
            Arg2 = arg2;
            Arg3 = arg3;
        }

        public bool Equals(WorkDoneDomainEvent other)
        {
            return other.Arg1.Equals(Arg1) && other.Arg2.Equals(Arg2) && other.Arg3.Equals(Arg3);
        }

        public override bool Equals(object obj)
        {
            return obj is WorkDoneDomainEvent other && other.Arg1.Equals(Arg1) && other.Arg2.Equals(Arg2) && other.Arg3.Equals(Arg3);
        }

        public override int GetHashCode()
        {
            return global::System.HashCode.Combine(Arg1, Arg2, Arg3);
        }

        public static bool operator ==(WorkDoneDomainEvent left, WorkDoneDomainEvent right)
        {
            return left.Arg1.Equals(right.Arg1) && left.Arg2.Equals(right.Arg2) && left.Arg3.Equals(right.Arg3);
        }

        public static bool operator !=(WorkDoneDomainEvent left, WorkDoneDomainEvent right)
        {
            return !left.Arg1.Equals(right.Arg1) || !left.Arg2.Equals(right.Arg2) || !left.Arg3.Equals(right.Arg3);
        }
    }
}

Domain Event Dispatcher

Since version 0.4.0, the package comes with a domain event dispatcher. Domain event handlers are automatically registered into the IoC container.

// Adding to IoC container
builder.Services.AddDomainEventDispatcher();

// Dispatching domain events
private readonly IDomainEventDispatcher _dispatcher;
// ...
_dispatcher.Dispatch(new ExampleDomainEvent(), cancellationToken);
// ...

// Handling of domain events
public class ExampleDomainEventHandler : IDomainEventHandler<ExampleDomainEvent>
{
    public Task Handle(ExampleEvent domainEvent, CancellationToken cancellationToken)
    {
        // ...
    }
}

Versioning

We use SemVer for versioning. For the versions available, see the branches on this repository.

Authors

  • Szymon Hałucha - Maintainer

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.0 130 5/25/2024
0.3.0 128 5/24/2024
0.2.0 121 5/19/2024
0.1.0 121 5/16/2024

Added IDomainEventDispatcher and IDomainEventHandler