Is it possible to utilize ASP.NET Boilerplate instead of Entity Framework Core with Dapper?

aspnetboilerplate dapper entity-framework-core

Question

Dapper and ASP.NET Boilerplate are new to me. I learned from the lesson that ASP.NET Boilerplate functions with Entity Framework Core. Can we use a different Data Access Layer for EF Core? Is Dapper compatible with ASP.NET Boilerplate?

1
0
3/13/2019 6:55:29 AM

Popular Answer

Sure! Dapper may be used.

Installation

Install https://www.nuget.org/packages/Abp.Dapper, EF Core, EF 6.x, or the NHibernate ORM NuGet packages into the project you want to utilize before you begin.


Registration of Modules

Prior to registering the AbpDapperModule, you must first add the DependsOn property to your module.

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

Table to Entity Mapping

Mappings are configurable. The Person class, for instance, corresponds to the Persons table in the following instance:

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

Module Example:

[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, dapper repositories may be injected using the Generic IDapperRepository interface rather than the default IRepository interface.

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");
    }
}

Visit the following links:

1
3/14/2019 11:27:23 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow