dimanche 3 juin 2018

UnitOfWork + Repository pattern - Adding a custom repository

I'm following this example :

Entities

[Table("Authors")]
public class Author {
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}
[Table("Books")]
public class Book {
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }
    public int Author_ID { get; set; } 

    [ForeignKey("Author_ID")] 
    public virtual Author Author { get; set; }
}

DbContext

public class MyDbContext : DbContext
{
    public virtual DbSet<Author> Authors { get; set; }
    public virtual DbSet<Book> Books { get; set; }
    public MyDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }
}

Generic repository

public interface IRepository<T> where T : class
{
   IQueryable<T> Entities { get; }
   void Remove(T entity);
   void Add(T entity);
}
public class GenericRepository<T> : IRepository<T> where T : class
{
    private readonly MyDbContext _dbContext;
    private IDbSet<T> _dbSet => _dbContext.Set<T>();
    public IQueryable<T> Entities => _dbSet;
    public GenericRepository(MyDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public void Remove(T entity)
    {
        _dbSet.Remove(entity);
    }
    public void Add(T entity)
    {
        _dbSet.Add(entity);
    }
}

UnitOfWork

public interface IUnitOfWork
{
    IRepository<Author> AuthorRepository { get; }
    IRepository<Book> BookRepository { get; }

    /// <summary>
    /// Commits all changes
    /// </summary>
    void Commit();
    /// <summary>
    /// Discards all changes that has not been commited
    /// </summary>
    void RejectChanges();
    void Dispose();
}
public class UnitOfWork : IUnitOfWork
{
    private readonly MyDbContext _dbContext;
    #region Repositories
    public IRepository<Author> AuthorRepository => 
       new GenericRepository<Author>(_dbContext);
    public IRepository<Book> BookRepository => 
       new GenericRepository<Book>(_dbContext);
    #endregion
    public UnitOfWork(MyDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    public void Commit()
    {
        _dbContext.SaveChanges();
    }
    public void Dispose()
    {
        _dbContext.Dispose();
    }
    public void RejectChanges()
    {
        foreach (var entry in _dbContext.ChangeTracker.Entries()
              .Where(e => e.State != EntityState.Unchanged))
        {
            switch (entry.State)
            {
                case EntityState.Added:
                    entry.State = EntityState.Detached;
                    break;
                case EntityState.Modified:
                case EntityState.Deleted:
                    entry.Reload();
                    break;
            }
        }
    }
}

Question

What if I need a custom Add function? If I add a Code property in my Book class :

[Table("Books")]
public class Book {
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }
    public int Author_ID { get; set; } 
    public string Code { get; set; } //I'm adding a Code property here

    [ForeignKey("Author_ID")] 
    public virtual Author Author { get; set; }
}

and I want to autofill the Code property before insert the Book object in DB. I guess I need to create a "custom" BookRepository which inherit the GenericRepository and override the Add function in order to have something like that :

public void Add(Book entity)
    {
        entity.Code = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds+entity.Title.Replace(" ","");
        _dbSet.Add(entity);
    }

I'm not familiar with design pattern / inheritance / interface concepts. Is it possible to do something like that?

Aucun commentaire:

Enregistrer un commentaire