Motorbay.EntityFrameworkCore.Abstractions 1.0.3

Prefix Reserved
dotnet add package Motorbay.EntityFrameworkCore.Abstractions --version 1.0.3
                    
NuGet\Install-Package Motorbay.EntityFrameworkCore.Abstractions -Version 1.0.3
                    
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="Motorbay.EntityFrameworkCore.Abstractions" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Motorbay.EntityFrameworkCore.Abstractions" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Motorbay.EntityFrameworkCore.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Motorbay.EntityFrameworkCore.Abstractions --version 1.0.3
                    
#r "nuget: Motorbay.EntityFrameworkCore.Abstractions, 1.0.3"
                    
#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.
#:package Motorbay.EntityFrameworkCore.Abstractions@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Motorbay.EntityFrameworkCore.Abstractions&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Motorbay.EntityFrameworkCore.Abstractions&version=1.0.3
                    
Install as a Cake Tool

Motorbay.EntityFrameworkCore.Abstractions

This library provides a set of Entity Framework Core abstractions for the unit of work pattern and other commonly used features.

Installation

Package Manager Console

Install-Package Motorbay.EntityFrameworkCore.Abstractions

Usage

Repository Example

Model
public class MyEntity
	: IUniqueEntity<long>
{
	public long Id { get; set; }
	public string Name { get; set; }
}
Extend RepositoryErrorDescriptor (OPTIONAL)

You can extend the RepositoryErrorDescriptor class to add custom error codes or override the existing errors.

public class MyRepositoryErrorDescriptor
	: RepositoryErrorDescriptor
{
	public RepositoryError EntityWithNameNotFound<TKey, TEntity>(string name)
        where TKey : IEquatable<TKey>
        where TEntity : class, IUniqueEntity<TKey>
    {
        return new RepositoryError(
            nameof(EntityWithNameNotFound), 
			$"Entity of type '{typeof(TEntity).Name}' with name '{name}' was not found."
        );
    }

	// Add more custom error codes here...
}

This can be passed into the constructor of the DatabaseRepository<TKey, TEntity, TErrorDescriptor> or ReadOnlyDatabaseRepository<TKey, TEntity, TErrorDescriptor> class or registered as a singleton in the DI container.

Repository

You can create a repository by inheriting from DatabaseRepository<TEntity, TKey> or ReadOnlyDatabaseRepository<TEntity, TKey> or manually implementing IRepository<TEntity, TKey> or IReadOnlyRepository<TEntity, TKey>.

public class MyEntityRepository
	: DatabaseRepository<MyEntity, long, MyRepositoryErrorDescriptor>, IMyEntityRepository
{
	public MyEntityRepository(MyDbContext context, MyRepositoryErrorDescriptor errorDescriptor)
		: base(context, errorDescriptor)
	{
	}

	public async Task<RepositoryResult<MyEntity>> GetByNameAsync(string name, bool isTracked, CancellationToken cancellationToken = default)
	{
		MyEntity? entity = await GetQuerable(isTracked).FirstOrDefaultAsync(x => x.Name == name, cancellationToken);

		return entity == null
			? RepositoryResult<MyEntity>.Failure(ErrorDescriptor.EntityWithNameNotFound<long, MyEntity>(name))
			: RepositoryResult<MyEntity>.Success(entity);
	}

	public Task<RepositoryResult<MyEntity>> GetByNameAsync(string name, CancellationToken cancellationToken = default)
	{
		return GetByNameAsync(name, isTracked: false, cancellationToken); // Default to no tracking
	}

	// Add more custom methods here...
}

public interface IMyEntityRepository
	: IRepository<MyEntity, long>
{
	// This is the recommended way to add custom methods to your repository
	Task<RepositoryResult<MyEntity>> GetByNameAsync(string name, bool isTracked, CancellationToken cancellationToken = default);
	Task<RepositoryResult<MyEntity>> GetByNameAsync(string name, CancellationToken cancellationToken = default);

	// Add more custom methods here...
}
IHostApplicationBuilder Dependency Injection

Add this to your Program.cs file.

builder.Services.AddDbContext<MyDbContext>(options =>
{
	// Configure your database here
});

builder.Services.AddScoped<IMyEntityRepository, MyEntityRepository>();

// OPTIONAL: Register the custom error descriptor
builder.Services.AddSingleton<MyRepositoryErrorDescriptor>();

TimestampedEntity Example

Model
public class MyTimestampedEntity
	: ITimestampedEntity
{
	public DateTimeOffset CreatedAt { get; set; }
	public DateTimeOffset UpdatedAt { get; set; }
}
DbContext
public class MyDbContext
	: TimestampedDbContext
{
	public DbSet<MyTimestampedEntity> MyTimestampedEntities { get; set; }

	public MyDbContext(DbContextOptions<MyDbContext> options)
		: base(options)
	{
	}

	protected override void OnModelCreating(ModelBuilder modelBuilder)
	{
		modelBuilder.Entity<MyTimestampedEntity>(entity =>
		{
			entity.Property(x => x.CreatedAt).IsRequired();
			entity.Property(x => x.UpdatedAt).IsRequired();
		});
	}

	protected override void UpdateTimestamps(DateTimeOffset date)
	{
		// OPTIONAL: Override the default update behavior.
	}
}
Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
1.0.3 213 6/18/2024
1.0.2 182 5/28/2024
1.0.1 188 5/27/2024
1.0.0 199 4/5/2024
1.0.0-rc.1 117 3/30/2024
1.0.0-beta.3 109 3/28/2024
1.0.0-beta.2 119 3/26/2024
1.0.0-beta.1 121 3/23/2024