YDal多是東半球最好.Net Core ORM框架


typora-copy-images-to: staticmysql


ydal

項目框架介紹git

多是東半球最好netcore ORM框架,目前暫時支持mysql、mssql,其核心設計目標是開發迅速、學習簡單、輕量級、易擴展。現已開放源代碼,開箱即用。
It may be the best NETCORE ORM framework in the eastern hemisphere. At present, it temporarily supports MySQL and MSSQL, and its core design goal is to opengithub

特此聲明sql

重複造輪子僅爲netcore開源貢獻本身一份力量,讓用戶在orm框架上多一份選擇,能夠免費商用並提供永久技術支持,githunb:https://github.com/KenneyChen/ydal數據庫

使用步驟

安裝dlljson

Install-Package ydal
Install-Package YDal.CodeGenerator

也可使用nuget管理客戶端進行安裝c#

注入代碼api

public void ConfigureServices(IServiceCollection services)
 {
            services.AddControllers();

             services
               .AddDal()//類庫核心功能
               .AddDalCodeGenerator();//代碼生成器專用,生成框架代碼以後請註釋這段代碼AddDalCodeGenerator; 
 }

生成框架代碼配置app

{
    //鏈接數據庫專用
    "ConnectionStrings": {
        "DefaultConnection": "server=localhost;uid=root;pwd=123456;port=3306;database=test;sslmode=Preferred;",
        "DbType": "MySQL"  //無此配置,默認mysql
    }
    //mssql鏈接字符串 Data Source=192.168.1.110;Initial Catalog=test;Persist Security Info=True;User ID=test;Password=123456;MultipleActiveResultSets=True;App=EntityFramework;
    //代碼生成器插件使用 
    "DbOption": {
        "ConnectionString": "server=localhost;uid=root;pwd=123456;port=3306;database=test;sslmode=Preferred;",
        "DbType": "MySQL", //mssql  mysql
        "Author": "做者名稱",
        "OutputPath": "F:\\app\\y-dal\\cs", //生成代碼路徑
        "ModelsNamespace": "Samples.Models", //實體命名空間
        "IRepositoryNamespace": "Samples.Repository", //倉儲接口命名空間
        "RepositoryNamespace": "Samples.Repository.Impl", //倉儲命名空間
        "IServicesNamespace": "Samples.Services", //服務接口命名空間
        "ServicesNamespace": "Samples.Services.Impl", //服務命名空間
        "Tables":"" //指定表名生成,多個用逗號隔開,爲空所有
    }
}

測試框架

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Samples.Models;
using Samples.Repository;

