Skip to content

Commit

Permalink
Remove automapper for mapperly source generated model mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackburn29 committed Mar 4, 2024
1 parent 498fc83 commit 17ae68f
Show file tree
Hide file tree
Showing 25 changed files with 219 additions and 233 deletions.
8 changes: 4 additions & 4 deletions src/Fragment.NetSlum.Core/Fragment.NetSlum.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="8.0.2" />
<PackageReference Include="IPNetwork2" Version="2.6.618">
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="IPNetwork2" Version="3.0.665">
<Aliases>IPNetwork2</Aliases>
</PackageReference>
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0-beta.2" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Fragment.NetSlum.Networking.Packets.Response.Mail;
using Fragment.NetSlum.Networking.Sessions;
using Fragment.NetSlum.Persistence;
using Microsoft.EntityFrameworkCore;

namespace Fragment.NetSlum.Networking.Packets.Request.Mail;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Threading.Tasks;
using Fragment.NetSlum.Networking.Attributes;
using Fragment.NetSlum.Networking.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Fragment.NetSlum.Networking.Attributes;
using Fragment.NetSlum.Networking.Constants;
using Fragment.NetSlum.Networking.Objects;
using Fragment.NetSlum.Networking.Packets.Response.Character;
using Fragment.NetSlum.Networking.Packets.Response.Mail;
using Fragment.NetSlum.Networking.Sessions;
using Fragment.NetSlum.Persistence;
Expand Down
1 change: 0 additions & 1 deletion src/Fragment.NetSlum.Persistence/Entities/BannedIp.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using Microsoft.EntityFrameworkCore;

namespace Fragment.NetSlum.Persistence.Entities;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using Microsoft.EntityFrameworkCore;

namespace Fragment.NetSlum.Persistence.Entities;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using AutoMapper;
using Fragment.NetSlum.Networking.Sessions;
using Fragment.NetSlum.Server.Api.Models;
using Fragment.NetSlum.Server.Mappings;
using Fragment.NetSlum.TcpServer;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -12,12 +12,10 @@ namespace Fragment.NetSlum.Server.Api.Controllers;
public class AreaServersController : ControllerBase
{
private readonly ITcpServer _gameServer;
private readonly IMapper _mapper;

public AreaServersController(ITcpServer gameServer, IMapper mapper)
public AreaServersController(ITcpServer gameServer)
{
_gameServer = gameServer;
_mapper = mapper;
}

/// <summary>
Expand All @@ -29,12 +27,17 @@ public IEnumerable<AreaServerStatus> GetOnlineAreaServers()
{
foreach (var client in _gameServer.Sessions)
{
if (client is not FragmentTcpSession session || !session.IsAreaServer)
if (client is not FragmentTcpSession { IsAreaServer: true } session)
{
continue;
}

yield return _mapper.Map<AreaServerStatus>(session.AreaServerInfo);
if (session.AreaServerInfo == null)
{
continue;
}

yield return AreaServerMapper.Map(session.AreaServerInfo);
}
}
}
12 changes: 5 additions & 7 deletions src/Fragment.NetSlum.Server/Api/Controllers/GuildsController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Fragment.NetSlum.Persistence;
using Fragment.NetSlum.Persistence.Extensions;
using Fragment.NetSlum.Server.Api.Models;
using Fragment.NetSlum.Server.Mappings;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

Expand All @@ -14,12 +14,10 @@ namespace Fragment.NetSlum.Server.Api.Controllers;
public class GuildsController
{
private readonly FragmentContext _database;
private readonly IMapper _mapper;

public GuildsController(FragmentContext database, IMapper mapper)
public GuildsController(FragmentContext database)
{
_database = database;
_mapper = mapper;
}

/// <summary>
Expand All @@ -40,7 +38,7 @@ public PagedResult<GuildInfo> GetAllGuilds(int page = 1, int pageSize = 50)

var guildCount = _database.Guilds.Count();

return new PagedResult<GuildInfo>(page, pageSize, guildCount, _mapper.Map<List<GuildInfo>>(guilds.ToList()));
return new PagedResult<GuildInfo>(page, pageSize, guildCount, guilds.Select(g => GuildMapper.Map(g)).ToList());
}

/// <summary>
Expand All @@ -57,7 +55,7 @@ public PagedResult<GuildInfo> GetAllGuilds(int page = 1, int pageSize = 50)
.Include(g => g.Members)
.FirstOrDefault(g => g.Id == guildId);

return _mapper.Map<GuildInfo>(guild);
return guild == null ? null : GuildMapper.Map(guild);
}

/// <summary>
Expand All @@ -75,7 +73,7 @@ public IEnumerable<PlayerInfo> GetGuildMembers(int guildId)

