-
Notifications
You must be signed in to change notification settings - Fork 1
Repositories
Orbis Alonzo Gutierrez edited this page Aug 23, 2023
·
1 revision
Is a common pattern used in the development of applications, which allows us to abstract the data access layer, in this way we can change the data source without affecting the rest of the application.
methods
-
GetPaginatedListAsync: Get a list paginated from the database -
GetListAsync: Get a list not paginated -
GetAll:Get a IQueryable data from entity -
CreateAsync: Create a new entity in the db -
UpdateAsync: Update a Entity -
GetByIdAsync:Get a entity -
SoftRemoveAsync: Update the entity and set IsDeleted equals to true -
RemoveAsync:Delete a entity -
ExistAsync: Verify if the entity exist -
CountAsync: Count the entity in the db
how to use it?
- Create a interface and Class as service and use the inheritance of the base repository, passing a model as a generic parameter when the model is a class that inherits from the base model.
BaseModelorBaseBasicModel.
//database context inheritance
public class ApplicationDbContext : FoundationKitDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Person> Persons { get; set; }
}
//database context with identity inheritance
public class ApplicationIdentityDbContext : FoundationKitIdentityDbContext<User>
{
public ApplicationIdentityDbContext(DbContextOptions<ApplicationIdentityDbContext> options) : base(options)
{
}
public DbSet<Person> Persons { get; set; }
}
//using base model
public class Person : BaseModel
{
public string? Name { get; set; }
[NotMapped]
public override string? CreatedBy { get; set; }
}
// inheritance base repository
public interface IPersonService
: IBaseRepository<Person>
{
}
// inheritance base repository and implementation of base repository
public class PersonService : BaseRepository<ApplicationDbContext, Person>, IPersonService
{
public PersonService(ApplicationDbContext context) : base(context)
{
}
}
//usage in a minimal api endpoint
app.MapPost("/api/person", async ([FromServices] IPersonService service,
Person person,
CancellationToken cancellationToken) =>
{
return await service.Create(person, cancellationToken);
})
.WithName("add");Is very identical to BaseRepository
methods
-
GetPaginatedList: Get a list paginated from the database -
GetList: Get a list not paginated -
GetAll:Get a IQueryable data from entity -
Create: Create a new entity in the db -
Update: Update a Entity -
GetById:Get a entity -
SoftRemove: Update the entity and set IsDeleted equals to true -
Remove:Delete a entity -
Exist: Verify if the entity exist -
Count: Count the entity in the db
//database context inheritance
public class ApplicationDbContext : FoundationKitDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Person> Persons { get; set; }
}
//database context with identity inheritance
public class ApplicationIdentityDbContext : FoundationKitIdentityDbContext<User>
{
public ApplicationIdentityDbContext(DbContextOptions<ApplicationIdentityDbContext> options) : base(options)
{
}
public DbSet<Person> Persons { get; set; }
}
//models
public class Person : BaseBasicModel
{
public string? Name { get; set; }
}
public class PersonDto : BaseOutput
{
public string? Name { get; set; }
}
public class PersonEdit : BaseEdit
{
public string? Name { get; set; }
}
public class PersonInput : BaseInput
{
public string? Name { get; set; }
}
public interface IPersonMapService : IMapRepository<PersonInput, PersonEdit, PersonDto>
{
}
public class PersonMapService
: MapRepository<ApplicationIdentityDbContext, Person, PersonInput, PersonEdit, PersonDto>,
IPersonMapService
{
public PersonMapService(ApplicationIdentityDbContext context, IMapper mapper) : base(context, mapper)
{
}
}
//Ready you want access this methods
public class PeopleController : Controller
{
public PeopleController(IPersonMapService service)
{
}
//... you code here
}