Browse Source

Use ObjectMapper instead of manual mapping. Also enabled property injection and created base ApplicationService class.

pull/96/head
Halil İbrahim Kalkan 9 years ago
parent
commit
dd800db99a
  1. 1
      src/AbpDesk/AbpDesk.Application/AbpDesk.Application.csproj
  2. 11
      src/AbpDesk/AbpDesk.Application/AbpDesk/AbpDeskApplicationModule.cs
  3. 14
      src/AbpDesk/AbpDesk.Application/AbpDesk/AbpDeskApplicationModuleAutoMapperProfile.cs
  4. 30
      src/AbpDesk/AbpDesk.Application/AbpDesk/Tickets/TicketAppService.cs
  5. 2
      src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs
  6. 6
      src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs
  7. 2
      src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs
  8. 1
      src/Volo.Abp.Identity.Application/Volo.Abp.Identity.Application.csproj
  9. 12
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpDeskApplicationModuleAutoMapperProfile.cs
  10. 11
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpIdentityApplicationModule.cs
  11. 25
      src/Volo.Abp.Identity.Application/Volo/Abp/Identity/UserAppService.cs
  12. 9
      src/Volo.Abp/Volo/Abp/Application/Services/ApplicationService.cs

1
src/AbpDesk/AbpDesk.Application/AbpDesk.Application.csproj

@ -12,6 +12,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
<ProjectReference Include="..\AbpDesk.Application.Contracts\AbpDesk.Application.Contracts.csproj" /> <ProjectReference Include="..\AbpDesk.Application.Contracts\AbpDesk.Application.Contracts.csproj" />
<ProjectReference Include="..\AbpDesk.Domain\AbpDesk.Domain.csproj" /> <ProjectReference Include="..\AbpDesk.Domain\AbpDesk.Domain.csproj" />
</ItemGroup> </ItemGroup>

11
src/AbpDesk/AbpDesk.Application/AbpDesk/AbpDeskApplicationModule.cs

@ -1,14 +1,23 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace AbpDesk namespace AbpDesk
{ {
[DependsOn(typeof(AbpDeskDomainModule))] [DependsOn(typeof(AbpDeskDomainModule), typeof(AbpAutoMapperModule))]
public class AbpDeskApplicationModule : AbpModule public class AbpDeskApplicationModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
services.AddAssemblyOf<AbpDeskApplicationModule>(); services.AddAssemblyOf<AbpDeskApplicationModule>();
services.Configure<AbpAutoMapperOptions>(options =>
{
options.Configurators.Add(context =>
{
context.MapperConfiguration.AddProfile<AbpDeskApplicationModuleAutoMapperProfile>();
});
});
} }
} }
} }

14
src/AbpDesk/AbpDesk.Application/AbpDesk/AbpDeskApplicationModuleAutoMapperProfile.cs

@ -0,0 +1,14 @@
using AbpDesk.Tickets;
using AbpDesk.Tickets.Dtos;
using AutoMapper;
namespace AbpDesk
{
public class AbpDeskApplicationModuleAutoMapperProfile : Profile
{
public AbpDeskApplicationModuleAutoMapperProfile()
{
CreateMap<Ticket, TicketDto>();
}
}
}

30
src/AbpDesk/AbpDesk.Application/AbpDesk/Tickets/TicketAppService.cs

@ -1,6 +1,8 @@
using System.Linq; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using AbpDesk.Tickets.Dtos; using AbpDesk.Tickets.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Services.Dtos; using Volo.Abp.Application.Services.Dtos;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Linq; using Volo.Abp.Linq;
@ -9,15 +11,14 @@ using Volo.ExtensionMethods;
namespace AbpDesk.Tickets namespace AbpDesk.Tickets
{ {
public class TicketAppService : ITicketAppService public class TicketAppService : ApplicationService, ITicketAppService
{ {
private readonly IQueryableRepository<Ticket, int> _ticketRepository; private readonly IQueryableRepository<Ticket, int> _ticketRepository;
private readonly IAsyncQueryableExecuter _asyncQueryableExecuter; private readonly IAsyncQueryableExecuter _asyncQueryableExecuter;
public TicketAppService( public TicketAppService(
IQueryableRepository<Ticket, int> ticketRepository, IQueryableRepository<Ticket, int> ticketRepository,
IAsyncQueryableExecuter asyncQueryableExecuter IAsyncQueryableExecuter asyncQueryableExecuter)
)
{ {
_ticketRepository = ticketRepository; _ticketRepository = ticketRepository;
_asyncQueryableExecuter = asyncQueryableExecuter; _asyncQueryableExecuter = asyncQueryableExecuter;
@ -30,14 +31,11 @@ namespace AbpDesk.Tickets
!input.Filter.IsNullOrWhiteSpace(), !input.Filter.IsNullOrWhiteSpace(),
t => t.Title.Contains(input.Filter) || t.Body.Contains(input.Filter) t => t.Title.Contains(input.Filter) || t.Body.Contains(input.Filter)
) )
.Select(t => new TicketDto );
{
Id = t.Id,
Title = t.Title,
Body = t.Body
}));
return new ListResultDto<TicketDto>(tickets); return new ListResultDto<TicketDto>(
ObjectMapper.Map<List<Ticket>, List<TicketDto>>(tickets)
);
} }
public ListResultDto<TicketDto> GetAll2(GetAllTicketsInput input) public ListResultDto<TicketDto> GetAll2(GetAllTicketsInput input)
@ -47,15 +45,11 @@ namespace AbpDesk.Tickets
!input.Filter.IsNullOrWhiteSpace(), !input.Filter.IsNullOrWhiteSpace(),
t => t.Title.Contains(input.Filter) || t.Body.Contains(input.Filter) t => t.Title.Contains(input.Filter) || t.Body.Contains(input.Filter)
) )
.Select(t => new TicketDto
{
Id = t.Id,
Title = t.Title,
Body = t.Body
})
.ToList(); .ToList();
return new ListResultDto<TicketDto>(tickets); return new ListResultDto<TicketDto>(
ObjectMapper.Map<List<Ticket>, List<TicketDto>>(tickets)
);
} }
} }
} }

2
src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs

@ -98,7 +98,7 @@ namespace Volo.Abp.AutoMapper
foreach (var type in types) foreach (var type in types)
{ {
logger.LogDebug(type.FullName); logger.LogDebug(type.FullName);
context.MapperConfigurationExpression.CreateAutoAttributeMaps(type); context.MapperConfiguration.CreateAutoAttributeMaps(type);
} }
} }

6
src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs

@ -19,21 +19,21 @@ namespace Volo.Abp.AutoMapper
public interface IAbpAutoMapperConfigurationContext public interface IAbpAutoMapperConfigurationContext
{ {
IMapperConfigurationExpression MapperConfigurationExpression { get; } IMapperConfigurationExpression MapperConfiguration { get; }
IServiceProvider ServiceProvider { get; } IServiceProvider ServiceProvider { get; }
} }
public class AbpAutoMapperConfigurationContext : IAbpAutoMapperConfigurationContext public class AbpAutoMapperConfigurationContext : IAbpAutoMapperConfigurationContext
{ {
public IMapperConfigurationExpression MapperConfigurationExpression { get; } public IMapperConfigurationExpression MapperConfiguration { get; }
public IServiceProvider ServiceProvider { get; } public IServiceProvider ServiceProvider { get; }
public AbpAutoMapperConfigurationContext( public AbpAutoMapperConfigurationContext(
IMapperConfigurationExpression mapperConfigurationExpression, IMapperConfigurationExpression mapperConfigurationExpression,
IServiceProvider serviceProvider) IServiceProvider serviceProvider)
{ {
MapperConfigurationExpression = mapperConfigurationExpression; MapperConfiguration = mapperConfigurationExpression;
ServiceProvider = serviceProvider; ServiceProvider = serviceProvider;
} }
} }

2
src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs

@ -15,6 +15,8 @@ namespace Autofac.Builder
IServiceCollection services) IServiceCollection services)
where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData
{ {
registrationBuilder = registrationBuilder.PropertiesAutowired();
var serviceType = registrationBuilder.RegistrationData.Services.OfType<IServiceWithType>().FirstOrDefault()?.ServiceType; var serviceType = registrationBuilder.RegistrationData.Services.OfType<IServiceWithType>().FirstOrDefault()?.ServiceType;
if (serviceType == null) if (serviceType == null)
{ {

1
src/Volo.Abp.Identity.Application/Volo.Abp.Identity.Application.csproj

@ -12,6 +12,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
<ProjectReference Include="..\Volo.Abp.Identity\Volo.Abp.Identity.csproj" /> <ProjectReference Include="..\Volo.Abp.Identity\Volo.Abp.Identity.csproj" />
<ProjectReference Include="..\Volo.Abp.Identity.Application.Contracts\Volo.Abp.Identity.Application.Contracts.csproj" /> <ProjectReference Include="..\Volo.Abp.Identity.Application.Contracts\Volo.Abp.Identity.Application.Contracts.csproj" />
</ItemGroup> </ItemGroup>

12
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpDeskApplicationModuleAutoMapperProfile.cs

@ -0,0 +1,12 @@
using AutoMapper;
namespace Volo.Abp.Identity
{
public class AbpIdentityApplicationModuleAutoMapperProfile : Profile
{
public AbpIdentityApplicationModuleAutoMapperProfile()
{
CreateMap<IdentityUser, IdentityUserDto>();
}
}
}

11
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/AbpIdentityApplicationModule.cs

@ -1,14 +1,23 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
[DependsOn(typeof(AbpIdentityModule), typeof(AbpIdentityApplicationContractsModule))] [DependsOn(typeof(AbpIdentityModule), typeof(AbpIdentityApplicationContractsModule), typeof(AbpAutoMapperModule))]
public class AbpIdentityApplicationModule : AbpModule public class AbpIdentityApplicationModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
services.AddAssemblyOf<AbpIdentityApplicationModule>(); services.AddAssemblyOf<AbpIdentityApplicationModule>();
services.Configure<AbpAutoMapperOptions>(options =>
{
options.Configurators.Add(context =>
{
context.MapperConfiguration.AddProfile<AbpIdentityApplicationModuleAutoMapperProfile>();
});
});
} }
} }
} }

25
src/Volo.Abp.Identity.Application/Volo/Abp/Identity/UserAppService.cs

@ -1,13 +1,14 @@
using System; using System;
using System.Linq; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Services.Dtos; using Volo.Abp.Application.Services.Dtos;
namespace Volo.Abp.Identity namespace Volo.Abp.Identity
{ {
//TODO: Consider a way of passing cancellation token to all async application service methods! //TODO: Consider a way of passing cancellation token to all async application service methods!
public class UserAppService : IUserAppService public class UserAppService : ApplicationService, IUserAppService
{ {
private readonly IIdentityUserRepository _userRepository; private readonly IIdentityUserRepository _userRepository;
@ -18,28 +19,18 @@ namespace Volo.Abp.Identity
public async Task<ListResultDto<IdentityUserDto>> GetAll() public async Task<ListResultDto<IdentityUserDto>> GetAll()
{ {
var users = (await _userRepository.GetListAsync()) var users = await _userRepository.GetListAsync();
.Select(u => new IdentityUserDto
{
Id = u.Id,
Email = u.Email,
UserName = u.UserName
})
.ToList();
return new ListResultDto<IdentityUserDto>(users); return new ListResultDto<IdentityUserDto>(
ObjectMapper.Map<List<IdentityUser>, List<IdentityUserDto>>(users)
);
} }
public async Task<IdentityUserDto> Get(Guid id) public async Task<IdentityUserDto> Get(Guid id)
{ {
var user = await _userRepository.GetAsync(id); var user = await _userRepository.GetAsync(id);
return new IdentityUserDto return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
{
Id = user.Id,
UserName = user.UserName,
Email = user.Email
};
} }
} }
} }

9
src/Volo.Abp/Volo/Abp/Application/Services/ApplicationService.cs

@ -0,0 +1,9 @@
using Volo.Abp.ObjectMapping;
namespace Volo.Abp.Application.Services
{
public abstract class ApplicationService : IApplicationService
{
public IObjectMapper ObjectMapper { get; set; }
}
}
Loading…
Cancel
Save