Skip to content

Insert Operations

Furkan Güngör edited this page Jun 20, 2022 · 5 revisions

Summary

EasyRepository contains two different methods for insert operations.

  • Classes that inherit from the EasyBaseEntity type
  • Custom types

CreationDate field is automatically set in insert operations in classes derived from EasyBaseEntity type.

Insert

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Author> AddAuthorAsync()
    {
        var entity = await _unitOfWork.Repository.AddAsync<Author, Guid>(new Author
        {
            Name = "John",
            Surname = "Doe"
        }, cancellationToken: default);
        await _unitOfWork.Repository.CompleteAsync();
        return entity;
    }
}

or

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Car> AddCarAsync()
    {
        var car = new Car
        {
            Model = "3.15",
            Brand = "BMW"
        }
        var entity = await repositor.AddAsync(car);
        await _unitOfWork.Repository.CompleteAsync();
        return entity
    }
}

Multiple Insert

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Author> AddAuthorsAsync()
    {
        var authors = new List<Author>
        {
           new Author {Name = "John", Surname = "Doe"},
           new Author {Name = "Franz", Surname = "Kafka"}
        }
        var entities = await repositor.AddRangeAsync<Author,Guid>(authors);
        await _unitOfWork.Repository.CompleteAsync();
        return entities;
    }
}

or

public class MyClass
{
    private readonly IUnitOfWork _unitOfWork;
    public MyClass(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    public async Task<Car> AddCarsAsync()
    {
        var cars = new List<Car>
        {
            new Car {Model = "Astra", Brand = "Opel"},
            new Car {Model = "Corsa", Brand = "Opel"}
        }
        var entities = await repositor.AddRangeAsync(cars);
        await _unitOfWork.Repository.CompleteAsync();
        return entities;
    }
}
Clone this wiki locally