https://github.com/angelodotnet/efcoregeneric
https://github.com/angelodotnet/efcoregeneric
hacktoberfest hacktoberfest-accepted
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/angelodotnet/efcoregeneric
- Owner: AngeloDotNet
- License: mit
- Created: 2023-02-23T17:57:12.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-21T14:02:05.000Z (over 3 years ago)
- Last Synced: 2025-03-15T08:55:47.776Z (over 1 year ago)
- Topics: hacktoberfest, hacktoberfest-accepted
- Language: C#
- Homepage:
- Size: 28.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
[](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/publish.yml)
[](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/linter.yml)
[](https://github.com/AngeloDotNet/EFCoreGenericLibraryV3/actions/workflows/codeql.yml)