Skip to content

Entity Registration

Casey edited this page Apr 6, 2025 · 7 revisions

EntityAxis provides flexible, fluent APIs to register your entities and their services using dependency injection. This simplifies your setup, promotes consistency, and enables automatic handler and validator discovery.


✍️ Command Service Registration

Use AddEntityAxisCommandService to register a service that supports create, update, and delete operations:

services.AddEntityAxisCommandService<IProductCommandService, ProductService, Product, Guid>();

This registers the following interfaces:

  • IProductCommandService
  • ICommandService<Product, Guid>
  • ICreate<Product, Guid>
  • IUpdate<Product, Guid>
  • IDelete<Product, Guid>

💡 You should always register the generic interface ICommandService<TEntity, TKey> explicitly. This ensures consistency and interoperability across libraries. If you're using the MediatR integration, it is required to resolve command handlers correctly.


🔍 Query Service Registration

Use AddEntityAxisQueryService to register a service that supports read operations:

services.AddEntityAxisQueryService<IProductQueryService, ProductService, Product, Guid>();

This registers the following interfaces:

  • IProductQueryService
  • IQueryService<Product, Guid>
  • IGetById<Product, Guid>
  • IGetAll<Product, Guid>
  • IGetPaged<Product, Guid>

💡 Likewise, IQueryService<TEntity, TKey> should be explicitly registered to support consistent resolution — and is necessary when using the MediatR implementation.


🧩 Supporting Custom Interfaces

EntityAxis fully supports services that implement custom command and query contracts, allowing you to define rich, domain-specific interfaces without sacrificing the benefits of generic CRUD operations.

public interface IProductCommandService : ICommandService<Product, Guid>
{
    Task DeactivateProductAsync(Guid id);
}

As long as your custom interface inherits from the appropriate generic interface (ICommandService<TEntity, TKey> or IQueryService<TEntity, TKey>), EntityAxis will automatically detect and register it during service scanning.

💡 This approach gives you the best of both worlds: generic infrastructure for common operations and custom contracts for specialized business logic — all discoverable and injectable through a consistent registration model.


🚀 Convention-Based Registration

You can register all command and query services automatically by scanning assemblies:

services.AddEntityAxisCommandAndQueryServicesFromAssembly<ProductService>();

Or scan multiple assemblies:

services.AddEntityAxisCommandAndQueryServicesFromAssemblies(new[]
{
    typeof(ProductService).Assembly,
    typeof(OtherService).Assembly
});

EntityAxis will discover any class that implements ICommandService<,> or IQueryService<,> (even via a custom interface) and register all necessary interfaces for you.


💡 Things to Know

  • These registration methods typically belong in your Infrastructure layer.
  • EntityAxis uses TryAdd to avoid overwriting existing service registrations.
  • Only generic interface types like ICommandService<TEntity, TKey> are used internally by the library — ensure they're registered, even when using custom abstractions.

See Also

Clone this wiki locally