abstract-class


AutoMapper and Base Types


I have a collection of EntityDtos.
Each EntityDto has a property called EntityType.
Each of these EntityTypes correspond to a different subclass, something like this
abstract class EntityBase { EntityType = EntityType.Base; }
class EntityOne : EntityBase { EntityType = EntityType.One; }
class EntityTwo : EntityBase { EntityType = EntityType.Two; }
I am trying to map to a collection of EntityBase. AutoMapper fails with the error "Instances of abstract classes cannot be created". I have the Type enum, and thus know what type each should be mapped to...but really, just want them all mapped into my EntityBase collection.
I can't figure this out...
I have this working, but it's VERY ugly.
Mapper.CreateMap<EntityCollectionDto, EntityCollection>().ForMember(
s => s.Entities, d => d.MapFrom(
x => new List<EntityBase>(
from p in x.Entitys
select p.EntityType == EntityType.One ? Mapper.Map<EntityOne>(p) as EntityBase
: p.EntityType == EntityType.Two ? Mapper.Map<EntityTwo>(p) as EntityBase
: Mapper.Map<EntityThree>(p) as EntityBase
)
)
);
Mapper.CreateMap<EntityDto, EntityOne>();
Mapper.CreateMap<EntityDto, EntityTwo>();
I don't know if you'll like this any better, but assuming the entity classes as follows:
public abstract class EntityBase
{
public EntityType EntityType { get { return EntityType.Base; } }
}
public class EntityOne : EntityBase
{
public new EntityType EntityType { get { return EntityType.One; } }
}
public class EntityTwo : EntityBase
{
public new EntityType EntityType { get { return EntityType.Two; } }
}
public class EntityThree : EntityBase
{
public new EntityType EntityType { get { return EntityType.Three; } }
}
public class EntityCollection
{
public IList<EntityBase> Entities { get; set; }
}
public class EntityDto
{
public EntityType EntityType { get; set; }
}
public class EntityCollectionDto
{
public IList<EntityDto> Entities { get; set; }
}
You can create a TypeConverter:
public class EntityTypeConverter : AutoMapper.TypeConverter<EntityDto, EntityBase>
{
protected override EntityBase ConvertCore(EntityDto source)
{
switch (source.EntityType)
{
case EntityType.One:
return AutoMapper.Mapper.Map<EntityOne>(source);
case EntityType.Two:
return AutoMapper.Mapper.Map<EntityTwo>(source);
default:
return AutoMapper.Mapper.Map<EntityThree>(source);
}
}
}
This will then simplify your mapping to:
AutoMapper.Mapper.CreateMap<EntityDto, EntityBase>()
.ConvertUsing(new EntityTypeConverter());
AutoMapper.Mapper.CreateMap<EntityDto, EntityOne>();
AutoMapper.Mapper.CreateMap<EntityDto, EntityTwo>();
AutoMapper.Mapper.CreateMap<EntityDto, EntityThree>();
AutoMapper.Mapper.CreateMap<EntityCollectionDto, EntityCollection>();
AutoMapper.Mapper.AssertConfigurationIsValid();
So you still have the specific mapping in the TypeConverter (I'm not sure there's a way to avoid that), but I think the end result is a bit cleaner.

Related Links

Abstract classes error display
Abstract Classes - A glitch in my program
How to implement IXmlSerializable correctly for a ICollection<T> where T is an abstract class?
Typescript multiple inheritance workarounds?
Class diagram usage in UML
AutoMapper and Base Types
Abstract Class Initialization
Covariant return types
Empty abstract function cannot contain body
Using Visitor Pattern to detect intersection between two shapes
mongodb lift class Snippet needs to be abstract
Abstract class as return of wcf ria method
OpenKinect - FreenectDevice abstract?
Intercepting method from abstract parent on derived instance using DynamicProxy
In an UML diagram, when should a class be abstract?
Abstract Class is a good practice?

Categories

HOME
ms-access
netsuite
server
lodash
grep
read-eval-print-loop
microservices
jgroups
retrofit
alpha
wamp
pheatmap
cross-validation
modelica
spring-tool-suite
alignment
gz
zebra-printers
iolanguage
fatal-error
jqwidget
alpine
zapier
functional-testing
arabic
kvc
django-storage
neo4j-spatial
dbclient
procdump
bpel
pdb
gtrendsr
textmate
normal-distribution
io-redirection
qwerty
gesture
vapor
sqlite2
bootstrapper
tasker
android-tabhost
g-code
http-live-streaming
jvm-languages
glew
botbuilder
bootstrap-dialog
.net-4.6.2
taffy
python-c-api
blogengine.net
dropbox-php
django-scheduler
ableton-live
database-optimization
mikroc
contact-list
ubuntu-10.04
ios4
rhino-servicebus
tcpserver
xna-4.0
intel-fortran
ruby-2.2
method-parameters
oberon
key-management
graph-api-explorer
inmobi
sorl-thumbnail
nstableviewcell
zend-route
project-planning
valueconverter
comexception
balanced-payments
java.nio.file
pyhdf
ccss
dataservice
pushbackinputstream
ocunit
cisco-jtapi
twrequest
viewswitcher
qtkit
amazon-appstore
semantic-diff
xmlspy
locationlistener
visitor-statistic
gallio
gamma
webkit.net
kdbg
gacutil
.net-1.0
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App