RoyalCode.OperationHint.Abstractions 1.0.0

dotnet add package RoyalCode.OperationHint.Abstractions --version 1.0.0                
NuGet\Install-Package RoyalCode.OperationHint.Abstractions -Version 1.0.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="RoyalCode.OperationHint.Abstractions" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RoyalCode.OperationHint.Abstractions --version 1.0.0                
#r "nuget: RoyalCode.OperationHint.Abstractions, 1.0.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 RoyalCode.OperationHint.Abstractions as a Cake Addin
#addin nuget:?package=RoyalCode.OperationHint.Abstractions&version=1.0.0

// Install RoyalCode.OperationHint.Abstractions as a Cake Tool
#tool nuget:?package=RoyalCode.OperationHint.Abstractions&version=1.0.0                

OperationHint

Operation Hint is an application layer pattern for informing the persistence layer in a decoupled way about the current operation to be performed.

This library offers a flexible solution for adding hints to the data repositories in your application, inspired by EFCore's Query Hints.

These hints can be easily applied to query operations to modify the repository's behavior, giving you greater control over data access.

The most basic use would be to apply the Include to the queries according to the needs of the operation. With Operation Hint, the application layer doesn't need to understand how relationships work or the technology of the persistence layer.

Operation Hint should be used with any repository and/or unit of work implementation. The repository or unit of work interfaces should have the option of informing the operation using an enum. In the implementations, the library components can be called to apply the modifications to the queries.

NuGet Packages

dotnet add package RoyalCode.OperationHint.Abstractions
dotnet add package RoyalCode.OperationHint.EntityFramework

Examples of implementation and use

Let's take two interfaces as an example, one for a repository and one for a work unit:

public interface IRepository<T>
    where T : class
{
    public T? Find(object id);

    public IQueryable<T> Query();
}
public interface IUnitOfWork
{
    public void AddHint(Enum hint);

    public void SaveChanges();
}

This implementation is very brief, just to illustrate the use of this library's components.

Now let's look at the implementation of the work unit:

using Microsoft.EntityFrameworkCore;
using RoyalCode.OperationHint.Abstractions;

public class DefaultUnitOfWork<TDb> : IUnitOfWork
    where TDb : DbContext
{
    private readonly TDb db;
    private readonly IHintsContainer hintsContainer;

    public DefaultUnitOfWork(TDb db, IHintsContainer hintsContainer)
    {
        this.db = db;
        this.hintsContainer = hintsContainer;
    }

    public void AddHint(Enum hint)
    {
        hintsContainer.AddHint(hint);
    }

    public void SaveChanges()
    {
        db.SaveChanges();
    }
}

And the implementation of the repository:

using Microsoft.EntityFrameworkCore;
using RoyalCode.OperationHint.Abstractions;

public class DefaultRepository<TEntity, TDb> : IRepository<TEntity>
    where TDb : DbContext
    where TEntity : class
{
    private readonly TDb db;
    private readonly IHintPerformer hintPerformer;

    public DefaultRepository(TDb db, IHintPerformer hintPerformer)
    {
        this.db = db;
        this.hintPerformer = hintPerformer;
    }

    public TEntity? Find(object id)
    {
        var entity = db.Set<TEntity>().Find(id);
        if (entity is not null)
            hintPerformer.Perform<TEntity, DbContext>(entity, db);
        return entity;
    }

    public IQueryable<TEntity> Query()
    {
        IQueryable<TEntity> query = db.Set<TEntity>();
        query = hintPerformer.Perform(query);
        return query;
    }
}

Finally, you need to configure the hints handler. This can be done via extension methods for the IServiceCollection.

To show an example, I'll take a part of the implementation of the unit tests, see below:

services.ConfigureOperationHints(registry =>
{
    registry.AddIncludesHandler<ComplexEntity, TestHints>((hint, includes) =>
    {
        switch (hint)
        {
            case TestHints.TestSingleRelation:
                includes.IncludeReference(e => e.SingleRelation);
                break;
            case TestHints.TestMultipleRelation:
                includes.IncludeCollection(e => e.MultipleRelation);
                break;
            case TestHints.TestAllRelations:
                includes
                    .IncludeReference(e => e.SingleRelation)
                    .IncludeCollection(e => e.MultipleRelation);
                break;
        }
    });
});

Explanations:

  • services is an object of IServiceCollection.
  • ConfigureOperationHints is the extension method for configuring hint handlers.
  • registry is an object of IHintHandlerRegistry, where hints handlers are registered.
  • AddIncludesHandler<TEntity, THint> is a method for registering a hint handler. The generic types are the entity to be handled and the type of the hint to be entered.
  • (hint, includes) are the parameters of the function that handles the hint. hint is the value of the enum (for example, entered in the unit of work). includes is an object of Includes<TEntity>, specifically for adding Include to EFCore queries.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 is compatible.  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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on RoyalCode.OperationHint.Abstractions:

Package Downloads
RoyalCode.Searches.Persistence.EntityFramework

Persistence components implementation with EntityFrameworkCore, including the handling of Searchable, Filters and Specifiers.

RoyalCode.EntityFramework.OperationHint

Persistence components implementation with EntityFrameworkCore, for work with Operation Hint.

RoyalCode.OperationHint.EntityFramework

Persistence components implementation with EntityFrameworkCore, for work with Operation Hint.

RoyalCode.Searches.EntityFramework

Persistence components implementation with EntityFrameworkCore, including the handling of Searchable, Filters and Specifiers.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 225 3/13/2024
1.0.0-preview-4.0 168 11/25/2023