namespace Samples.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private IdemoRepository DemoRepostiory1;


        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger, IdemoRepository DemoRepostiory)
        {
            _logger = logger;
            this.DemoRepostiory1 = DemoRepostiory;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            DemoRepostiory1.Insert(new demo { UserName = "明哥富婆" });

            Console.WriteLine($"{DemoRepostiory1.GetHashCode()}");
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

API使用

支持如下2種寫法,根據自身須要選擇其中一種便可,如下全部代碼查看源碼UnitTest項目

lambda

xxxRepository.Where(f=>f.Id==123);

Linq:

from a in xxxRepository

一、where (條件查詢)

①簡單使用
var order1 = _orderdetailRepository.Entities.Where(f => f.itemid == 123)
              .Select(f => new
              {
                  f.itemid
              })
              .FirstOrDefault();

var order1 = _orderdetailRepository.Filter(f => f.itemid == 123);

排序從小到大
var order1 = _orderdetailRepository.Filter(f => f.orderid == 1, p => p.OrderBy(f => f.Id));

排序從大到小
var order1 = _orderdetailRepository.Filter(f => f.orderid == 1, p => p.OrderByDescending(f => f.Id));

多個字段排序
var order1 = _orderdetailRepository.Filter(f => f.orderid == 1, p => p.OrderBy(f => f.Id).ThenBy(f=>f.itemid));    

②多個條件
var order1 = _orderdetailRepository.Filter(f => f.itemid == 123 && f.orderid == 1); 

③多個表關聯 
var order1 = from a in _orderdetailRepository.Entities
             join b in _ordermasterRepository.Entities on a.orderid equals b.Id
             select new
             {
                 b.paytime,
                 a.totalprice,
                 a.itemid,
             };

二、select(映射)

①匿名對象
var order1 = _orderdetailRepository.Entities.Where(f => f.itemid == 123)
             .Select(f => new
              {
                  f.itemid
              })
             .FirstOrDefault();

②自定義對象
 public class Test
 {
        public string itemname04 { get; set; }
        public int itemid04 { get; set; }
 }
var order1 = _orderdetailRepository.Entities.Where(f => f.itemid == 123)
             .Select(f => new test
              {
                  f.itemid
              })
             .FirstOrDefault();

三、update (更新)

①簡單更新(不推薦)
var order1 = _orderdetailRepository.FilterWithTracking(f => f.itemid == 123);
order1.totalprice = 30.01M;
var r= _orderdetailRepository.Update(order1);

②sql語法糖更新(強烈推薦)
 var r=_orderdetailRepository.Update(f => f.itemid == 123, f => new Orderdetail
         {
            totalprice = 32.01M
         });

四、delete (刪除)

① 簡單刪除(不推薦)
var order1 = _orderdetailRepository.FilterWithTracking(f => f.itemid == 9990);
var r = _orderdetailRepository.Delete(order1);

② sql語法糖更新(強烈推薦)
var r = _orderdetailRepository.Delete(f => f.itemid == 9990);

五、insert (插入)

①、簡單插入
var r = _orderdetailRepository.Insert(new Orderdetail
            {
                itemid = 111115,
                itemname = "測試insert",
                totalprice = 111,
                orderid = 1,
            });
            
② 批量插入
var itemId = new Random().Next(100000, 999999);
var list = new List<Orderdetail>();
for (int i = 0; i < 10; i++)
{
    list.Add(new Orderdetail
    {
       itemid = itemId,
       itemname = "測試插入",
       totalprice = 111,
       orderid = 1,
       });
    };
var r = _orderdetailRepository.Insert(list);

六、page分頁 (分頁)

①無條件單表分頁
var page = new PagingInfo
{
     PageIndex = 2, //當前頁
     PageSize = 5,//每頁條數
};
var result = _orderdetailRepository.GetListByPage(page);
var totalCount = result.TotalCount;//返回總頁數 

② 有條件單表分頁
var page = new PagingInfo
{
    PageIndex = 2, //當前頁
    PageSize = 5,//每頁條數
};
var result = _orderdetailRepository.Entities.Where(f => f.Id > 10).Paging(page); 

③ 支持匿名對象分頁
var page = new PagingInfo
{
    PageIndex = 1, //當前頁
    PageSize = 5,//每頁條數
};
var result = _orderdetailRepository.Entities
              .Where(f => f.Id > 10)
              .Select(p => new
                {
                    p.itemid,
                    p.itemname
                })
                .Paging(page);

④ 支持自定義對象分頁
 var page = new PagingInfo
 {
                PageIndex = 1, //當前頁
                PageSize = 5,//每頁條數
 };
var result = _orderdetailRepository.Entities
               .Where(f => f.Id > 10)
                .Select(p => new Test
                {
                    itemid04 = p.itemid,
                    itemname04 = p.itemname
                })
                .Paging(page);  


⑤多表分頁
var page = new PagingInfo
{
                PageIndex = 2, //當前頁
                PageSize = 5,//每頁條數
};
var query = from a in _orderdetailRepository.Entities
            join b in _ordermasterRepository.Entities on a.orderid equals b.Id
            select new
            {
                b.paytime,
                a.totalprice,
                a.itemid,
            };
var result = query.Paging(page);

問題反饋

能夠直接進羣反饋:916213430

相關文章
相關標籤/搜索