fluentsprings / ExpressMapper

Mapping .Net types
http://www.expressmapper.org
Other
310 stars 65 forks source link

Migrate from AutoMapper to ExpressMapper - IOC problem with SimpleInjector initialization #113

Closed clabnet closed 8 years ago

clabnet commented 8 years ago

Hi, I have migrated from AutoMapper to your product. I must use it with SimpleInjector, but I cannot find any documentation for mapper registration. Here the mapper code :

        static SearchMapper()
        {
            Mapper.Register()
                .Member(dest => dest.selected, src => true);
            Mapper.Compile();
        }

        public SearchModel ToDTO(DCCvwStatusReport rec)
        {
            return Mapper.Map(rec);
        }

        public IEnumerable ToDTOList(IEnumerable recs)
        {
            return Mapper.Map, IEnumerable>(recs);
        }

The mapper registration on container bootstrap :

...
> container.Register<ISearchMapper,  SearchMapper>();
...

The SearchModel class (used as src object):

  public class SearchModel : StatusMsg
    {
    }

The DCCvwStatusReport class, (used as dest object)

    public partial class DCCvwStatusReport
    {
        public virtual int? statusID { get; set; }
        public virtual string vin { get; set; }
        public virtual string msisdn { get; set; }
        public virtual string ansInfo { get; set; }
        public virtual string ansStatus { get; set; }
        public virtual DateTime? ansLastRefresh { get; set; }
        public virtual string dseInfo { get; set; }
        public virtual string dseStatus { get; set; }
        public virtual DateTime? dseLastRefresh { get; set; }
    }

And mapper interface :

    public interface ISearchMapper
    {
        SearchModel ToDTO(DCCvwStatusReport rec);
        IEnumerable ToDTOList(IEnumerable recs);
    }

When I run the verify of IOC container initialization, I get this error :

The configuration is invalid. Creating the instance for type ISearchMapper failed. The type initializer for 'EvoPack.Dcc.BusinessEntities.SearchMapper' threw an exception.

Error occured trying to compile mapping for: source EvoPack.Dcc.DataModel.DCCvwStatusReport, destination EvoPack.Dcc.BusinessEntities.SearchModel. See the inner exception for details."

{"No parameterless constructor defined for this object."}

Any help ?

Thank's

clabnet commented 8 years ago

Big news.

After 1 day of test and research, I have found the trick. Have you tried to map an entity containing an attribute with "object" as type ? This is the problem.

On my StatusMsg class inherithed by SearchModel definition was an object as type.

public object msisdn { get; set; } // WRONG ! public string msisdn { get; set; } // correct!