public interface IGenericRepository<TEntity>
{
// ....
}
public class GenericRepository : GenericRepository<TEntity>
{
// ....
}
/// <summary>
///
/// </summary>
/// <seealso cref="Microsoft.EntityFrameworkCore.DbContext" />
public partial class BloggingContext : DbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="BloggingContext"/> class.
/// </summary>
public BloggingContext()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BloggingContext"/> class.
/// </summary>
/// <param name="options">The options.</param>
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{
}
/// <summary>
/// blog.
/// </summary>
public virtual DbSet<Blog> Blog { get; set; }
/// <summary>
/// post.
/// </summary>
public virtual DbSet<Post> Post { get; set; }
/// <summary>
/// <para>
/// Override this method to configure the database (and other options) to be used for this context.
/// This method is called for each instance of the context that is created.
/// The base implementation does nothing.
/// </para>
/// <para>
/// In situations where an instance of <see cref="T:Microsoft.EntityFrameworkCore.DbContextOptions" /> may or may not have been passed
/// to the constructor, you can use <see cref="P:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.IsConfigured" /> to determine if
/// the options have already been set, and skip some or all of the logic in
/// <see cref="M:Microsoft.EntityFrameworkCore.DbContext.OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder)" />.
/// </para>
/// </summary>
/// <param name="optionsBuilder">A builder used to create or modify options for this context. Databases (and other extensions)
/// typically define extension methods on this object that allow you to configure the context.</param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
/// <summary>
/// Override this method to further configure the model that was discovered by convention from the entity types
/// exposed in <see cref="T:Microsoft.EntityFrameworkCore.DbSet`1" /> properties on your derived context. The resulting model may be cached
/// and re-used for subsequent instances of your derived context.
/// </summary>
/// <param name="modelBuilder">The builder being used to construct the model for this context. Databases (and other extensions) typically
/// define extension methods on this object that allow you to configure aspects of the model that are specific
/// to a given database.</param>
/// <remarks>
/// If a model is explicitly set on the options for this context (via <see cref="M:Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.UseModel(Microsoft.EntityFrameworkCore.Metadata.IModel)" />)
/// then this method will not be run.
/// </remarks>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.6-servicing-10079");
modelBuilder.Entity<Blog>(entity =>
{
entity.Property(e => e.Url).IsRequired();
});
modelBuilder.Entity<Post>(entity =>
{
entity.HasOne(d => d.Blog)
.WithMany(p => p.Post)
.HasForeignKey(d => d.BlogId);
});
}
}
/// <summary>
/// Class BlogRepository.
/// Implements the <see cref="Sample.Repository.Interface.IBlogRepository" />
/// </summary>
/// <seealso cref="Sample.Repository.Interface.IBlogRepository" />
public class BlogRepository : IBlogRepository
{
/// <summary>
/// The database
/// </summary>
private readonly BloggingContext _context;
/// <summary>
/// Initializes a new instance of the <see cref="BlogRepository"/> class.
/// </summary>
/// <param name="context">The context.</param>
public BlogRepository(BloggingContext context)
{
this._context = context;
}
/// <summary>
/// 新增 Blog
/// </summary>
/// <param name="blog">實體</param>
/// <returns></returns>
public async Task<bool> AddAsync(Blog blog)
{
_context.Add(blog);
var count = await _context.SaveChangesAsync();
return count > 0;
}
/// <summary>
/// 取得 Blog
/// </summary>
/// <param name="condition">查詢條件</param>
/// <returns></returns>
public async Task<IEnumerable<Blog>> GetAsync(BlogQuery condition)
{
var blogs = await _context.Blog.ToListAsync();
return blogs;
}
/// <summary>
/// 刪除 Blog
/// </summary>
/// <param name="id">blog id</param>
/// <returns></returns>
public async Task<bool> RemoveAsync(int id)
{
var blog = await _context.Blog.FindAsync(id);
_context.Blog.Remove(blog);
var count = await _context.SaveChangesAsync();
return count > 0;
}
/// <summary>
/// 更新 Blog
/// </summary>
/// <param name="blog">實體</param>
/// <returns></returns>
public async Task<bool> UpdateAsync(Blog blog)
{
_context.Update(blog);
var count = await _context.SaveChangesAsync();
return count > 0;
}
}
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
var connection = this.Configuration.GetConnectionString("DefaultConnection");
// DI Register
services.AddScoped<IBlogService, BlogService>();
services.AddScoped<IBlogRepository, BlogRepository>();
services.AddDbContext<BloggingContext>(
options => options.UseSqlServer(connection));
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
///...............
}
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <seealso cref="Sample.Repository.Interface.IGenericRepository{TEntity}" />
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
/// <summary>
/// UnitOfWork 實體
/// </summary>
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="GenericRepository{TEntity}"/> class.
/// </summary>
/// <param name="unitofwork">The unitofwork.</param>
public GenericRepository(IUnitOfWork unitofwork)
{
this._unitOfWork = unitofwork;
}
/// <summary>
/// 新增
/// </summary>
/// <param name="entity">實體</param>
public void Add(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_unitOfWork.Context.Set<TEntity>().Add(entity);
}
/// <summary>
/// 取得全部
/// </summary>
/// <returns></returns>
public async Task<ICollection<TEntity>> GetAllAsync()
{
return await this._unitOfWork.Context.Set<TEntity>().ToListAsync();
}
/// <summary>
/// 取得單筆
/// </summary>
/// <param name="predicate">查詢條件</param>
/// <returns></returns>
public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate)
{
return await this._unitOfWork.Context.Set<TEntity>().FirstOrDefaultAsync(predicate);
}
/// <summary>
/// 刪除
/// </summary>
/// <param name="entity">實體</param>
public void Remove(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this._unitOfWork.Context.Entry(entity).State = EntityState.Deleted;
}
/// <summary>
/// 更新
/// </summary>
/// <param name="entity">實體</param>
public void Update(TEntity entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
this._unitOfWork.Context.Entry(entity).State = EntityState.Modified;
}
}
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
var connection = this.Configuration.GetConnectionString("DefaultConnection");
// DI Register
services.AddScoped<IBlogService, BlogService>();
services.AddScoped<IGenericRepository<Blog>, GenericRepository<Blog>>();
services.AddScoped<DbContext, BloggingContext>();
services.AddDbContext<BloggingContext>(
options => options.UseSqlServer(connection));
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <seealso cref="Sample.Repository.Implement.GenericRepository{TEntity}" />
public class BlogRepository<TEntity> : GenericRepository<TEntity> where TEntity : class
{
/// <summary>
/// Initializes a new instance of the <see cref="BlogRepository{TEntity}"/> class.
/// </summary>
/// <param name="context">db context.</param>
public BlogRepository(DbContext context) : base(context)
{
}
}
/// <summary>
/// BlogService
/// </summary>
public class BlogService : IBlogService
{
private IMapper _mapper;
private IUnitOfWork _unitofwork;
/// <summary>
/// Initializes a new instance of the <see cref="BlogService"/> class.
/// </summary>
public BlogService(
IUnitOfWork unitofwork,
IMapper mapper)
{
this._unitofwork = unitofwork;
this._mapper = mapper;
}
/// <summary>
/// 新增 Blog
/// </summary>
/// <param name="blogDto">Blog Dto</param>
/// <returns></returns>
public async Task<int> AddAsync(BlogDto blogDto)
{
// Convert BlogDto to Blog
var blog = this._mapper.Map<Blog>(blogDto);
this._unitofwork.BlogRepository.Add(blog);
return await this._unitofwork.SaveChangeAsync();
}
/// <summary>
/// 取得 Blog
/// </summary>
/// <param name="blogQueryDto">查詢條件</param>
/// <returns></returns>
public async Task<IEnumerable<BlogDto>> GetAsync(BlogQueryDto blogQueryDto)
{
var blogs = await this._unitofwork.BlogRepository.GetAllAsync();
// Convert Blog to BlogDto
var blogDtos = this._mapper.Map<IEnumerable<BlogDto>>(blogs);
return blogDtos;
}
/// <summary>
/// 刪除 Blog
/// </summary>
/// <param name="blogId">Blog Id</param>
/// <returns></returns>
public async Task<int> RemoveAsync(int blogId)
{
var blog = await this._unitofwork.BlogRepository.GetAsync(x => x.BlogId == blogId);
this._unitofwork.BlogRepository.Remove(blog);
return await this._unitofwork.SaveChangeAsync();
}
/// <summary>
/// 修改 Blog
/// </summary>
/// <param name="blogDto">Blog Dto</param>
/// <returns></returns>
public async Task<int> UpdateAsync(BlogDto blogDto)
{
// Convert BlogDto to Blog
var blog = this._mapper.Map<Blog>(blogDto);
this._unitofwork.BlogRepository.Update(blog);
return await this._unitofwork.SaveChangeAsync();
}
}