An open API service indexing awesome lists of open source software.

https://github.com/angelodotnet/efcoregeneric


https://github.com/angelodotnet/efcoregeneric

hacktoberfest hacktoberfest-accepted

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

# Entity Framework Core Generic

If you like this repository, please drop a :star: on Github!

## Installation

The library is available on [NuGet](https://www.nuget.org/packages/EFCoreGeneric) or run the following command in the .NET CLI:

```bash
dotnet add package EFCoreGeneric
```

## Example entity

In this example, the entity is a data model that implements the *IEntity* interface and has a primary key of type *int*.

```csharp
namespace MyNet6Project;

public class MyEntity : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
```

## Example interface

In this example, the interface is a service that implements the *IMyService* interface.

```csharp
namespace MyNet6Project;

public interface IMyService
{
Task> GetListItemAsync();
Task GetItemAsync(int id);
Task CreateItemAsync(MyEntity item);
Task UpdateItemAsync(MyEntity item);
Task DeleteItemAsync(MyEntity item);
}
```

## Example class

In this example, the class is a service that implements the *IMyService* interface.

```csharp
namespace MyNet6Project;

public class MyService : IMyService
{
private readonly IUnitOfWork unitOfWork;

public MyService(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}

public async Task> GetListItemAsync()
{
var listItem = await unitOfWork.ReadOnly.GetAllAsync();
return listItem;
}

public async Task GetItemAsync(int id)
{
var item = await unitOfWork.ReadOnly.GetByIdAsync(id);
return item;
}

public async Task CreateItemAsync(MyEntity item)
{
await unitOfWork.Command.CreateAsync(item);
}

public async Task UpdateItemAsync(MyEntity item)
{
await unitOfWork.Command.UpdateAsync(item);
}

public async Task DeleteItemAsync(MyEntity item)
{
await unitOfWork.Command.DeleteAsync(item);
}
}
```

## Registering services at Startup

```csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped();
services.AddScoped(typeof(IUnitOfWork<,>), typeof(UnitOfWork<,>));
services.AddScoped(typeof(IDatabaseRepository<,>), typeof(DatabaseRepository<,>));
services.AddScoped(typeof(ICommandRepository<,>), typeof(CommandRepository<,>));
}
```

## Contributing

Contributions and/or suggestions are always welcome.

## Badges

[![Publish](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/publish.yml/badge.svg?branch=main)](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/publish.yml)

[![Lint Code Base](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/linter.yml/badge.svg?branch=main)](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/linter.yml)

[![CodeQL](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/codeql.yml/badge.svg?branch=main)](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/codeql.yml)