zoukankan      html  css  js  c++  java
  • ABP框架系列之十六:(Dapper-Integration-Dapper集成)

    Introduction

    Dapper is an object-relational mapper (ORM) for .NET. Abp.Dapper package simply integrates Dapper to ASP.NET Boilerplate. It works as secondary ORM provider with EF 6.x, EF Core or NHibernate.

    Installation

    Before you start, you need to install Abp.Dapper and one of EF Core, EF 6.x and NHibernate ORM nuget packages to project that you want to use it.

    Module Registration

    First you need to add DependsOn attribute for AbpDapperModule on your module where you register it.

    [DependsOn(
         typeof(AbpEntityFrameworkCoreModule),
         typeof(AbpDapperModule)
    )]
    public class MyModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
        }
    }

    Note that AbpDapperModule dependency should be added later than EF Core dependency.

    Entity to Table Mapping

    You can configure mappings. For example, Person class maps to Persons table in the following example:

    public class PersonMapper : ClassMapper<Person>
    {
        public PersonMapper()
        {
            Table("Persons");
            Map(x => x.Roles).Ignore();
            AutoMap();
        }
    }

    You should set the assemblies contains mapper classes. Excample:

    [DependsOn(
         typeof(AbpEntityFrameworkModule),
         typeof(AbpDapperModule)
    )]
    public class MyModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
            DapperExtensions.SetMappingAssemblies(new List<Assembly> { typeof(MyModule).GetAssembly() });
        }
    }
    			

    Usage

    After registering AbpDapperModule, you can use Generic IDapperRepository interface (instead of standard IRepository) to inject dapper repositories.

    public class SomeApplicationService : ITransientDependency
    {
        private readonly IDapperRepository<Person> _personDapperRepository;
        private readonly IRepository<Person> _personRepository;
    
        public SomeApplicationService(
            IRepository<Person> personRepository,
            IDapperRepository<Person> personDapperRepository)
        {
            _personRepository = personRepository;
            _personDapperRepository = personDapperRepository;
        }
    
        public void DoSomeStuff()
        {
            var people = _personDapperRepository.Query("select * from Persons");
        }
    }
    

    You can use both EF repositories and Dapper repositories at the same time, in the same transaction.

  • 相关阅读:
    零位扩展和符号位扩展
    JAVA虚拟机20基于栈的解释器执行过程示例
    JAVA虚拟机16方法的动态调用
    JAVA虚拟机17栈帧(局部变量表操作数栈动态连接返回地址)
    JAVA虚拟机21JAVA内存模型
    JAVA虚拟机18方法调用
    符号扩展和零位扩展
    JAVA虚拟机22原子性、可见性与有序性、先行发生原则
    MYSQL各版本下载,包括linux和windows
    【转】Android模拟器怎么配置网络连通
  • 原文地址:https://www.cnblogs.com/smileberry/p/7929362.html
Copyright © 2011-2022 走看看