mystaging介绍

  • 这是一个 .netcore+pgsql 的脚手架,可以一键生成实体对象和业务层接口,让开发人员无需关注底层变动,专注编写业务代码,它可以让你使用 .netcore2.0的新特性,基于 pgsql 数据库,可以在项目中自由的使用 lambda 表达式编写业务,同时支持自定义的 sql 语句。

特性

  • mystaging,非常的小巧,下面将介绍 mystaging 的项目框架。
  • 一键生成,无需编写实体模型代码
  • 支持数据库枚举类型自动映射生成
  • 支持视图自动生成实体模型和业务操作接口
  • 该项目目前处于起步阶段,可能不适用于大型项目,请结合业务需要酌情使用
  • 项目 gitbhub 地址:https://github.com/lianggx/mystaging
  • 开发交流QQ群:614072957

如何开始?

使用构建工具 MyStaging.App
  1. 将 MyStaging.csproj 项目打包成 MyStaging.zip ,并复制到 MyStaging.App/bin/debug 目录下
  2. 编辑构建工具下的 @build.bat 文件,配置相关参数,参数配置见参数说明
  3. 运行该批处理文件,可以直接生成 proj.db 项目文件
参数说明
  • -h host,数据库所在服务器地址
  • -p port,端口号
  • -u username,登录数据库账号名称
  • -a password,登录密码
  • -d database,数据库名称
  • -pool pool,数据库连接池最大值,默认 32
  • -proj csproj,生成的 db 层项目名称
  • -o output path,生成项目的输出路径
初始化数据库连接
  • 在生成的 db 项目文件根目录下,找到: _startup.cs 文件,在程序入口 Program.cs 或者 Startup.cs 的适当位置,使用以下代码,传递日志记录对象和数据库连接字符串进行 db 层初始化
 string connectionString = "Host=127.0.0.1;Port=5432;Username=postgres;Password=123456;Database=your db;Pooling=true;Maximum Pool Size=100";
ILogger logger = loggerFactory.CreateLogger<MyStaging.Helpers.PgSqlHelper>();
_startup.Init(logger, connectionString);

实体对象说明

构建工具会自动生成DAL层,包含实体模型和对象关系,由于执行数据库查询的过程中,高度依赖实体模型映射,所以在生成实体模型时,对实体做了一些ORM的映射设置,这些设置对实体模型的影响非常小。

  • EntityMappingAttribute

    该特性类接受一个属性:TableName,指明该实体模型映射到数据库中的>模式.表名,如

     ```
    [EntityMapping(TableName = "public.user")]
    public partial class Public_userModel
    {
    }
    ```
  • ForeignKeyMappingAttribute

    应用该该特性类到属性上,表示这个是一个外键引用的属性,如

      ```
    private Public_userModel _public_User=null;
    [ForeignKeyMapping,JsonIgnore]public Public_userModel Public_User { get{ if(_public_User==null)_public_User= Public_user.Context.Where(f=>f.Id==this.User_id).ToOne(); return _public_User;} }
    ```
    *以上代码还应用了特性:JsonIgnore ,表示,该外键在对象进行 json 序列化的时候选择忽略该属性*
  • NonDbColumnMappingAttribute

    应用该该特性类到属性上,表示这个是一个自定义的属性,在进行数据库查询的时候将忽略该属性,如

     ```
    [NonDbColumnMappingAttribute,JsonIgnore] public Public_user.UpdateBuilder UpdateBuilder{get{return new Public_user.UpdateBuilder(this.Id);}}
    ```

以上代码还应用了特性:JsonIgnore ,表示,该外键在对象进行 json 序列化的时候选择忽略该属性

MyStaging 命名空间

  • MyStaging.Common 主要定义公共类型和资源
  • MyStaging.Helpers 数据库操作帮助类、连接池管理工具类、DAL操作辅助函数
  • MyStaging.Mapping 动态对象生成管理、实体对象映射定义

MyStaging.Helpers.QueryContext

DAL继承的基类,该类实现了所有对数据库操作的封装,可直接继承使用,如果使用脚手架附带的构建工具,直接进行业务编写即可


数据库操作

  • 插入记录
    Public_userModel user = new Public_userModel();
user.Id = Guid.NewGuid();
user.Login_name = "test@gmail.com";
Public_user.Insert(user);
  • 修改记录
    // 自动根据主键修改
Public_userModel user = new Public_userModel();
user.UpdateBuilder.SetLogin_time(DateTime.Now).SaveChange(); // 自定义条件修改
user.UpdateBuilder.SetLogin_time(DateTime.Now).Where(f => f.Sex == true).SaveChange(); // 直接修改
Public_user.Update(Guid.Empty).SetLogin_time(DateTime.Now).Where(f => f.Sex == true).SaveChange(); // 自定义条件的直接修改
Public_user.UpdateBuilder.SetLogin_time(DateTime.Now).Where(f => f.Id == Guid.Empty).Where(f => f.Sex == true).SaveChange();
  • 删除记录
    // 根据主键删除
Public_user.Delete(Guid.Empty);
// 根据条件删除
Public_user.DeleteBuilder.Where(f => f.Id == Guid.Empty).Where(f => f.Sex == true).SaveChange();
  • 查询单条记录
    Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne();
  • 指定查询列
    Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne("id","login_name");
  • 指定查询返回类型
    public class UserModel{
public string Login_name{get;set;}
public Guid Id{get;set;}
}
Public_userModel user = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne<UserModel>("id","login_name");
  • 查询列表
    List<Public_userModel> list = Public_user.Context.ToList();
List<Public_userModel> list = Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToList(); public class UserModel{
public string Login_name{get;set;}
public Guid Id{get;set;}
} List<UserModel> list = Public_user.Context.ToList<UserModel>("id","login_name");
  • 表连接查询
    public class UserModel{
public string Login_name{get;set;}
public Guid Id{get;set;}
} List<UserModel> list = Public_user.Context.Union<TopicModel>("b",UnionType.INNER_JOIN,(a,b)=>a.Id==b.User_Id).Where(a=>a.Id=Guid.Empty).Where<TopicModel>(b=>b.Publish==true).ToList<UserModel>("id","login_name");
  • 分页
    Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderBy(f=>f.State).Page(1,10);
  • 排序
    Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderBy(f=>f.State);
Public_user.Context.Where(f => f.Login_name == "test@gmail.com").OrderDescing(f=>f.State);
  • 聚合查询
    Public_user.Context.Where(f => f.Login_name == "test@gmail.com").Avg(f=>f.Age);
Public_user.Context.Where(f => f.Login_name == "test@gmail.com").Sum(f=>f.Blance);
// Max,Min,GroupBy,Having
  • 事务
     PgSqlHelper.Transaction(() =>
{
Public_userModel user= Public_user.Context.Where(f => f.Login_name == "test@gmail.com").ToOne();
user.UpdateBuilder.SetLogin_time(DateTime.Now).SaveChange();
});

.netcore2.0+pgsql 脚手架的更多相关文章

  1. netcore2.0 ORM框架中如何配置自定义的主外键加载

    环境:netcore2.0 DB :mysql ORM:Ant https://github.com/yuzd/AntData.ORM/tree/netcore2 [给我一个star吧] NUGET: ...

  2. NetCore2.0技术文章目录

    记录NetCore2.0的学习和工作,理解对与错不重要,重要的是,我飘~~~过 ------------------------------------------------------------ ...

  3. 01、NetCore2.0优化之Web服务器 与 IIS解耦

    01.NetCore2.0优化之Web服务器 与 IIS解耦 在Asp.Net Core 2.0中,是如何实现跨平台的?不使用IIS了,在linux上的WebServer是什么? ---------- ...

  4. 02、NetCore2.0优化之Nuget包

    02.NetCore2.0优化之Nuget包 在NetCore2.0中的包是如何管理的?如何存储的?微软做了哪些优化工作? -------------------------------------- ...

  5. 03、NetCore2.0下Web应用之搭建最小框架

    03.NetCore2.0下Web应用之搭建最小框架 这里我们不使用VS2017或者CLI命令的方式创建Asp.Net Core 2.0网页应用程序,而是完全手工的一点点搭建一个Web框架,以便更好的 ...

  6. 04、NetCore2.0下Web应用之Startup源码解析

    04.NetCore2.0Web应用之Startup源码解析   通过分析Asp.Net Core 2.0的Startup部分源码,来理解插件框架的运行机制,以及掌握Startup注册的最优姿势. - ...

  7. 05、NetCore2.0依赖注入(DI)之Web应用启动流程管理

    05.NetCore2.0依赖注入(DI)之Web应用启动流程管理 在一个Asp.net core 2.0 Web应用程序中,启动过程都做了些什么?NetCore2.0的依赖注入(DI)框架是如何管理 ...

  8. 06、NetCore2.0依赖注入(DI)之整合Autofac

    06.NetCore2.0依赖注入(DI)之整合Autofac 除了使用NetCore2.0系统的依赖注入(DI)框架外,我们还可以使用其他成熟的DI框架,如Autofac.Unity等.只要他们支持 ...

  9. 07、NetCore2.0依赖注入(DI)之生命周期

    07.NetCore2.0依赖注入(DI)之生命周期 NetCore2.0依赖注入框架(DI)是如何管理注入对象的生命周期的?生命周期有哪几类,又是在哪些场景下应用的呢? -------------- ...

随机推荐

  1. Day3----《Pattern Recognition and Machine Learning》Christopher M. Bishop

    其实今天只花了一点点时间来学习这本书, 如果模型的参数过多,而训练数据又不足够多的话,就会出现overfitting. overfitting可以通过regularization来解决,贝叶斯方法也可 ...

  2. Hive中的Order by与关系型数据库中的order by语句的异同点

    在Hive中,ORDER BY语句是对查询结果集进行整体的排序,最终将会产生一个reducer进行全局的排序,达到的最终结果是和传统的关系型数据库是一样的. 在数据量非常大的时候,全局排序的单个red ...

  3. 2.SSM整合_多表_一对一或多对一的增删改查

    一对一和多对一配置一样,这里就放到一起. 1.配置文件跟上一章一样,这里就不多写了,主要是Mapper映射文件 多 接口 public interface NewsMapper { public vo ...

  4. [LeetCode] Car Fleet 车队

    N cars are going to the same destination along a one lane road.  The destination is target miles awa ...

  5. Redis配置参数详解

    Redis配置参数详解 /********************************* GENERAL *********************************/ // 是否作为守护进 ...

  6. css实现超出两行隐藏

    overflow:hidden; text-overflow:ellipsis; display:-webkit-box; -webkit-box-orient:vertical; -webkit-l ...

  7. 小程序textarea完美填坑

    相信做微信小程序的码友们都被textarea这个原生组件坑过,什么placeholder位置错乱,穿透弹窗或遮罩层,ios上输入法弹起后换行输入内容遮挡,删除输入内容时内容被遮挡等等... 反正综上所 ...

  8. JavaWeb开发SSM框架搭建详解

    1.需要用到的jar包:由于很多的jar包不好下载,我直接上传到百度网盘: 很多,而且不好下载,我已经整理好好了: 链接:https://pan.baidu.com/s/1iIFprmstp86uKz ...

  9. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  10. [Swift]LeetCode902. 最大为 N 的数字组合 | Numbers At Most N Given Digit Set

    We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  (Not ...