Home > Enterprise >  Automapper in VB.Net
Automapper in VB.Net

Time:01-13

I am having a devil of a time attempting to translate the following piece of code from c# to VB.Net. I have little experience in VB.Net and all my searches have proven fruitless to date.

IMapper DataReaderMapper = new MapperConfiguration(cfg => {
    cfg.AddDataReaderMapping();
    cfg.CreateMap<IDataReader, MyDTO1>();
    cfg.CreateMap<IDataReader, MyDTO2>();
    cfg.CreateMap<IDataReader, MyDTO3>();
}).CreateMapper();

The code is using the Automapper and Automapper.Data nuget packages to map datatables to DTOs. I know the code works fine in c#.

My best guess was the following:

Dim DataReaderMapper As IMapper = New MapperConfiguration(Function(cfg) {cfg.AddDataReaderMapping(), cfg.CreateMap(Of IDataReader, MyDTO1)()}).CreateMapper()

The above results in an "Overload resolution failure" warning as I am obviously not passing in the arguments/parameters in the correct fashion/order. I can usually muddle by with most translations that I have to deal with but this one is stumping me. Any help would be appreciated.

CodePudding user response:

According to https://converter.telerik.com/

    Dim DataReaderMapper As IMapper = New MapperConfiguration(Function(cfg)
                                                                  cfg.AddDataReaderMapping()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO1)()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO2)()
                                                                  cfg.CreateMap(Of IDataReader, MyDTO3)()
                                                              End Function).CreateMapper()

CodePudding user response:

I was able to resolve by combining the responses of Nick Abbot's Telerik conversion and Craig's suggestion to swap out "Function" for "Sub" to get the following:

Dim DataReaderMapper As IMapper = New MapperConfiguration(Sub(cfg)
                                                              cfg.AddDataReaderMapping()
                                                              cfg.CreateMap(Of IDataReader, MyDTO1)()
                                                              cfg.CreateMap(Of IDataReader, MyDTO2)()
                                                              cfg.CreateMap(Of IDataReader, MyDTO3)()
                                                          End Sub).CreateMapper()

Thank you gentlemen for your assistance!

  • Related