我正在关注this example:
实体
[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)
{
}
}
通用存储库
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;
}
}
}
}
问题
如果我需要自定义添加功能怎么办? 如果我在Book类中添加Code属性:
[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; }
}
我希望在将DB对象插入数据库之前自动填充Code属性。 我想我需要创建一个“自定义” BookRepository ,它继承 GenericRepository 并覆盖添加功能,以便有类似的东西:< / p>
public void Add(Book entity)
{
entity.Code = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds+entity.Title.Replace(" ","");
_dbSet.Add(entity);
}
我不熟悉设计模式/继承/接口概念。有可能做那样的事吗?
答案 0 :(得分:0)
您要添加的逻辑属于模型而不是存储库,存储库与此无关。负责的存储库是访问数据库并对数据库运行查询。
我写了一篇文章,介绍如何使用EF计划Thin存储库和工作单元
我将在下面插入链接 https://www.codeproject.com/Articles/1157241/Very-Thin-Database-Layer-using-UnitOfWork-Pattern
希望这会有所帮助