第七章 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<>));

           
        }
    }
笔者原创!如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,转载请添加原博客连接,否则保留追究法律责任的权利,谢谢! YC.Boilerplate 快速开发框架交流,请加群:1060819005 区块链交流请加QQ群:538327407(已满),群2:135019400. 我的博客地址:http://www.cnblogs.com/linbin524/
原文地址:https://www.cnblogs.com/linbin524/p/15210305.html