最为一名越过菜鸟之后的开发,需要做接口开发。下面做一个纯粹的接口编程的实例demo,仅仅是一个webapi接口的抽象。

下面是代码接口,AbsEFWork是webapi,BaseEntityFramework是一个接口库。

先介绍一下webapi的实现,代码是从底层往上层写的,阅读代码的习惯应该是自上向下。

 public class ProductController : CustomController<Product>
{
public ProductController(IEFCoreService<Product> efCoreService) : base(efCoreService)
{
}
}

控制器代码很简单的实现了CustomController,数据载体是Product。

using BaseEntityFramework.Implementations;
using BaseEntityFramework.Implementations.Entitys;
using BaseEntityFramework.IService;
using Microsoft.EntityFrameworkCore; namespace BaseEntityFramework
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers();
builder.Services.AddDbContext<ProductDbContext>(options => options.UseInMemoryDatabase("memorydb"));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IEFCoreService<Product>, EFCoreProductService>();
var app = builder.Build(); // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
} app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
}
}
}

Program启动程序需要实现IEFCoreService的注入,以及ProductDbContext 的内存实现。

这样就可以启动一个swagger

对于product数据存储的具体实现,实体类product和dbcontext必须要自己去实现它。

 public class Product:IEntity
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; } [Column(TypeName = "decimal(28,16)")]
public decimal Price { get; set; }
}
using BaseEntityFramework.Implementations.Entitys;
using Microsoft.EntityFrameworkCore; namespace BaseEntityFramework.Implementations
{
public class ProductDbContext:DbContext
{
public ProductDbContext(DbContextOptions<ProductDbContext> dbContextOptions):base(dbContextOptions)
{ }
public DbSet<Product> Products { get; set; }
}
}

查看上面的控制器代码,有注入IEFCoreService<Product>的业务代码,对于接口肯定是需要一个实现,这里可以再次封装一个抽象的基类来(温馨提示:重复的代码,必须优化),我这里暂时没做处理。

using BaseEntityFramework.Implementations.Entitys;
using BaseEntityFramework.IService;
using BaseEntityFramework.Models;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions; namespace BaseEntityFramework.Implementations
{
public class EFCoreProductService : IEFCoreService<Product>
{
private readonly ProductDbContext _dbContext;
public EFCoreProductService(ProductDbContext productDbContext)
{
_dbContext = productDbContext;
} public async Task<bool> Add(Product entity)
{
_dbContext.Products.Add(entity);
var result = await _dbContext.SaveChangesAsync();
return result != 0;
} public Task<bool> Delete(Product entity)
{
throw new NotImplementedException();
} public async Task<IEnumerable<Product>> GetAll()
{
var result =await _dbContext.Products.ToListAsync();
return result;
} public Task<Product> GetEntity(Expression<Func<Product, bool>> expression)
{
throw new NotImplementedException();
} public async Task<IReadOnlyCollection<Product>> GetList(Expression<Func<Product, bool>> expression)
{
var result = await _dbContext.Products.Where(expression).ToListAsync();
return result.AsReadOnly();
} public Task<PageResult<Product>> GetPageResult<Req>(PageInput<Req> pagInput) where Req : new()
{
throw new NotImplementedException();
} public Task<bool> Update(Product entity)
{
throw new NotImplementedException();
}
}
}

上面的代码很简单易懂,最大的好处就是可以复用。实体类和 dbcontext越多这个简简单单的结构就越是有用。

BaseEntityFramework的核心逻辑就是把业务代码做了抽象,做了一个统一的模板,不管 是从那方便说都只有好处。而且作为开发只关心自己的业务代码这一块。

 public interface IEFCoreService<T> where T:IEntity
{
Task<bool> Add(T entity) ;
Task<bool> Delete(T entity);
Task<bool> Update(T entity);
Task<IReadOnlyCollection<T>> GetList(Expression<Func<T,bool>> expression) ;
Task<PageResult<T>> GetPageResult<Req>(PageInput<Req> pagInput) where Req:new();
Task<T> GetEntity(Expression<Func<T, bool>> expression);
Task<IEnumerable<T>> GetAll();
}

以上的实例只是一个简单的demo,项目中需要做框架的话这或许是一个开始,需要做的远远不止这些。

源代码:

liuzhixin405/AbsEFWork (github.com)

面向接口编程实践之aspnetcoreapi的抽象的更多相关文章

  1. Java反射(六)纯面向接口编程的简单框架实践

    我们知道在使用MyBatis开发时,只需要添加DAO接口和对应的映射XML文件,不需要写DAO的实现类,其实底层是通过动态代理实现. 本文将使用前几篇文章的知识点实现一个纯面向接口编程的简单框架,与M ...

  2. .NET项目开发—浅谈面向接口编程、可测试性、单元测试、迭代重构(项目小结)

    阅读目录: 1.开篇介绍 2.迭代测试.重构(强制性面向接口编程,要求代码具有可测试性) 2.1.面向接口编程的两个设计误区 2.1.1.接口的依赖倒置 2.1.2.接口对实体的抽象 2.2.迭代单元 ...

  3. C#面向接口编程详解(1)——思想基础

    我想,对于各位使用面向对象编程语言的程序员来说,“接口”这个名词一定不陌生,但是不知各位有没有这样的疑惑:接口有什么用途?它和抽象类有什么区别?能不能用抽象类代替接口呢?而且,作为程序员,一定经常听到 ...

  4. 面向接口编程详解-Java篇

    相信看到这篇文字的人已经不需要了解什么是接口了,我就不再过多的做介绍了,直接步入正题,接口测试如何编写.那么在这一篇里,我们用一个例子,让各位对这个重要的编程思想有个直观的印象.为充分考虑到初学者,所 ...

  5. 设计与实现分离——面向接口编程(OO博客第三弹)

    如果说继承是面向对象程序设计中承前启后的特质,那么接口就是海纳百川的体现了.它们都是对数据和行为的抽象,都是对性质和关系的概括.只不过前者是纵向角度,而后者是横向角度罢了.今天呢,我想从设计+语法角度 ...

  6. Spring学习(2):面向接口编程思想

    一. 引言 Spring核心的IOC的实体用了面向接口编程思想,所以有必要了解下.简单来说的话,Spring就是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 接口的定义的概念:泛指实 ...

  7. C#扫盲篇(二)依赖倒置•控制反转•依赖注入•面向接口编程--满腹经纶的说

    扫盲系列的文章收到了广大粉丝朋友的支持,十分感谢,你们的支持就是我最大动力. 我的扫盲系列还会继续输出,本人也是一线码农,有什么问题大家可以一起讨论.也可以私信或者留言您想要了解的知识点,我们一起进步 ...

  8. 编写Java程序,使用面向接口编程模拟不同动物的吼叫声

    返回本章节 返回作业目录 需求说明: 使用面向接口编程模拟不同动物的吼叫声 实现思路: 使用面向接口编程模拟不同动物吼叫声的实现思路: 定义发声接口Voice,在其中定义抽象吼叫方法sing(). 分 ...

  9. C#进阶系列——MEF实现设计上的“松耦合”(终结篇:面向接口编程)

    序:忙碌多事的八月带着些许的倦意早已步入尾声,金秋九月承载着抗战胜利70周年的喜庆扑面而来.没来得及任何准备,似乎也不需要任何准备,因为生活不需要太多将来时.每天忙着上班.加班.白加班,忘了去愤,忘了 ...

  10. 使用Castle扩展Ibatis.Net,面向接口编程-更优雅的代码

    使用Ibatis.Net做项目半年了,甚是喜欢,感觉确实是个简单.轻巧的O/R Mapping框架,特别是将Sql配置在Xml文件中,相当于直接将Dao层抽离了出来. 本文假定读者对Ibatis.Ne ...

随机推荐

  1. CNN_LSTM

    1.keras/examples at master · keras-team/keras · GitHubhttps://github.com/keras-team/keras/tree/maste ...

  2. Swagger详解

    1.Swagger的介绍 1.1 Swagger的工作原理 1.系统启动,扫描到api工程中的Swagger2Configuration类 2.在此类中指定了包路径,会找到在此包下及子包下标记有@Re ...

  3. CentOS7带图形界面不能扫描网卡。

    CentOS7在带有图形界面,在配置/sysconfig/network-script/ifcfg-ens*后不能识别到网卡,有可能是NetworkManager打开导致,可以使用systemctl ...

  4. JavaScript argument

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 什么是互联网控制消息协议ICMP 以及如何作为网络分析利器

    什么是互联网控制消息协议(ICMP) Internet控制消息协议(ICMP)是网络设备用来诊断网络通信问题的网络层协议.ICMP主要用于确定数据是否及时到达其预期目的地.通常,ICMP协议用于网络设 ...

  6. finereport连接sql server

    1.添加jdbc 2.选择sql server 3.jdbc:sqlserver://localhost:1433(localhost=其他IP地址)

  7. angularJS:一个页面多个ng-app

    var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $rootScope) { $scop ...

  8. Java面向对象 --2

    22.面向对象特征之二: 继  承     2020-04-29  21:39:33 01.多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么子类无需再定义这些属性和行为,只要继承父类即 ...

  9. flask - fastapi (python 异步API 框架 可以自动生成swagger 文档) 常用示例 以及整合euraka nacos

    flask - fastapi    (python 异步API 框架  可以自动生成swagger 文档)  常用示例: 之前使用 flask 需要手动写文档, 这个可以自动生成, fastapi ...

  10. OSPF之路由撤销1