Trying to set up a Dapper-based Data Access Layer. ABP.Dapper documentation is confusing and incomplete

纵饮孤独 提交于 2019-12-25 02:15:57

问题


I'm trying to set up a simple DAL that will return a List of typed objects. Pretty standard data repository stuff. I downloaded all of ABP's code from GitHub, built the DLLs for Abp.Dapper and Abp.EntityFrameworkCore and started following the instructions on this page: https://aspnetboilerplate.com/Pages/Documents/Dapper-Integration

But I can't even get past step one of this. This code doesn't compile because it doesn't know what SampleApplicationModule is. But there's no guidance in these instructions as to what that is supposed to be.

How am I supposed to use Abp's libraries? I'm lost. Can someone please let me know the minimum number of things I need to do in order to wire up my database to Abp's library and query for a List of typed objects?

Code from Abp's Dapper Integration documentation:

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

回答1:


if you are confused what to write for SampleApplicationModule use the below code

Module Registration

 [DependsOn(
     typeof(AbpEntityFrameworkModule),
     typeof(AbpKernelModule),
     typeof(AbpDapperModule)
 )]
 public class SampleApplicationModule : AbpModule
 {
     public override void Initialize()
     {
         IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
     }
 }

Usage

public class SomeDomainService : ITransientDependency
{
    private readonly IDapperRepository<Animal> _animalDapperRepository;
    private readonly IRepository<Animal> _animalRepository;
    private readonly IDapperRepository<Person> _personDapperRepository;
    private readonly IRepository<Person> _personRepository;
    private readonly IUnitOfWorkManager _unitOfWorkManager;

    public SomeDomainService(
        IUnitOfWorkManager unitOfWorkManager,
        IRepository<Person> personRepository,
        IRepository<Animal> animalRepository,
        IDapperRepository<Person> personDapperRepository,
        IDapperRepository<Animal> animalDapperRepository)
    {
        _unitOfWorkManager = unitOfWorkManager;
        _personRepository = personRepository;
        _animalRepository = animalRepository;
        _personDapperRepository = personDapperRepository;
        _animalDapperRepository = animalDapperRepository;
    }

    public void DoSomeStuff()
    {
        using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin())
        {
            _personRepository.Insert(new Person("Oğuzhan"));
            _personRepository.Insert(new Person("Bread"));

            _animalRepository.Insert(new Animal("Bird"));
            _animalRepository.Insert(new Animal("Cat"));

            _unitOfWorkManager.Current.SaveChanges();

            Animal animal = _animalRepository.FirstOrDefault(x => x.Name == "Bird");

            Person person = _personDapperRepository.Get(1);
            int personCount = _personDapperRepository.Count(x => x.Name == "Oğuzhan");
            List<Animal> persons = _animalDapperRepository.GetList(x => x.Name.StartsWith("O")).ToList();

            uow.Complete();
        }
    }
}

See the related post for AbpDapper https://github.com/aspnetboilerplate/aspnetboilerplate/pull/1854#issuecomment-284511423

PS: Abp.Dapper integration is implemented by the community.



来源:https://stackoverflow.com/questions/51678094/trying-to-set-up-a-dapper-based-data-access-layer-abp-dapper-documentation-is-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!