第七章 Net 5.0 快速開發框架 YC.Boilerplate -- 數據層ORM 設計


在線文檔:http://doc.yc-l.com/#/README
在線演示地址:http://yc.yc-l.com/#/login
源碼github:https://github.com/linbin524/yc.boilerplate
源碼gitee:https://gitee.com/linxuanming/yc.boilerplate
元磁之力框架技術群QQ:1060819005

視頻教程:

數據層ORM 設計

適配支持 Dapper和Freesql

數據層采用開發動態注入模式選擇對應的ORM,目前規划設計支持Dapper 和Freesql。

映射流程如下

dapper ORM

dapper 使用說明

統一設計IUnitOfWork 接口,默認實現sql 工作單元模式的DefaultUnitOfWork和簡單型 單表 Lambda模式的LambdaUnitOfWork。
PS:接入dapper 主要是為了原生sql的對接。性能效率高,缺點是沒有lambda 語法糖的舒服感。

類圖關系如下:

注入操作核心代碼

 public class DapperAutofacModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
          
            builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope().EnableInterfaceInterceptors()
                     .InterceptedBy(typeof(AopInterceptor));//這一步是倉儲注入的重要的點,允許攔截器
           
            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
            // builder.RegisterType<DefaultUnitOfWork>().As<IUnitOfWork>().WithParameter("dbConnectionString",dbConfig.DefaultDbConnectionString).AsImplementedInterfaces().InstancePerLifetimeScope().PropertiesAutowired();
         
            builder.RegisterType<DefaultUnitOfWork>().As<DapperFrameWork.IUnitOfWork>().AsImplementedInterfaces().InstancePerLifetimeScope().PropertiesAutowired();
            //builder.RegisterType<TenantIdentificationStrategy>().As<ITenantIdentificationStrategy>().AsImplementedInterfaces().InstancePerLifetimeScope().PropertiesAutowired();


        }
    }

FreeSql ORM

freeSql 使用介紹

通過設計 IFreeSqlRepository<TEntity, TKey>、 IFreeSqlRepository 接口,來具體實現freesql Orm 映射。
PS:接入freesql 原因:在作者使用過的各種orm中,它的學習成本低、舒適度、便捷度都很棒,便於懶人模式。

類圖關系如下:

注入核心代碼

    /// <summary>
    /// FreeSql 注入模塊
    /// </summary>
    public class FreesqlAutofacModule : Autofac.Module
    {
        /// <summary>
        /// FreeSql 注入模塊操作
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(FreeSqlRepository<,>)).As(typeof(IFreeSqlRepository<,>)).InstancePerLifetimeScope().EnableInterfaceInterceptors()
                                 .InterceptedBy(typeof(AopInterceptor));//freeSql 注入

            builder.RegisterGeneric(typeof(FreeSqlRepository<>)).As(typeof(IFreeSqlRepository<>)).InstancePerLifetimeScope().EnableInterfaceInterceptors()
                     .InterceptedBy(typeof(AopInterceptor));//freeSql 注入

            //這個自帶DI注入,也是可以的,反正后面都會被autofac接管
            //services.AddScoped(typeof(IFreeSqlRepository<,>), typeof(FreeSqlRepository<,>));
            //services.AddScoped(typeof(IFreeSqlRepository<>), typeof(FreeSqlRepository<>));

           
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM