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
<PackageReference Include="Motorbay.EntityFrameworkCore.Abstractions" Version="1.0.3" />
<PackageVersion Include="Motorbay.EntityFrameworkCore.Abstractions" Version="1.0.3" />
<PackageReference Include="Motorbay.EntityFrameworkCore.Abstractions" />
paket add Motorbay.EntityFrameworkCore.Abstractions --version 1.0.3
#r "nuget: Motorbay.EntityFrameworkCore.Abstractions, 1.0.3"
#:package Motorbay.EntityFrameworkCore.Abstractions@1.0.3
#addin nuget:?package=Motorbay.EntityFrameworkCore.Abstractions&version=1.0.3
#tool nuget:?package=Motorbay.EntityFrameworkCore.Abstractions&version=1.0.3
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 | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.6)
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 |