Reputation: 4161
Very strange thing happened in my project, i have a pretty simple CLR
objects,
First one is the Model
other is ViewModel
, after i compile the project i run my WebApi ASP.NET
project, with the required parameters, i can see that my Model
return with data.
Once i can see the Mapper did mapping ok, and the second time it's return every thing with nulls. The problem that is not happens all the time.
Very Important: Update 14.03.2013
It's stop doing it when i recycle application,
but after a while it's start doing it again, i re-save the web.config
file
then it's ok again.
Here is my Model/ViewModel:
public class Gallery : Entity
{
public override long Id { get; set; }
public virtual Settings SiteOwner { get; set; }
public virtual Category Category { get; set; }
public virtual string PageTitle { get; set; }
public virtual string TitleDescription { get; set; }
public virtual string GalleryTitle { get; set; }
public virtual IList<UIItem> GalleryItems { get; set; }
}
public class UIItem : Entity
{
public override long Id { get; set; }
public virtual Product Product { get; set; }
public virtual Gallery Gallery { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Price { get; set; }
public virtual string ImageUrl { get; set; }
public virtual string VideoUrl { get; set; }
public virtual string FileUrl { get; set; }
}
public class GalleryViewModel
{
public virtual string PageTitle { get; set; }
public virtual string TitleDescription { get; set; }
public virtual string GalleryTitle { get; set; }
public virtual IList<UIItemViewModel> GalleryItems { get; set; }
}
public class UIItemViewModel
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Price { get; set; }
public virtual string ImageUrl { get; set; }
public virtual string VideoUrl { get; set; }
public virtual string FileUrl { get; set; }
}
Here is the way i use it
// my apicontroller
// FindGalleryByAppIdAndCategoryId returns => Gallery
var source = _galleryRepository.FindGalleryByAppIdAndCategoryId(appId, catId);
return Mapper.DynamicMap<GalleryViewModel>(source);
Upvotes: 4
Views: 377
Reputation: 2415
virtual
keyword? Only properties, which involved in relations one-to-one, one-to-many, many-to-many and complex types can be marked with virtual
keyword to apply lazy loading.2.
Mapper.CreateMap<UIItem,UIItemViewModel>();
...
UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem);
If you need special mapping logic, you can do like this:
Mapper.CreateMap<UIItem,UIItemViewModel>().ConvertUsing(new ModelToViewModelConverter());
...
UIItemViewModel result = Mapper.Map<UIItem,UIItemViewModel>(your UIItem);
...
Converter will be look like this:
public class ModelToViewModelConverter : ITypeConverter<UIItem,UIItemViewModel>
{
public UIItemViewModel Convert(ResolutionContext context)
{
UIItem item = (UIItem)context.SourceValue;
// Perform your convertion logic
}
}
In the same way you can map all of your entities even collection to collection.
Upvotes: 3
Reputation: 2989
Can you try using
Mapper.Map<Source,Destination>(source);
or
Mapper.Map(source);
and ensure you have declared a
CreateMap<Source, Destination>();
I don't see your reason for using Dynamic Map when you can declare a CreateMap of the two types. You shouldn't experience this behaviour doing it this way.
Upvotes: 1
Reputation: 6741
Most likely this happens in cases, when Gallery and UIItem have cross references:
public class Gallery : Entity
{
public virtual IList<UIItem> GalleryItems { get; set; }
}
public class UIItem : Entity
{
public virtual Gallery Gallery { get; set; }
}
Can you please test this case in your code?
Upvotes: 2