❔ ✅ custom mapper and resolvers

Hello, I've been trying to use automapper for a while, but now I've decided to just replace it with my own mapping methods, I've created static extension class, but there is one problem with it. Mapping class is now static, but my resolvers classed need to dependency inject services to work, how I am supposed to call my resolver in static class? MappingExtensions:
public static class MappingExtensions
{
public static PublicAd MapToPublicAd(this Ad ad, User user)
{
return new PublicAd
{
Claimed = ad.Claimed,
Distance = // I don't know
};
}
...
public static class MappingExtensions
{
public static PublicAd MapToPublicAd(this Ad ad, User user)
{
return new PublicAd
{
Claimed = ad.Claimed,
Distance = // I don't know
};
}
...
DistanceResolver:
public class DistanceResolver
{
private readonly IDistanceService _distanceService;

public DistanceResolver(IDistanceService distanceService)
{
_distanceService = distanceService;
}

public double Resolve(Ad ad, User user)
{
if (user is null)
{
throw new ArgumentException($"No valid user passed to");
}

if (user.Municipality is null || source.Municipality is null)
{
return -1d;
}

return _distanceService.GetDistanceInKm(
ad.Municipality.Location!.Y,
ad.Municipality.Location!.X,
user.Municipality.Location!.Y,
user.Municipality.Location!.X
);
}
}
public class DistanceResolver
{
private readonly IDistanceService _distanceService;

public DistanceResolver(IDistanceService distanceService)
{
_distanceService = distanceService;
}

public double Resolve(Ad ad, User user)
{
if (user is null)
{
throw new ArgumentException($"No valid user passed to");
}

if (user.Municipality is null || source.Municipality is null)
{
return -1d;
}

return _distanceService.GetDistanceInKm(
ad.Municipality.Location!.Y,
ad.Municipality.Location!.X,
user.Municipality.Location!.Y,
user.Municipality.Location!.X
);
}
}
10 Replies
Jothay
Jothay2y ago
The code calling the MapToPublicAd method can pass in the IDistanceResolver as an arg for it to call, or you'll have to call it outside the func and then pass in the value for the Distance prop public static PublicAd MapToPublicAd(this Ad ad, User user, IDistanceResolver resolver) Distance = resolver.Resolve(ad, user) public static PublicAd MapToPublicAd(this Ad ad, User user, decimal distance) Distance = distance I usually pull those parts of maps out of the mapping class and just do them after-the-fact Let the mapper handle the parts that directly read off of data, and calculated equations to a separate action
Scottek utek
Scottek utekOP2y ago
but that would mean fill other properties every time after mapping is done
Jothay
Jothay2y ago
Which is the trade-off you have to decide you would rather deal with, one or the other. Which one is more convenient to you?
Scottek utek
Scottek utekOP2y ago
I'll probably take IDistanceResolver as a paramether
Jothay
Jothay2y ago
And I would accept that as a valid solution if it were me
Scottek utek
Scottek utekOP2y ago
ok, thanks for help 😊
Scottek utek
Scottek utekOP2y ago
gone wrong
Jothay
Jothay2y ago
ok so the number of resolvers to send gets unwieldy
Scottek utek
Scottek utekOP2y ago
it looks funny but it's actually ok
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server