Skip to content

Commit

Permalink
Enable AutoMapper dependency injection to load all profiles #1264
Browse files Browse the repository at this point in the history
  • Loading branch information
hocinehacherouf committed Oct 3, 2022
1 parent 7992143 commit 5614981
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
using System.Linq;
using System.Threading.Tasks;
using AutoFixture;
using AutoMapper;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Server.Services;
using FluentAssertions;
Expand Down Expand Up @@ -67,6 +68,7 @@ public override void Setup()
Services = ServiceCollection.BuildServiceProvider();

this.deviceModelService = Services.GetRequiredService<IDeviceModelService<DeviceModelDto, DeviceModelDto>>();
Mapper = Services.GetRequiredService<IMapper>();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
using System.Linq;
using System.Threading.Tasks;
using AutoFixture;
using AutoMapper;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Domain.Exceptions;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void SetUp()
Services = ServiceCollection.BuildServiceProvider();

this.edgeDeviceModelService = Services.GetRequiredService<IEdgeModelService>();
Mapper = Services.GetRequiredService<IMapper>();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Domain.Services
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using AutoMapper;
using Azure;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Domain;
Expand Down Expand Up @@ -79,6 +80,7 @@ public override void Setup()
Services = ServiceCollection.BuildServiceProvider();

this.loRaWanCommandService = Services.GetRequiredService<ILoRaWANCommandService>();
Mapper = Services.GetRequiredService<IMapper>();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Devices;
using Microsoft.EntityFrameworkCore;
using AutoMapper;

[TestFixture]
public class LoRaWanDeviceServiceTests : BackendUnitTest
Expand Down Expand Up @@ -64,6 +65,7 @@ public override void Setup()
Services = ServiceCollection.BuildServiceProvider();

this.lorawanDeviceService = Services.GetRequiredService<IDeviceService<LoRaDeviceDetails>>();
Mapper = Services.GetRequiredService<IMapper>();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases
{
using System;
using System.Reflection;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
Expand All @@ -13,6 +14,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases
using Moq;
using NUnit.Framework;
using Portal.Infrastructure;
using Portal.Server;
using Portal.Server.Mappers;
using RichardSzalay.MockHttp;

Expand Down Expand Up @@ -47,18 +49,8 @@ public virtual void Setup()
httpClient.BaseAddress = new Uri("http://fake.local");
_ = ServiceCollection.AddSingleton(httpClient);

// Add Mapper Configuration
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new DeviceTagProfile());
mc.AddProfile(new EdgeDeviceModelProfile());
mc.AddProfile(new EdgeDeviceModelCommandProfile());
mc.AddProfile(new DeviceModelProfile());
mc.AddProfile(new DeviceModelCommandProfile());
mc.AddProfile(new DeviceProfile());
});
Mapper = mappingConfig.CreateMapper();
_ = ServiceCollection.AddSingleton(Mapper);
// Add AutoMapper Configuration
_ = ServiceCollection.AddAutoMapper(typeof(Startup));

// Add InMemory Database
var contextOptions = new DbContextOptionsBuilder<PortalDbContext>()
Expand Down
17 changes: 17 additions & 0 deletions src/AzureIoTHub.Portal/Server/Mappers/EnumerableProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Mappers
{
using System.Collections.Generic;
using AutoMapper;
using Azure;

public class EnumerableProfile : Profile
{
public EnumerableProfile()
{
_ = CreateMap(typeof(AsyncPageable<>), typeof(List<>));
}
}
}
36 changes: 10 additions & 26 deletions src/AzureIoTHub.Portal/Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
namespace AzureIoTHub.Portal.Server
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using Azure;
using Azure.Storage.Blobs;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Domain.Repositories;
using AzureIoTHub.Portal.Infrastructure;
using AzureIoTHub.Portal.Infrastructure.Factories;
using AzureIoTHub.Portal.Infrastructure.Repositories;
using AzureIoTHub.Portal.Infrastructure.Seeds;
using AzureIoTHub.Portal.Server.Jobs;
using Domain;
using Domain.Exceptions;
using Domain.Repositories;
using Extensions;
using Hellang.Middleware.ProblemDetails;
using Hellang.Middleware.ProblemDetails.Mvc;
using Identity;
using Infrastructure;
using Infrastructure.Factories;
using Infrastructure.Repositories;
using Infrastructure.Seeds;
using Jobs;
using Managers;
using Mappers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -274,21 +271,8 @@ Specify the authorization token got from your IDP as a header.
new HeaderApiVersionReader("X-Version"));
});

var mapperConfig = new MapperConfiguration(mc =>
{
_ = mc.CreateMap(typeof(AsyncPageable<>), typeof(List<>));

mc.AddProfile(new DevicePropertyProfile());
mc.AddProfile(new DeviceTagProfile());
mc.AddProfile(new EdgeDeviceModelProfile());
mc.AddProfile(new EdgeDeviceModelCommandProfile());
mc.AddProfile(new DeviceModelProfile());
mc.AddProfile(new DeviceModelCommandProfile());
mc.AddProfile(new DeviceProfile());
});

var mapper = mapperConfig.CreateMapper();
_ = services.AddSingleton(mapper);
// Add AutoMapper Configuration
_ = services.AddAutoMapper(typeof(Startup));

_ = services.AddHealthChecks()
.AddDbContextCheck<PortalDbContext>()
Expand Down

0 comments on commit 5614981

Please sign in to comment.