29 September 2023
by
Tran Manh Hung
From Converters to Dependency Injection: Navigating Model Migrations
csharp
design-patterns
Do you know the three-layer software design? Think of it as tech's equivalent of a sandwich with neatly stacked presentation, logic, and data layers. Now, why choose multiple models over one mammoth model? It's akin to organizing playlists by mood. Nifty, isn't it? In the coding realm, it's about being spick and span. Tiny model tweaks feel more like a brisk walk than a marathon.
But the real query is: how do we flit between models within these layers, especially when models diverge or beckon another repo or service?
Well, Examples Speak Louder
Keep It Simple
Suppose a Dog is set to evolve into a DogDto.
1public class Dog
2{
3 public string Name { get; set; }
4}
5
6public class DogDto
7{
8 public string Name { get; set; }
9}
Seems direct, right? Either use AutoMapper:
1Dog dog = new Dog{ Name = "doge"};
2DogDto dogDto = _mapper.Map<DogDto>(dog);
Or, take the traditional route:
1Dog dog = new Dog{ Name = "doge"};
2DogDto dogDto = new DogDto { Name = dog.Name };
Navigating Naming Nuisances
What if DogDto opts for a different nomenclature?
1public class Dog
2{
3 public string Name { get; set; }
4}
5
6public class DogDto
7{
8 public string Naming { get; set; }
9}
You've got two aces up your sleeve: use a FromXX/toXX method or integrate a mapper profile. Here's option one:
1public class DogDto
2{
3 public string Naming { get; set; }
4
5 public static DogDto FromDog(Dog dog)
6 {
7 return new DogDto
8 {
9 Naming = dog.Name
10 };
11 }
12}
And here you have how would mapper profile look like:
1using AutoMapper;
2
3public class UserProfile : Profile
4{
5 public UserProfile()
6 {
7 CreateMap<Dog, DogDto>()
8 .ForMember(dest => dest.Naming, opt => opt.MapFrom(src => src.Name));
9 }
10}
1var configuration = new MapperConfiguration(cfg =>
2{
3 cfg.AddProfile<UserProfile>();
4});
When Models are Like Apples and Oranges
1public class Dog
2{
3 public string Name { get; set; }
4}
5
6public class DogDto
7{
8 public int NumberOfLegs { get; set; }
9
10 public Superpower SuperPower { get; set; }
11}
Time to roll up the sleeves! Converters are the knights in shining armor here:
1public interface IConverter<In, Out>
2{
3 Out Convert(In input);
4}
5
6public class DogConverter : IConverter<Dog, DogDto>
7{
8 // Your conversion magic here!
9}
Do you need a reliable partner in tech for your next project?
Elevate Your Game: Services Within Converters
Ever bumped into a situation where your models are as different as, well, apples and oranges? And, to spice things up, you need to juggle an
AnimalRepository
or a FoodService
within your converter. Tough spot, right?Remember this code?
1var converter = new DogConverter(_animalRepository, _foodService, _anotherService, _pleaseStopThisIsTooMuch, userId);
It might remind you of the infamous spaghetti mess. But fear not, there's a cleaner approach. Let me share a neat trick I've brewed up!
Step into the World of ConverterFactory
A
ConverterFactory
is like a genie granting your converter wishes.1public class ConverterFactory
2{
3 private readonly IServiceProvider _serviceProvider;
4
5 public ConverterFactory(IServiceProvider serviceProvider)
6 {
7 _serviceProvider = serviceProvider;
8 }
9
10 public IConverter<InType, OutType> GetConverter<InType, OutType>()
11 {
12 var converter = _serviceProvider.GetService(typeof(IConverter<InType, OutType>));
13 if (converter == null)
14 {
15 throw new Exception("Missing converter alert!");
16 }
17
18 return (IConverter<InType, OutType>)converter;
19 }
20}
Boot Things Up in Startup.cs
Here's where we align our forces. Add your services to the service collection:
1services.AddTransient<ConverterFactory>();
2services.AddTransient<IConverter<Dog, DogDto>, DogConverter>();
3services.AddTransient<IAnimalRepository, AnimalRepository>();
4services.AddTransient<IFoodService, FoodService>();
Smooth Operations with DogHelper
With everything set, it's showtime:
1public class DogHelper
2{
3 private readonly ConverterFactory _converterFactory;
4 public DogHelper(ConverterFactory converterFactory)
5 {
6 _converterFactory = converterFactory;
7 }
8
9///...
10 public DogDto TransformDog(Dog input)
11 {
12 var converter = _converterFactory.GetConverter<Dog, DogDto>();
13 return converter.Convert(input);
14 }
15}
What's the magic? No more manually creating converter instances and wrestling with services. Dependency injection sweeps in, handling all the heavy lifting ensuring cleaner and more efficient code.
Wrapping Up: A Fresh Look at Tools and Tactics
Is Automapper Outdated? Let's Debate
Let me drop a bombshell: Could Automapper be sliding into obsolescence? I recently found myself locked in a spirited dialogue with a colleague about its continuing relevance. The crux of the debate? Automapper's Achilles' heel is its runtime error reporting. Get a property name or type conversion wrong, and you're signing up for bug city.
Sure, Automapper shone when replicating models that were virtually identical, especially those property-heavy behemoths we often see in enterprise settings. But let’s be real: Visual Studio's ultra-efficient auto-code completion has redefined the game, making that argument a little wobbly.
ConverterFactory: A Quick Fix, But Not a Cure-All
Time to tackle the not-so-hidden issue: If you find yourself leaning heavily on something like a ConverterFactory, coupled with dependency injection, it might be time to sniff out potential architectural odors. Could it be a red flag suggesting you revisit your system’s blueprints?
The Final Takeaway
So, there we are! Whether you're nodding along or vigorously shaking your head, my hope is that this exploration has sparked some intellectual kindling. Got a different approach? Differing opinions? Lay it on me. Let's remember that achieving perfection is less a destination and more an ever-evolving journey. There's always room for development and refinement in the ever-changing tech landscape. So go ahead, share your insights—I'm more than ready for a good round to disscuss!
Let’s stay connected
Do you want the latest and greatest from our blog straight to your inbox? Chuck us your email address and get informed.
You can unsubscribe any time. For more details, review our privacy policy