Hi I am using AutoMapper to move from a Model to a Dto and it's working great.
In one TypeConverter
I need to inject an Interface (a service) that has to be used by the type converter in order to do the conversion.
How can I accomplish this in AutoMapper?
Can you not just create a constructor on your TypeConverter
class, accepting the service? Rather than using the generic ConvertUsing
, pass in a new instance of your TypeConverter
constructed with the service...
public class MyTypeConverter : TypeConverter<String, String>
{
public MyTypeConverter(IMyService service)
{
MyService = service;
}
public IMyService MyService { get; set; }
protected override string ConvertCore(string source)
{
//use service
}
}
Usage:
Mapper.CreateMap<string, string>()
.ConvertUsing(new MyTypeConverter(_myService));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With