EfLight 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package EfLight --version 1.0.4
                    
NuGet\Install-Package EfLight -Version 1.0.4
                    
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="EfLight" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EfLight" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="EfLight" />
                    
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 EfLight --version 1.0.4
                    
#r "nuget: EfLight, 1.0.4"
                    
#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 EfLight@1.0.4
                    
#: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=EfLight&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=EfLight&version=1.0.4
                    
Install as a Cake Tool

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 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. 
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.3.0 144 7/28/2024
1.2.2 134 7/28/2024
1.2.1 123 7/28/2024
1.1.0 240 7/23/2023
1.0.5 260 3/29/2023
1.0.4 303 2/1/2023
1.0.3 354 1/6/2023