Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable AutoMapper dependency injection to load all profiles #1264 #1306

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -25,6 +25,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
using Device = Portal.Domain.Entities.Device;
using Microsoft.EntityFrameworkCore;
using Portal.Domain.Entities;
using AutoMapper;

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

this.deviceService = Services.GetRequiredService<IDeviceService<DeviceDetails>>();
Mapper = Services.GetRequiredService<IMapper>();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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.Exceptions;
using AzureIoTHub.Portal.Server.Services;
Expand Down Expand Up @@ -43,6 +44,7 @@ public override void Setup()
Services = ServiceCollection.BuildServiceProvider();

this.deviceTagService = Services.GetRequiredService<IDeviceTagService>();
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