Pracuję nad aplikacją MVC, która używa funkcji Inversion of Control i w konsekwencji szeroko wykorzystuje typy interfejsów, a implementacje betonu są wstrzykiwane przez narzędzie do rozwiązywania zależności, zależnie od potrzeb. Interfejsy encji dziedziczą po interfejsie bazowym opisującym podstawowe funkcje zarządzania dla encji. ViewModels są również szeroko stosowane.Mapowanie Automappera do interfejsu: wyjątek "Metoda nie zaimplementowana"
Aplikacja korzysta z Automappera i stworzyłem odwzorowania z modeli widoku na różne interfejsy encji. Konfiguracja mapowania jest poprawnie sprawdzana. Jednak, gdy wywołuję program Automapper w celu wykonania mapowania, kod kończy się niepowodzeniem z TypeLoadException
.
Wierzę, że Automapper potrafi mapować na interfejsy (patrz this od Jimmy'ego Bogarda).
Wydaje się, że generator proxy Automapper pominął dodanie MyMethod() do proxy i powoduje to wyjątek, gdy Reflection próbuje utworzyć typ.
Jeśli tak nie jest, jak mogę uruchomić tę mapę? Czy przegapiłem coś oczywistego?
Oto uproszczona aplikacja konsoli, który demonstruje scenariusz i który powiela błąd podczas uruchamiania:
public interface IEntity
{
string Foo { get; set; }
string Bar { get; set; }
string MyMethod();
}
public class MyEntity : IEntity
{
public string Foo { get; set; }
public string Bar { get; set; }
public string MyMethod()
{
throw new NotImplementedException();
}
}
public class MyViewModel
{
public string Foo { get; set; }
public string Bar { get; set; }
}
class Program
{
static void Main(string[] args)
{
AutomapperConfig();
MyViewModel vm = new MyViewModel { Foo = "Hello", Bar = "World" };
IEntity e = Mapper.Map<MyViewModel, IEntity>(vm);
Console.WriteLine(string.Format("{0} {1}", e.Foo, e.Bar));
}
private static void AutomapperConfig()
{
Mapper.Initialize(cfg => {
cfg.CreateMap<MyViewModel, IEntity>();
});
Mapper.AssertConfigurationIsValid();
}
}
Wyjątek rzucony jest:
InnerException: System.TypeLoadException
HResult=-2146233054
Message=Method 'MyMethod' in type 'Proxy<AutomapperException.IEntity_AutomapperException_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null>' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005' does not have an implementation.
Source=mscorlib
TypeName=Proxy<AutomapperException.IEntity_AutomapperException_Version=1.0.0.0_Culture=neutral_PublicKeyToken=null>
StackTrace:
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at AutoMapper.Impl.ProxyGenerator.CreateProxyType(Type interfaceType)
at AutoMapper.Impl.ProxyGenerator.GetProxyType(Type interfaceType)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.CreateObject(ResolutionContext context)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.NewObjectPropertyMapMappingStrategy.GetMappedObject(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper)
at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context)