VersaTul.Data.EFCore 1.0.19

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package VersaTul.Data.EFCore --version 1.0.19
NuGet\Install-Package VersaTul.Data.EFCore -Version 1.0.19
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="VersaTul.Data.EFCore" Version="1.0.19" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VersaTul.Data.EFCore --version 1.0.19
#r "nuget: VersaTul.Data.EFCore, 1.0.19"
#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 VersaTul.Data.EFCore as a Cake Addin
#addin nuget:?package=VersaTul.Data.EFCore&version=1.0.19

// Install VersaTul.Data.EFCore as a Cake Tool
#tool nuget:?package=VersaTul.Data.EFCore&version=1.0.19

VersaTul EFCore

VersaTul EFCore is a project that provides the ability to quickly create project specific database repositories running on Microsoft Entity Framework Core ORM. It provides generic repository functionality that can be reused to create project specific repositories. CRUD operations are defined both Synchronous and Asynchronous methods.

Features

  • Supports absolute unit of work with IUnitOfWork interface
  • Provides common functionality for CRUD operations with BaseRepository<TEntity, TKey> abstract class
  • Allows easy configuration of the connection string with DataConfiguration class
  • Supports both int and string as primary key types for entities
  • Enables project specific methods to be added to repositories

Installation

To install VersaTul EFCore, run the following command in the Package Manager Console:

PM> NuGet\Install-Package VersaTul.EFCore -Version latest

Usage

To use VersaTul EFCore, follow these steps:

  • Create a data model class that represents your entity, for example:
//DataModel
public class PlayerData
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime CreatedDate { get; set; }
}
  • Create a DbContext class that inherits from DbContext and configure the connection string and the DbSet properties, for example:
//Create DbContext class which inherits from DbContext
public class DatabaseContext : DbContext
{
    // IDataConnection - provides the means by which the connection string
    // can be obtained from Configuration settings.
    private readonly IDataConnection dataConnection;

    public DatabaseContext(IDataConnection dataConnection)
    {
        this.dataConnection = dataConnection;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            // using the GetConnectionString method to get the connection string at runtime.
            optionsBuilder.UseSqlServer(dataConnection.GetConnectionString());
        }
    }

    public DbSet<PlayerData> Players { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PlayerData>().HasKey(x => x.Id);
    }
}
  • Create a base repository interface that inherits from IRepository<TEntity, TKey> and specify the primary key type, for example:
// Create a base repository interface that inherits IRepository<TEntity, TKey>
// this will ensure all CRUD functionality is supported by every repository that inherits
// from this interface.
// This technique is optional, am simply specifying the TKey value here as ``int`` so as to
// reduce complexity for project specific repositories.
public interface IRepository<TEntity> : IRepository<TEntity, int>
    where TEntity : class, new()
{
}
  • Create a project specific repository interface that inherits from the base repository interface, for example:
// Create project specific repository from IRepository<TEntity> interface.
public interface IPlayerRepository : IRepository<PlayerData>
{
    //project specific methods can be added here
}
  • Create a base repository class that inherits from BaseRepository<TEntity, TKey> and specify the primary key type and the DbContext type, for example:
// Create BaseRepository that inherits from BaseRepository<TEnity, TKey>
// this will ensure all CRUD functionality is supported by every repository that inherits
// from this base. Also specifying the TKey value as ``int`` to reduce complexity.
// project specific DbContext should also be exposed from this class.
public abstract class BaseRepository<TEnity> : BaseRepository<TEnity, int>
    where TEnity : class, new()
{
    public BaseRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
    {
        if(unitOfWork == null)
            throw new ArgumentNullException(nameof(unitOfWork));

        DbSet = unitOfWork.DataContext.Set<TEnity>();
    }

    protected DbSet<TEnity> DbSet { get; }

    protected DatabaseContext DatabaseContext => DataContext as DatabaseContext;
}
  • Create a project specific repository class that inherits from the base repository class and the project specific repository interface, for example:
// Create project specific repository
public class PlayerRepository : BaseRepository<PlayerData>, IPlayerRepository
{
    public PlayerRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
    {
    }
}
  • Create a unit of work class that inherits from BaseUnitOfWork and specify the DbContext type, for example:
// Create project specific UnitOfWork
public class UnitOfWork : BaseUnitOfWork
{
    public UnitOfWork(DatabaseContext dataContext) : base(dataContext)
    {
    }
}
  • Configure the container using AutoFac Module, for example:
// Configure the container using AutoFac Module
public class AppModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        //Configs
        var configSettings = new Builder()
            .AddOrReplace("DBCon", "Server=127.0.0.1;Database=DemoDb;User Id=sa;Password=password@123;Persist Security Info=True;")
            .BuildConfig();

        builder.RegisterInstance(configSettings);

        //Singletons
        builder.RegisterType<DataConfiguration>().As<IDataConnection>().SingleInstance();

        //Per Dependency
        builder.RegisterType<DatabaseContext>().AsSelf().InstancePerLifetimeScope();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().As<VersaTul.Data.EFCore.Contracts.IUnitOfWork>().InstancePerLifetimeScope();
        builder.RegisterType<PlayerRepository>().As<IPlayerRepository>().InstancePerLifetimeScope();
    }
}
  • Use the repository in your controller or service, for example:
// Repository usage could look like the following:
[Route ("api/players")]
public class PlayerController: Controller
{
    private readonly IPlayerRepository playerRepository;

    public PlayerController(IPlayerRepository playerRepository)
    {
        this.playerRepository = playerRepository;
    }

    // Get
    [HttpGet]
    public IActionResult GetPlayers()
    {
        var players = playerRepository.Get();
        return OK(players);
    }

    [HttpGet (" {id}")]
    public IActionResult GetPlayer(int id)
    {
        var player = playerRepository.Get(id);
        if(player == null)
            return NotFound();

        return OK(player);
    }

    [HttpPost]
    public IActionResult CreatePlayer(CreatePlayerModel model)
    {
        var player = playerRepository.Add(new PlayerData
        {
            Name = model.Name,
            FirstName = model.FirstName,
            LastName = model.LastName
        });

        return OK(player);
    }
}

License

VersaTul EFCore is licensed under the MIT License. See the LICENSE file for details.

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. 
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.19 89 4/5/2024
1.0.18 79 4/4/2024
1.0.17 81 4/4/2024
1.0.16 89 3/1/2024
1.0.15 84 2/2/2024
1.0.14 74 2/1/2024
1.0.13 76 1/20/2024
1.0.12 80 1/15/2024
1.0.11 162 11/4/2023
1.0.10 157 7/22/2023