0

I have ASP.Net Core 2.1 C# application.

Below is my DTO

public class CustomerTO
{
 public int Id { get; set;}

 public DateTime SomeDate{get; set;}
}

Domain Class

public class Customer
{
 public int Id { get; set;}

 public string SomeDate{get; set;}
}

AutoMapper Class

public class ObjectsMapper
{
    private readonly IMapper _mapper;
    private const string _dateFormat = "yyyy’-‘MM’-‘dd’T’HH’:’mm’";

    public ObjectsMapper()
    {
        var config = new MapperConfiguration(cfg =>
        {

            cfg.CreateMap<Customer, CustomerTO>()

                .ForMember(dest => dest.SomeDate.ToString(_dateFormat), opt => opt.MapFrom(src => src.SomeDate))
                .ForMember(dest => dest.SomeDate.ToString(_dateFormat), opt => opt.MapFrom(src => src.SomeDate));

            cfg.CreateMap<CustomerTO, Customer>()

                   .ForMember(dest => dest.SomeDate, opt => opt.MapFrom(src => Convert.ToDateTime(src.SomeDate)))
                   .ForMember(dest => dest.SomeDate, opt => opt.MapFrom(src => Convert.ToDateTime(src.SomeDate)));

        });

        _mapper = config.CreateMapper();
    }

    public User Map(CustomerTO user) => _mapper.Map<CustomerTO, Customer>(customer);
}

But when the Map() is called from respective places, either it throws the below error or just stops the debugging/application with no error or output

[AutoMapperConfigurationException: Custom configuration for members is only supported for top-level individual members on a type.]

How do I handle this mapper (DateTime to String and vice-versa)?

Thanks!

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • That's not mapping, that's conversion. An unfortunate one too - a DTO could be allowed to use a string as a date for serialization reasons, given strong control of the serialization process and format but a *domain* entity shouldn't do that. It should use the appropriate type – Panagiotis Kanavos Oct 09 '19 at 09:49
  • why do you need to convert to string and vice-versa? what's the point? can you provide us a real case example? – imnotaduck Oct 09 '19 at 09:51
  • @PanagiotisKanavos, Here Domain is not linked to RDBMS but NoSQL, the NoSQL that I am using has nothing as DateTime – Kgn-web Oct 09 '19 at 09:51
  • @Kgn-web and obviously the code you use to deserialize the DTO has a way to convert that string to a DateTime already. The *domain* entity shouldn't use a string at all. – Panagiotis Kanavos Oct 09 '19 at 09:52
  • @rm.szc81, App would have Calendar in UI, any date that is coming from UI should store the date in the speficed format in NoSql – Kgn-web Oct 09 '19 at 09:52
  • @Kgn-web did you mix up the DTO and Domain labels perhaps? `CustomerTO` is is stored in the database and yet has a `DateTime` property. `Customer` is created from the DTO and shouldn't care what the *database* wants. You shouldn't have to map anything – Panagiotis Kanavos Oct 09 '19 at 09:53
  • I agree With @PanagiotisKanavos. The way the data is stored has nothing to do with the way it's displayed to the user. That's why I asked you what you are trying to do. – imnotaduck Oct 09 '19 at 09:56
  • @PanagiotisKanavos Sir, I know what I am doing & very well aware of why I am doing. Suppose I needed to do how can it mapped/converted. Can you please add share your thought on it ? – Kgn-web Oct 09 '19 at 09:59
  • @Kgn-web the destination lambda specifies the *destination* property. Transformations go in the `MapFrom` lambda. `Convert.ToDateTime` is just a call to `DateTime.Parse` – Panagiotis Kanavos Oct 09 '19 at 10:36
  • @PanagiotisKanavos, It would be great if you could please share the suggestion is form of ans post. Thanks! – Kgn-web Oct 09 '19 at 10:41
  • @PanagiotisKanavos as I am finding it a bit confusing in form of comments – Kgn-web Oct 09 '19 at 10:42
  • Check the duplicate questions, especially the first one. If you search for the error message you'll find a lot of similar questions. Conversions go inside `MapFrom`. That's where `ToString()` should go – Panagiotis Kanavos Oct 09 '19 at 11:03

0 Answers0