Minerals.AutoDomain 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Minerals.AutoDomain --version 0.1.0                
NuGet\Install-Package Minerals.AutoDomain -Version 0.1.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.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Minerals.AutoDomain --version 0.1.0                
#r "nuget: Minerals.AutoDomain, 0.1.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.1.0

// Install Minerals.AutoDomain as a Cake Tool
#tool nuget:?package=Minerals.AutoDomain&version=0.1.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.1.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
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </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 && ((ExampleEntity)obj).Id.Value.Equals(Id.Value);
    }

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

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </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 && ((ExampleEntityId)obj).Value.Equals(Value);
    }

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

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
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </auto-generated>
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Runtime.CompilerServices.CompilerGenerated]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial class ExampleAggregateRoot : global::Minerals.AutoDomain.IAggregateRoot, global::System.IEquatable<ExampleAggregateRoot>
{
    public ExampleAggregateRootId Id { get; private set; }

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

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

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

    public void RemoveDomainEvent(global::Minerals.AutoDomain.IDomainEvent domainEvent)
    {
        _domainEvents.Remove(domainEvent);
    }

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

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

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

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

// And

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

    private readonly int _value;

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

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

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

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

Domain Events

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

using Minerals.AutoDomain;

namespace ExampleNamespace
{
    [AggregateRoot, NewDomainEvent("ExampleClassUpdatedDomainEvent")]
    public partial class ExampleClass
    {
        [NewDomainEvent("ExampleClassCreatedDomainEvent")]
        public ExampleClass(int exampleNumber, string exampleText)
        {
            // ...
            AddDomainEvent(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
        [NewDomainEvent("WorkDoneDomainEvent", false)]
        public void DoWork(int arg1, int arg2, int arg3)
        {
            // ...
            AddDomainEvent(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
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly struct ExampleClassUpdatedDomainEvent : global::Minerals.AutoDomain.IDomainEvent
    {
        public ExampleClassId ExampleClassId { get; }

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

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly struct ExampleClassCreatedDomainEvent : global::Minerals.AutoDomain.IDomainEvent
    {
        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;
        }
    }
}

// And

// <auto-generated>
// This code was generated by a tool.
// Name: Minerals.AutoDomain
// Version: 0.1.0+050a78e4c90754f8ac1d3892073a3baa8e031d27
// </auto-generated>
namespace ExampleNamespace.Events
{
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Runtime.CompilerServices.CompilerGenerated]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public readonly struct WorkDoneDomainEvent : global::Minerals.AutoDomain.IDomainEvent
    {
        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;
        }
    }
}

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.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

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 123 5/25/2024
0.3.0 121 5/24/2024
0.2.0 114 5/19/2024
0.1.0 112 5/16/2024

First release