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

aspnetboilerplate dapper entity-framework-core

Question

I'm new to ASP.NET Boilerplate and Dapper. From the tutorial I got to know that ASP.NET Boilerplate works with Entity Framework Core. Can we use another Data Access Layer instead of EF Core? Is ASP.NET Boilerplate compatible with Dapper?

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

Popular Answer

Sure! you can use Dapper.

Installation

Before you start, you need to install https://www.nuget.org/packages/Abp.Dapper, either EF Core, EF 6.x or the NHibernate ORM NuGet packages in to the project you want to use.


Module Registration

First you need to add the DependsOn attribute for the 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());
    }
}

Entity to Table Mapping

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

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

Sample Module:

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

Check out these 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