Web页面开发暂时是没有问题了,现在开始接上Ptager.BL的DB部分。

首先需要初始化用户和书房信息。因为还没有给其他多余的设计,所以暂时只有个人昵称和书房名称。

添加 Init Razor Pages(/Pages/Shelves/Init) 。

/Pages/Shelves/Init.cshtml

 @page
@model InitModel
@{
ViewData["Title"] = "Shelf Init";
} <nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a asp-page="/Index">Home</a></li>
<li class="breadcrumb-item"><a asp-page="/My Books/Index">My Books</a></li>
<li class="breadcrumb-item active" aria-current="page">Bookcase Init</li>
</ol>
</nav> <form method="post">
<div class="form-group form-group-lg">
<label asp-for="Input.NickName"></label>
<input class="form-control form-control-lg" asp-for="Input.NickName" autocomplete="off">
</div>
<div class="form-group form-group-lg">
<label asp-for="Input.ShelfName"></label>
<input class="form-control form-control-lg" asp-for="Input.ShelfName" autocomplete="off">
</div>
<div class="form-group text-right">
<button class="btn btn-warning btn-lg" type="submit">Save</button>
</div>
</form>

现在去实现底层Repo的部分。

需要在BL层添加基本的底层代码:

在PTager.BL.Core层增加DataContract、Models和Repos Interface。

暂时还没有用到查询,所以只需要增加Init Model和Repo Interface就好。

新增Shelf.InitSpec Model文件:

Shelf.InitSpec.cs

 namespace PTager.BL
{
public partial class Shelf
{
public class InitSpec
{
public string NickName { get; set; }
public string ShelfName { get; set; }
}
}
}

Install Package Newtonsoft.Json

新增_module扩展文件:

_module.cs

 namespace PTager.BL
{
[System.Diagnostics.DebuggerNonUserCode]
public static partial class extOC
{
public static string ToJson<T>(this T me) where T : class
=> JsonConvert.SerializeObject(me);
}
}

并且引用Projects:Ptager.BL

新增IShelfRepo:

IShelfRepo.cs

 using System.Threading.Tasks;

 namespace PTager.BL
{
using M = Shelf;
public interface IShelfRepo
{
Task Init(M.InitSpec spec);
}
}

在PTager.BL.Data层增加Store和Repos的实现。

引用Projects:Ptager.BL和PTager.BL.Core,并且添加Nuget Package:System.Data.SqlClient、Microsoft.EntityFrameworkCore.Relational

其中Store的内容是模仿Linq to SQL的分层做的,但没那么高的性能,勉强够用而已。

RepoBase.cs

 namespace PTager.BL.Data
{
public class RepoBase
{
protected readonly BLDbContext _context;
public RepoBase(BLDbContext context)
{
_context = context;
}
}
}

_module.cs

 namespace PTager.BL.Data
{
[System.Diagnostics.DebuggerNonUserCode]
public static partial class extBL { }
}

BLDbContext.cs

 using System.Threading.Tasks;

 namespace PTager.BL.Data.Store
{
public class BLDbContext : DbContext
{
public BLDbContext(DbContextOptions<BLDbContext> options)
: base(options)
{
this.ChangeTracker.AutoDetectChangesEnabled = false;
} #region { Functions } #endregion #region { Actions } public async Task Shelf_Init(string json)
=> await this.ExecuteMethodCallAsync(nameof(Shelf_Init), args: json); #endregion
}
}

BLDbContext.ext.cs(这是目前代码中最脏的部分了,找时间需要优化这个部分)

 using Microsoft.EntityFrameworkCore;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks; namespace PTager.BL.Data.Store
{
public static partial class extUSDContext
{
public static IQueryable<T> CreateMethodCallQuery<T>(this DbContext me, MethodBase method, string schema = "svc")
where T : class
=> me.CreateMethodCallQuery<T>(method, schema, null);
public static IQueryable<T> CreateMethodCallQuery<T>(this DbContext me, MethodBase method, string schema = "svc", params object[] args)
where T : class
{
var hasArgs = args != null && args.Length > ;
//var fields = typeof(T).GetType().ToSelectFields();
var sql = $"select * from {schema}.{method.Name.Replace("_", "$")}(";
if (hasArgs) sql += string.Join(", ", args.Select(x => $"@p{args.ToList().IndexOf(x)}"));
sql += ")";
me.Database.SetCommandTimeout(TimeSpan.FromMinutes());
return hasArgs ? me.Set<T>().FromSql(sql, args) : me.Set<T>().FromSql(sql);
} public static async Task<string> ExecuteMethodCallAsync(this DbContext me, string methodName)
=> await me.ExecuteMethodCallAsync(methodName, null);
public static async Task<string> ExecuteMethodCallAsync(this DbContext me, string methodName, params object[] args)
=> await me.ExecuteMethodCallAsync(methodName, false, args);
public static async Task<string> ExecuteMethodCallAsync(this DbContext me, string methodName, bool lastOutput, params object[] args)
{
var hasArgs = args != null && args.Length > ;
var sql = $"svc.{methodName.Replace("_", "$")} ";
if (!hasArgs)
{
if (lastOutput == false)
{
await me.Database.ExecuteSqlCommandAsync(sql);
return string.Empty;
}
}
else
{
sql += string.Join(", ", args.Select(x => $"@p{args.ToList().IndexOf(x)}"));
if (lastOutput == false)
{
await me.Database.ExecuteSqlCommandAsync(sql, args);
return string.Empty;
}
sql += ", ";
}
sql += " @result output";
var parameters = args.Select(x => new SqlParameter($"@p{args.ToList().IndexOf(x)}", x)).ToList();
var outParam = new SqlParameter("@result", SqlDbType.VarChar, int.MaxValue)
{
Value = string.Empty,
Direction = ParameterDirection.Output
};
parameters.Add(outParam);
me.Database.SetCommandTimeout(TimeSpan.FromMinutes());
await me.Database.ExecuteSqlCommandAsync(sql, parameters.ToArray());
return outParam.Value.ToString();
}
}
}

好了,基础已建好,新增ShelfRepo的实现。

ShelfRepo.cs

 namespace PTager.BL.Data.Repos
{
using System.Threading.Tasks;
using PTager.BL.Data.Store;
using M = Shelf;
public class ShelfRepo : RepoBase, IShelfRepo
{
public ShelfRepo(BLDbContext context) : base(context)
{
} public async Task Init(M.InitSpec spec)
=> await _context.Shelf_Init(spec.ToJson());
}
}

在WebUI的Project中,引用新增的三个底层项目,并在Statup.cs的ConfigureServices中注册IShelfRepo的依赖和BL DB的连接注册:

     services.AddDbContext<BLDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("BLConnection")));
services.AddScoped<IShelfRepo, ShelfRepo>();

当然,这个时会报错的,因为我们并没有在appsetting中添加这个DB Connection。

修改后的appsettings.json

 {
"ConnectionStrings": {
"DefaultConnection": "Server=.\\SQL2017;Database=PTager;Trusted_Connection=True;MultipleActiveResultSets=true",
"BLConnection": "Server=.\\SQL2017;Database=PTagerBL;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

接下来需要去新建DB的Script实现这个Shelf_Init功能了。

记开发个人图书收藏清单小程序开发(六)Web开发的更多相关文章

  1. 记开发个人图书收藏清单小程序开发(十)DB开发——新增图书信息

    昨晚完成了Web端新增图书信息的功能,现在就差DB的具体实现了. 因为我把Book相关的信息拆分的比较多,所以更新有点小麻烦. 首先,我需要创建一个Book Type的Matter: 然后,将图片路径 ...

  2. 记开发个人图书收藏清单小程序开发(三)DB设计

    主要是参考豆瓣的图书查询接口: https://api.douban.com/v2/book/isbn/:9780132350884 返回内容如下: { "rating": { & ...

  3. 记开发个人图书收藏清单小程序开发(五)Web开发

    决定先开发Web端试试. 新增Web应用: 选择ASP.NET Core Web Application,填写好Name和Location,然后点击OK. 注意红框标出来的,基于.NET Core 2 ...

  4. 记开发个人图书收藏清单小程序开发(九)Web开发——新增图书信息

    书房信息初始化已完成,现在开始处理图书信息新增功能. 主要是实现之前New Razor Pages的后台部分. 新增需要保存的Model:Book.InitSpec.cs /Models/Book.I ...

  5. 记开发个人图书收藏清单小程序开发(四)DB设计

    早上起来,又改动了一下: 主要是,将非常用信息全部拆分出来,让Table尽量的小,小到不能继续拆分了,这样区分DB逻辑.增加了FileBank存储Book的封面图片,统一管理图片资源. 新添加的Typ ...

  6. 记开发个人图书收藏清单小程序开发(七)DB设计

    前面的书房初始化的前端信息已经完善,所以现在开始实现DB的Script部分. 新增Action:Shelf_Init.sql svc.sql CREATE SCHEMA [svc] AUTHORIZA ...

  7. 微信小程序开发系列一:微信小程序的申请和开发环境的搭建

    我最近也刚刚开始微信小程序的开发,想把我自学的一些心得写出来分享给大家. 这是第一篇,从零开始学习微信小程序开发.主要是小程序的注册和开发环境的搭建. 首先我们要在下列网址申请一个属于自己的微信小程序 ...

  8. 微信小程序开发系列五:微信小程序中如何响应用户输入事件

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

  9. 微信小程序开发系列七:微信小程序的页面跳转

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...

随机推荐

  1. Selenium2+python自动化64-100(大结局)[已出书]

    前言 小编曾经说过要写100篇关于selenium的博客文章,前面的64篇已经免费放到博客园供小伙伴们学习,后面的内容就不放出来了,高阶内容直接更新到百度阅读了. 一.百度阅读地址: 1.本书是在线阅 ...

  2. apache启动不了, 查找错误

    今天apache启动不了, 本来以为是端口冲突, 用 cmd-> netstat -aon|findstr "80"  或 tasklist|findstr "80 ...

  3. 用VB实现COM+组件配置

    在Windwos2000的管理工具里有一个“组件服务”工具,可以实现对COM+组件的应用的安装.启动.删除和对组件的安装.删除.这在安装一个有 COM+组件的应用系统时时非常有用的,我们可以通过程序控 ...

  4. 2 数据库开发--MySQL下载(windows)

    Windows:(mysql) 操作: 0.下载安装mysql www.mysql.org downloads->进入社区community community 5.7.21 下载5.6 Mic ...

  5. tomcat 域名直接访问默认工程,而不添加项目路径

    <Engine name="Catalina" defaultHost="xx.xx.xx.xx"> <!--For clustering, ...

  6. SpringMVC 执行流程

  7. 前端开发之jQuery篇--选择器

    主要内容: 1.jQuery简介 2.jQuery文件的引入 3.jQuery选择器 4.jQuery对象与DOM对象的转换 一.jQuery简介 1.介绍 jQuery是一个JavaScript库: ...

  8. git的基础操作-入门

    本文是根据廖雪峰的git教程写的笔记:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...

  9. 41. First Missing Positive (HashTable)

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  10. 38-解决Fiddler查看Post参数中文乱码的问题

    转载自:https://blog.csdn.net/JusterDu/article/details/50888617 解决Fiddler查看Post参数中文乱码的问题 2016年03月14日 18: ...