EfLight 1.0.4
See the version list below for details.
dotnet add package EfLight --version 1.0.4
NuGet\Install-Package EfLight -Version 1.0.4
<PackageReference Include="EfLight" Version="1.0.4" />
<PackageVersion Include="EfLight" Version="1.0.4" />
<PackageReference Include="EfLight" />
paket add EfLight --version 1.0.4
#r "nuget: EfLight, 1.0.4"
#:package EfLight@1.0.4
#addin nuget:?package=EfLight&version=1.0.4
#tool nuget:?package=EfLight&version=1.0.4
EfLight 🪶
Goal
EfLight's goal is to provide a simple API to use EF Core in order to avoid the boilerplate code required for most basic operations with EF Core.
It provides two base classes:
CrudRepository : which as its name implies, provides most basic operations for CRUD.
PagingAndSortingRepository: it extends CrudRepository and provides pagination features on top of existing features of CrudRepository.
Example
Suppose that we have an entity class named Student and our DbContext is already configured.
[Table("Student")]
class Student
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }
}
To interact with the table, just create a repository class which extends whether CrudRepository or PagingAndSortingRepository (the choice depends on your requirements) with its corresponding interface that will be injected in other classes.
public interface IStudentRepository : ICrudRepository<Student, int>
{
}
class StudentRepository : CrudRepository<Student, int>, IStudentRepository
{
public StudentRepository(YourDbContext context) : base(context)
{
}
}
Then, you can register your repository class in the IoC container in order to use it from anywhere.
builder.Services.AddScoped<IStudentRepository, StudentRepository>();
All you have to do is to inject it where it is needed. For example:
public class StudentService
{
private readonly IStudentRepository _repository;
public StudentService(IStudentRepository repository)
{
_repository = repository;
}
public void CheckIfAllAboveAge(int age)
{
bool result = _repository.ExistsWhere(x => x.BirthData.Year > age);
// Your business logic
}
public Student? FindByName(string name)
{
Student? student;
Optional<Student> result = _repository.FindWhere(x => x.LastName == name);
// Throws the exception if the result is null
student = result.OrElseThrow(() => new Exception());
// Instead of throwing an exception, you can also do
student = result.IfNullThen(() => {
student = _repository.FindWhere(x => x.FirstName == name);
});
return student;
}
}
Adding your features
You can also provide more features to your repository classes depending on your use cases. Just use an interface defining the contract and provide your own implementation of that use case.
public interface IStudentRepository : ICrudRepository<Student, int>
{
void MakeLastNameNull(int studentId);
}
In the repository class, you'll have:
class StudentRepository : CrudRepository<Student, int>, IStudentRepository
{
public StudentRepository(YourDbContext context) : base(context)
{
}
public void MakeLastNameNull(int studentId)
{
// Your implementation
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. 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 was computed. 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. |
-
net6.0
- Microsoft.EntityFrameworkCore (>= 6.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.