foreach (var member in members)
{
yield return _mapper.Map<PlayerInfo>(member);
yield return CharacterMapper.Map(member);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using AutoMapper;
using Fragment.NetSlum.Networking.Stores;
using Fragment.NetSlum.Server.Api.Models;
using Fragment.NetSlum.Server.Mappings;
using Microsoft.AspNetCore.Mvc;

namespace Fragment.NetSlum.Server.Api.Controllers;
Expand All @@ -11,12 +11,10 @@ namespace Fragment.NetSlum.Server.Api.Controllers;
public class LobbiesController : ControllerBase
{
private readonly ChatLobbyStore _lobbyStore;
private readonly IMapper _mapper;

public LobbiesController(ChatLobbyStore lobbyStore, IMapper mapper)
public LobbiesController(ChatLobbyStore lobbyStore)
{
_lobbyStore = lobbyStore;
_mapper = mapper;
}

/// <summary>
Expand All @@ -28,7 +26,7 @@ public IEnumerable<Lobby> GetAllLobbies()
{
foreach (var lobby in _lobbyStore.ChatLobbies)
{
yield return _mapper.Map<Lobby>(lobby);
yield return LobbyMapper.Map(lobby);
}
}
}
21 changes: 12 additions & 9 deletions src/Fragment.NetSlum.Server/Api/Controllers/PlayersController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Fragment.NetSlum.Networking.Sessions;
using Fragment.NetSlum.Persistence;
using Fragment.NetSlum.Persistence.Entities;
using Fragment.NetSlum.Persistence.Extensions;
using Fragment.NetSlum.Server.Api.Models;
using Fragment.NetSlum.Server.Mappings;
using Fragment.NetSlum.TcpServer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
Expand All @@ -18,13 +18,11 @@ public class PlayersController : ControllerBase
{
private readonly ITcpServer _gameServer;
private readonly FragmentContext _database;
private readonly IMapper _mapper;

public PlayersController(ITcpServer gameServer, FragmentContext database, IMapper mapper)
public PlayersController(ITcpServer gameServer, FragmentContext database)
{
_gameServer = gameServer;
_database = database;
_mapper = mapper;
}

/// <summary>
Expand All @@ -47,9 +45,11 @@ public PagedResult<PlayerInfo> GetAllPlayers(int page = 1, int pageSize = 50, st

var characterCount = characters.Count();

var results = characters.Paginate(page, pageSize);
var results = characters.Paginate(page, pageSize)
.Select(r => CharacterMapper.Map(r))
.ToList();

return new PagedResult<PlayerInfo>(page, pageSize, characterCount, _mapper.Map<List<PlayerInfo>>(results));
return new PagedResult<PlayerInfo>(page, pageSize, characterCount, results);
}

/// <summary>
Expand Down Expand Up @@ -84,7 +84,7 @@ public IEnumerable<Client> GetOnlinePlayers()
.Include(p => p.CharacterStats)
.FirstOrDefault(p => p.Id == characterId);

return _mapper.Map<PlayerInfo>(player);
return player == null ? null : CharacterMapper.Map(player);
}

/// <summary>
Expand All @@ -101,7 +101,10 @@ public IEnumerable<PlayerInfo> GetAccountPlayerInfos(int accountId)
.Include(p => p.CharacterStats)
.Where(p => p.PlayerAccountId == accountId);

return _mapper.Map<IEnumerable<PlayerInfo>>(players);
foreach (var player in players)
{
yield return CharacterMapper.Map(player);
}
}

/// <summary>
Expand All @@ -120,7 +123,7 @@ public IEnumerable<PlayerStats> GetPlayerStatHistory(int characterId)

foreach (var stats in playerStats)
{
yield return _mapper.Map<PlayerStats>(stats);
yield return CharacterMapper.Map(stats);
}
}
}
1 change: 0 additions & 1 deletion src/Fragment.NetSlum.Server/Converters/ImageConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Fragment.NetSlum.Core.Extensions;
using Fragment.NetSlum.Core.Models;
using ImageMagick;
Expand Down
13 changes: 6 additions & 7 deletions src/Fragment.NetSlum.Server/Fragment.NetSlum.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,21 @@
<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.System" Version="8.0.0" />
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.0" />
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Haukcode.HighResolutionTimer" Version="1.1.0" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="NSwag.AspNetCore" Version="14.0.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="NSwag.AspNetCore" Version="14.0.3" />
<PackageReference Include="Riok.Mapperly" Version="3.4.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoMapper;
using Fragment.NetSlum.Core.CommandBus.Contracts.Commands;
using Fragment.NetSlum.Networking.Commands.Characters;
using Fragment.NetSlum.Persistence;
using Fragment.NetSlum.Persistence.Entities;
using Fragment.NetSlum.Server.Mappings;
using Microsoft.EntityFrameworkCore;

namespace Fragment.NetSlum.Server.Handlers.Character;

public class RegisterCharacterCommandHandler : CommandHandler<RegisterCharacterCommand, Persistence.Entities.Character>
{
private readonly FragmentContext _database;
private readonly IMapper _mapper;

public RegisterCharacterCommandHandler(FragmentContext database, IMapper mapper)
public RegisterCharacterCommandHandler(FragmentContext database)
{
_database = database;
_mapper = mapper;
}

public override async Task<Persistence.Entities.Character> Handle(RegisterCharacterCommand command, CancellationToken cancellationToken)
Expand All @@ -36,7 +34,7 @@ public RegisterCharacterCommandHandler(FragmentContext database, IMapper mapper)

PlayerAccount? playerAccount = await _database.PlayerAccounts.FirstOrDefaultAsync(p => p.SaveId == characterInfo.SaveId, cancellationToken);

character = _mapper.Map(characterInfo, character!);
character = CharacterInfoMapper.MapOrCreate(characterInfo, character);

// If a player account was found when looking up the save ID, use that player account instead of persisting a new one
if (playerAccount != null)
Expand Down
13 changes: 13 additions & 0 deletions src/Fragment.NetSlum.Server/Mappings/AreaServerMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Fragment.NetSlum.Networking.Models;
using Fragment.NetSlum.Server.Api.Models;
using Riok.Mapperly.Abstractions;

namespace Fragment.NetSlum.Server.Mappings;

[Mapper]
public partial class AreaServerMapper
{
[MapProperty(nameof(AreaServerInformation.ServerName), nameof(AreaServerStatus.Name))]
[MapProperty(nameof(AreaServerInformation.ActiveSince), nameof(AreaServerStatus.OnlineSince))]
public static partial AreaServerStatus Map(AreaServerInformation input);
}
19 changes: 0 additions & 19 deletions src/Fragment.NetSlum.Server/Mappings/AreaServerProfile.cs

This file was deleted.

Loading

0 comments on commit 17ae68f

Please sign in to comment.