Simple definitions for repository pattern
- Basic CRUD implemented
- Basic Context scope (aka ~UnitOfWork)
GenericRepository,GenericEntityRepositorywith and withoutPrimaryKey- Abstracts implement
IQueryable, example:_customerRepository.Where(...) SecureGuidValueGenerator,DateTimeValueGenerator,ITimestampedEntity- By convention, a property named Id or Id will be configured as the primary key of an entity
1. Install Nuget package F2.Repository
Install-Package F2.RepositoryOr, you can create your own BaseEntity:
public abstract class BaseEntity : IEntity<Guid>, ITimestampedEntity
{
public Guid Id { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}public class Book : BaseEntity
{
public string Title { get; set; }
public string Isbn { get; set; }
public virtual ICollection<Author> Authors { get; set; }
}public class BookEntityConfiguration : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.UseAutoGeneratedId();
builder.UseTimestampedProperty();
builder.HasMany(s => s.Authors).WithMany(s => s.Books);
}
}GenericEntityRepository<TEntityType> or GenericRepository<TEntityType> abstracts
using F2.Repository.Abstracts;
public class CustomerRepository : GenericEntityRepository<Customer>
{
public CustomerRepository(YourDbContext context) : base(context)
{
}
}public class YourDbScope : DbContextScope<YourDbContext>
{
public YourDbScope(YourDbContext context) : base(context)
{
YourRepository = yourRepository;
}
public YourRepository YourRepository { get; }
}All repositories will automatically registered for DI
services.AddDatabaseScope<YourDbContext>();
// ... services.AddDbContext<AutomateContext>(opts => opts.UseSqlServer(_configuration.GetConnectionString("SqlConnection"));Add-Migration -Context LibraryContext -Project F2.Repository.Demo PublisherOneToMany
Add-Migration -Context LibraryContext -Project F2.Repository.Demo PublisherOneToManyNullable