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

           
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值