Skip to content

Instantly share code, notes, and snippets.

View PradeepLoganathan's full-sized avatar

Pradeep Loganathan PradeepLoganathan

View GitHub Profile
[Route("api/[controller]")]
[ApiController]
public class BooksController : ControllerBase
{
private IUnitOfWork _unitOfWork;
public BooksController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
@PradeepLoganathan
PradeepLoganathan / UnitOfWork.cs
Last active October 22, 2020 05:01
UnitOfWork
public class UnitOfWork :IUnitOfWork
{
private readonly BookStoreDbContext _context;
public IBooksRepository Books { get; }
public ICatalogueRepository Catalogues { get; }
public UnitOfWork(BookStoreDbContext bookStoreDbContext,
IBooksRepository booksRepository,
ICatalogueRepository catalogueRepository)
public class BooksRepository:GenericRepository<Book>, IBooksRepository
{
public BooksRepository(BookStoreDbContext context) : base(context)
{
}
public IEnumerable<Book> GetBooksByGenre(string Genre)
{
return _context.Books.Where(x => x.Genre == Genre);
public abstract class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected readonly BookStoreDbContext _context;
public GenericRepository(BookStoreDbContext context)
{
_context = context;
}
public async Task<T> Get(int id)
{
public interface IUnitOfWork : IDisposable
{
IBooksRepository Books { get; }
ICatalogueRepository Catalogues { get; }
int Complete();
}
@PradeepLoganathan
PradeepLoganathan / CatalogueRepository.cs
Last active July 30, 2020 04:13
CatalogueRepository concrete class
using System.Collections.Generic;
using System.Threading.Tasks;
using BookStore.Domain.CatalogueAggregate;
using Microsoft.EntityFrameworkCore;
namespace BookStore.Repository
{
class CatalogueRepository : ICatalogueRepository
{
private readonly BookStoreDbContext _context;
@PradeepLoganathan
PradeepLoganathan / Startup.cs
Created July 30, 2020 03:47
Confgure services
public void ConfigureServices(IServiceCollection services)
{
services.AddRepository();
services.AddControllers();
}
@PradeepLoganathan
PradeepLoganathan / DependencyInjection.cs
Last active July 30, 2020 10:04
inject repositories and DbContext
using BookStore.Domain.BooksAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace BookStore.Repository
{
public static class DependencyInjection
{
public static IServiceCollection AddRepository(this IServiceCollection services)
{
public class BookStoreDbContext : DbContext
{
public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options) : base(options)
{ }
public DbSet<Book> BookItems { get; set; }
}
public interface IBooksRepository:IGenericRepository<Book>
{
}