Skip to content

Entity Registration

Casey edited this page Apr 6, 2025 · 7 revisions

Entity Registration

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 supports services that implement custom interfaces like this:

public interface IProductCommandService : ICommandService<Product, Guid> { }

As long as your custom interface inherits from the generic interface (ICommandService<TEntity, TKey> or IQueryService<TEntity, TKey>), it will be properly discovered and registered.


πŸš€ 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