4

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);
12
  • can you please mention the return type of FindGalleryByAppIdAndCategoryId OR what exact type is source Commented Mar 14, 2013 at 8:29
  • Fixed it in the question Commented Mar 14, 2013 at 8:32
  • 1
    please check that when you are getting null values in the galleryViewModel the source that you are passing in isn't null Commented Mar 14, 2013 at 8:37
  • Yes i have checked it and the result is always good that contains objects Commented Mar 14, 2013 at 8:44
  • 1
    check mazharkaunain.blogspot.be/2012/03/… Commented Mar 14, 2013 at 9:57

3 Answers 3

3
  1. Why did you marked your non-navigational properties with 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed what I was thinking, im not sure why he's relying on the DynamicMap if its not actually necessary. I would only use Dynamic in dire situations
2
+50

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?

2 Comments

Yes i will test it but one problem i do not map it to the DomainModel to DomainModel i map to ViewModel
Unfortunately, I cannot reproduce erroneous behaviour
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.