Skip to content

williampaciaroni/illyW-Framework

Repository files navigation

NuGet Version NuGet Version Quality Gate Status Coverage Reliability Rating Security Rating Vulnerabilities

illyW-Framework

illyW-Framework is a .NET framework that provides a set of utilities to facilitate application development by implementing some of the most common and powerful patterns like:

  • Generic Entity
  • Repository
  • Unit of Work
  • Result

This framework aims to reduce boilerplate code by offering generic implementations that can be easily extended or integrated into existing projects.

Installation

You can install illyW-Framework via NuGet. Run the following command in the Package Manager Console:

Install-Package illyW.Framework.Core
Install-Package illyW.Framework.EFCore

Usage

Generic Entity Pattern

The IEntity interface is a base interface which can be implemented to create entities with common identifiers.

public class TestEntity : IEntity<int>
{
    public int Id { get;set; }
    public string Name { get; set; }
}
public class TestEntity2 : IEntity<string>
{
    public string Id { get;set; }
    public string Name { get; set; }
}

Repository Pattern

The IGenericRepository interface provides a set of methods to support CRUD operations.

public interface ITestRepository : IGenericRepository<TestEntity, int>;
public class TestCoreDbContext(DbContextOptions options) : DbContext(options)
{
    public DbSet<TestEntity> TestEntities { get; set; } = null!;
}
public class TestRepository(TestCoreDbContext context)
    : GenericRepository<TestEntity, int, TestCoreDbContext>(context), ITestRepository;
serviceCollection.AddRepository<ITestRepository, TestRepository>();
[ApiController]
[Route("api/[controller]")]
public class TestController(ITestRepository repository) : ControllerBase
{

  [HttpGet]
  public ActionResult GetAll()
  {
      return Ok(repository.Fetch());
  }

}

Unit of Work

The IUnitOfWork interface provides a set of methods to centralize the management of transactional operations, ensuring that changes are applied atomically.

serviceCollection.AddUnitOfWork<TestCoreDbContext>();
[ApiController]
[Route("api/[controller]")]
public class TestController(IUnitOfWork uow) : ControllerBase
{

  [HttpPost]
  public ActionResult Create([FromBody] string name)
  {
      var repository = uow.GetRepository<ITestRepository>();
      var repository2 = uow.GetRepository<ITest2Repository>();

      var r1 = await repository.AddAsync(new TestEntity {Name = name});
      var r2 = await repository2.AddAsync(new TestEntity2 {Name = name});

      if(r1.IsSuccessful && r2.IsSuccesful)
      {
        uow.Commit();
        return Ok();
      }

      return UnprocessableEntity();
  }

}

Result

The IResult is an interface to represent the outcome of an operation, enabling elegant handling of success, failure, and status messages.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages