概述0x01、前言学习ASP.NET Core也有一段时间了,虽说很多内容知识点还是处于一知半解的状态,但是基本的,还是略懂一二。如果有错误,还望见谅。本文还是和之前一样,Demo+在Linux下运行(CentOS7+dotnetcore sdk)开发环境:win10+vs2015+sqlserver2014 0x02、demo新建一个ASP.NET Core Web Application项目--Catcher.EasyDemo.Website干掉Controllers文件夹。由于个人习惯问题,习惯性将Controller分离出来。新建三个Class Library项目:Catcher.EasyDemo.Controllers:剥离出来的ControllerCatcher.EasyDemo.DataAccess:数据访问Catcher.EasyDemo.Models:模型Controller项目需要添加MVC的引用:"Microsoft.AspNetCore.Mvc": "1.0.0"在Controllers中添加HomeController,内容和生成的是一样的。然后在Website中添加引用,这里有两种方式,一种是和平常一样的右键->添加引用,另一种是在project.json中的dependencies节点下面添加 "Catcher.EasyDemo.Controllers": "1.0.0-*",然后就会自动restore,完成之后就能正常跑起来了。(这里就不截图了)下面的话,在Models中添加一个Product类:1 namespace Catcher.EasyDemo.Models2 {3 public class Product4 {5 public int ProductId { get; set; }6 public string ProductName { get; set; }7 public string ProductSource { get; set; }8 public decimal ProductPrice { get; set; }9 }10 }在DataAccess中添加ProductDataAccess类,用于数据交互,里面有用到dapper,所以要添加引用,以及用到了读取json配置的方法,所以还要添加Microsoft.Extensions.Configuration的引用,同时还要添加Models的引用,方法上面已经说过了。这里没有用一些复杂的东西,就一个单例模式和一些简单的数据库 *** 作。1 using Catcher.EasyDemo.Models;2 using Dapper;3 using Microsoft.Extensions.Configuration;4 using System.Collections.Generic;5 using System.Data;6 using System.Data.SqlClient;7 using System.IO;8 using System.Linq;910 namespace Catcher.EasyDemo.DataAccess11 {12 public sealed class ProductDataAccess13 {14 public static ProductDataAccess Instance15 {16 get17 {18 return Nested.instance;19 }20 }2122 class Nested23 {24 static Nested() { }25 internal static readonly ProductDataAccess instance = new ProductDataAccess();26 }2728 /// <summary>29 /// get the connection string form the appsettings.json30 /// </summary>31 /// <returns></returns>32 private string GetConnStr()33 {34 var builder = new ConfigurationBuilder();35 builder.SetBasePath(Directory.GetCurrentDirectory());36 builder.AddJsonFile("appsettings.json");37 var config = builder.Build();38 return config.GetConnectionString("dapperConn");39 }4041 /// <summary>42 /// open the connection43 /// </summary>44 /// <returns></returns>45 private SqlConnection OpenConnection()46 {47 SqlConnection conn = new SqlConnection(GetConnStr());48 conn.Open();49 return conn;50 }5152 /// <summary>53 /// get all products54 /// </summary>55 /// <returns></returns>56 public IList<Product> GetAll()57 {58 using (IDbConnection conn = OpenConnection())59 {60 string sql = @"SELECT [ProductId]61 ,[ProductName]62 ,[ProductSource]63 ,[ProductPrice]64 FROM [dbo].[Product]";65 return conn.Query<Product>(sql).ToList();66 }67 }6869 /// <summary>70 /// delete the product by product's id71 /// </summary>72 /// <param name="pid">id of the product</param>73 /// <returns></returns>74 public bool Delete(int pid)75 {76 using (IDbConnection conn = OpenConnection())77 {78 string sql = string.Format(@"DELETE FROM [dbo].[Product] WHERE [ProductId]={0} ", pid.ToString());79 return conn.Execute(sql) > 0;80 }81 }8283 /// <summary>84 /// add the product85 /// </summary>86 /// <param name="product">entity of the product</param>87 /// <returns></returns>88 public bool Add(Product product)89 {90 using (IDbConnection conn = OpenConnection())91 {92 string sql = string.Format(@"INSERT INTO [dbo].[Product]93 ([ProductName]94 ,[ProductSource]95 ,[ProductPrice])96 VALUES97 ('{0}','{1}',{2})", product.ProductName, product.ProductSource, product.ProductPrice);98 return conn.Execute(sql) > 0;99 }100 }101 }102 }然后在Controllers中添加一个ProductController,具体内容如下: 1 using Microsoft.AspNetCore.Mvc;2 using Catcher.EasyDemo.Models;3 using Catcher.EasyDemo.DataAccess;45 namespace Catcher.EasyDemo.Controllers6 {7 public class ProductController : Controller8 {9 /// <summary>10 /// Index11 /// </summary>12 /// <returns></returns>13 public IActionResult Index()14 {15 return View(ProductDataAcc
学习ASP.NET Core也有一段时间了,虽说很多内容知识点还是处于一知半解的状态,但是基本的,还是
本文还是和之前一样,Demo+在linux下运行(CentOS7+dotnetcore sdk)开发环境:win10+vs2015+sqlserver2014
0x02、demo
干掉Controllers文件夹。由于个人习惯问题,习惯性将Controller分离出来。新建三个Class library项目:
Catcher.EasyDemo.Controllers:剥离出来的ControllerCatcher.EasyDemo.DataAccess:数据访问
Catcher.EasyDemo.Models:模型
Controller项目需要添加MVC的引用:"Microsoft.AspNetCore.Mvc": "1.0.0"
在Controllers中添加HomeController,内容和生成的是一样的。然后在Website中添加引用,这里有两种方式,一种是和平常一样的右键->添加引用,另一种是在project.Json中的dependencIEs节点下
面添加 "Catcher.EasyDemo.Controllers": "1.0.0-*",然后就会自动restore,完成之后就能正常跑起
来了。(这里就不截图了)下面的话,在Models中添加一个Product类:
ProductID { ; Productname { ; ProductSource { ; ProductPrice { ; }在DataAccess中添加ProductDataAccess类,用于数据交互,里面有用到dapper,所以要添加引用,
以及用到了读取Json配置的方法,所以还要添加Microsoft.Extensions.Configuration的引用,同时还要添加Models的引用,方法上面已经说过了。
这里没有用一些复杂的东西,就一个单例模式和一些简单的数据库 *** 作。
ProductDataAccess instance = builder = builder.AddJsonfile( config = config.GetConnectionString( sqlConnection conn = IList (IDbConnection conn = sql = conn.query Delete( (IDbConnection conn = sql = .Format(