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

Private Abstract Method in parent class makes the child class abstract?
Android Realm Abstract Class Instantiate
If DateFormat is an abstract class how is it instantiated with getInstance() method
wxribbonbuttonbase dynamic label change
Initializing Variable in Subclass
Why cant we instantiate abstract class
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

Categories

HOME
google-chrome-extension
pdf
wso2
pycharm
onedrive
fme
dxl
in-app-purchase
qore
facebook-messenger-bot
applepay
commonmark
event-handling
django-simple-history
ef-migrations
normalizr
one-hot-encoding
opencover
errorlevel
tapestry
emgucv
one-to-many
underflow
fgetcsv
neo4j-spatial
webkitspeechrecognition
pdb
gtrendsr
stringtemplate
phpfreechat
language-concepts
hexo
bower-install
devextreme
xcode-extension
dism
g-code
keydown
modelmapper
angular-resource
zip4j
dandelion
integrity
worker-thread
nsarray
dtexec
windows-mobile-6.5
pdfclown
jspdf-autotable
color-profile
django-scheduler
blacklist
crosswalk-runtime
migradoc
intrusion-detection
paypal-express
connect-by
sigabrt
browser-link
freedesktop.org
unity-networking
r-tree
msys
t4mvc
microbenchmark
tween
jsonpickle
tt-news
angularjs-ng-click
cctv
client-side-templating
rdl
document-classification
bitcoinj
centos5
imdbpy
opcache
easy-install
enterprisedb
jelly
specification-pattern
angularjs-controller
google-email-migration
netbeans-6.9
buildr
stage
django-tagging
remember-me
cxxtest
rescale
substrings
adrotator
boost-smart-ptr
scripting-languages

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