https://github.com/swtanggara/intoitif
It's kinda Unit of Work, Repository things, done intuitively in EF AND EF Core.
https://github.com/swtanggara/intoitif
csharp interceptor-service repository-pattern unit-of-work
Last synced: 11 months ago
JSON representation
It's kinda Unit of Work, Repository things, done intuitively in EF AND EF Core.
- Host: GitHub
- URL: https://github.com/swtanggara/intoitif
- Owner: swtanggara
- License: mit
- Created: 2018-10-14T17:32:31.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T01:02:04.000Z (over 3 years ago)
- Last Synced: 2025-07-25T22:50:56.116Z (11 months ago)
- Topics: csharp, interceptor-service, repository-pattern, unit-of-work
- Language: C#
- Homepage: https://swtanggara.github.io/IntoItIf/
- Size: 1.52 MB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
IntoItIf
===============
[](https://ci.appveyor.com/project/swtanggara/intoitif)
[](https://github.com/swtanggara/IntoItIf/releases)
[](https://github.com/swtanggara/IntoItIf/releases/latest)
[](https://github.com/swtanggara/IntoItIf/blob/master/LICENSE)
It's kinda Unit of Work, Repository things, done intuitively in EF **AND** EF Core.
| Project | NuGet | Frameworks | Dependencies | NuGet Downloads |
| ------- | ----- | ---------- | ------------ | --------------- |
| IntoItIf.Dsl | [](https://www.nuget.org/packages/IntoItIf.Dsl/) | net471 and up, netstandard20 and up | IntoItIf.Dal | [](https://www.nuget.org/packages/IntoItIf.Dsl/) |
| IntoItIf.Dal | [](https://www.nuget.org/packages/IntoItIf.Dal/) | net471 and up, netstandard20 and up | IntoItIf.Core | [](https://www.nuget.org/packages/IntoItIf.Dal/) |
| IntoItIf.Core | [](https://www.nuget.org/packages/IntoItIf.Core/) | net471 and up, netstandard20 and up | None | [](https://www.nuget.org/packages/IntoItIf.Core/) |
| IntoItIf.Dsl.AutoMapper | [](https://www.nuget.org/packages/IntoItIf.Dsl.AutoMapper/) | net471 and up, netstandard20 and up | IntoItIf.Dsl | [](https://www.nuget.org/packages/IntoItIf.Dsl.AutoMapper/) |
| IntoItIf.Dsl.BatMap | [](https://www.nuget.org/packages/IntoItIf.Dsl.BatMap/) | net471 and up, netstandard20 and up | IntoItIf.Dsl | [](https://www.nuget.org/packages/IntoItIf.Dsl.BatMap/) |
| IntoItIf.Dsl.Mapster | [](https://www.nuget.org/packages/IntoItIf.Dsl.Mapster/) | net471 and up, netstandard20 and up | IntoItIf.Dsl | [](https://www.nuget.org/packages/IntoItIf.Dsl.Mapster/) |
### Setting it Up
You must setting up your `DbContext` class first, either by inheriting `EfCoreDbContext` (EF Core) or `EfDbContext` (EF):
```c#
public class MyDbContext : EfCoreDbContext // Inherit from EfDbContext if you are using EF6 or above
{
public DbSet Entities { get; set; }
}
```
where `MyEntity` must inherit from `IEntity` or from base templating class `BaseEntity`:
```c#
public class MyEntity : BaseEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
```
next, define your `MyDto` class for mapping from `MyEntity`. This `MyDto` class must inherit from `IDto`, or better from `BaseDto` class:
```c#
public class MyDto: BaseDto
{
public int Id { get; set; }
public string Name { get; set; }
}
```
don't forget to define you `MyDto` validator class, by inheriting `BaseFluentValidator` (using [`FluentValidator`](https://github.com/JeremySkinner/FluentValidation)) or `BaseValitValidator` (using [`Valit`](https://github.com/valit-stack/Valit)):
```c#
public class MyDtoFluentValidator: BaseFluentValidator
{
public MyDtoFluentValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
}
}
```
(`BaseValitValidator` version):
```c#
public class MyDtoValitValidator : BaseValitValidator
{
public MyDtoValitValitator()
{
Valitator = ValitRules.Create()
.Ensure(x => x.Id, x => x.IsNonZero())
.Ensure(x => x.Name, x => x.Required())
.CreateValitator();
}
protected override IValitator Valitator { get; }
}
```
next, create you `IMapperProfile` derived class to maps `MyEntity` to `MyDto` and vice-versa:
```c#
public class MyMapperProfile : IMapperProfile
{
public Option<(Type Source, Type Destination)>[] GetBinds()
{
return new Option<(Type Source, Type Destination)>[]
{
(typeof(MyEntity), typeof(MyDto)),
(typeof(MyDto), typeof(MyEntity)),
};
}
}
```
And, lastly, at your *startup* class, inject the `IMapperService` like so:
```c#
// Use AutoMapper if you want to support Value-Object pattern
var mapperSvc = new AutoMapperService(); // Choose between AutoMapperService, BatMapMapperService, or MapsterMapperService
mapperSvc.Initialize(new MyMapperProfile());
DslInjecterGetter.SetBaseMapperService(mapperSvc);
var uow = new EfCoreUnitOfWork(new MyDbContext()); // Or use EfUnitOfWork, if you are using EF6 or above.
DslInjecterGetter.SetBaseUnitOfWork(uow);
```
### Usage
It's quite daunting to setting it up huh? But wait, this is how you can utilize my charming library:
```c#
Option ctok = CancellationToken.None;
var dto = new MyDto();
var createResult = Create.Handle(dto, ctok);
var deleteResult = Delete.Handle(dto, ctok);
var readLookupResult = ReadLookup.Handle(false, ctok);
var readOneResult = ReadOne.Handle(dto, ctok);
var readPagedResult = ReadPaged.Handle(1, 1, null, "Bla", ctok);
var updateResult = Update.Handle(dto, ctok);
```
And if you need transactional DB processing, you would do it like this:
```c#
using (var trx = uow.GetDbTransaction())
{
try
{
var createResult = Create.Handle(dto, ctok);
var deleteResult = Delete.Handle(dto, ctok);
var readLookupResult = ReadLookup.Handle(false, ctok);
var readOneResult = ReadOne.Handle(dto, ctok);
var readPagedResult = ReadPaged.Handle(1, 1, null, "Bla", ctok);
var updateResult = Update.Handle(dto, ctok);
trx.Commit();
}
catch (Exception ex)
{
trx.Rollback();
}
}
```
Yes, of course, you will ask: what `MyCreateInterceptor`, `MyDeleteInterceptor`, `MyReadLookupInterceptor`,
`MyReadOneInterceptor`, `MyReadOneInterceptor`, `MyReadPagedInterceptor`, and `MyUpdateInterceptor` are all about. It's your task to find what they are....