It's been a while since i've used automapper, but i'm almost sure that my situation should be possible.
Setup
I created the following mapping configuration:
var map = cfg.CreateMap<TSource, Structure>();
So in my situation the source is a generic type (unknown) and the target type is Structure (known).
A possible option for the TSource type could be:
public class DataChannel
{
public string Id { get; set; }
public string Description { get; set; }
public string Ean { get; set; }
public DateTimeOffset ValidFrom { get; set; }
public bool IsManual { get; set; }
public string Type { get; set; }
public string Unit { get; set; }
public string Address { get; set; }
public string BuildingId { get; set; }
}
The target Structure object looks like this:
public class Structure : IStructure
{
public Structure()
{
Children = new List<Structure>();
Properties = new List<StructureProperty>();
}
public int Id { get; set; }
public ICollection<StructureProperty> Properties { get; set; }
public List<Structure> Children { get; set; }
}
Situation
For example, I would like the string properties "Unit" and "Type" to be added as a StructureProperty object to the Properties collection of the Structure entity.
map.ForMember(c => c.Properties, m => m.MapFrom<StructurePropertyResolver<TSource>>());
How can this be done?
So I'm currently debating the option of either defining concrete classes with properties, or to go with a metadata-type design. For example:
public class Employee
{
public Guid Id { get; set; }
public string Name { get; set; }
public string EmployeeCode { get; set; }
public string SpecialAssignment { get; set; }
public string SomeFutureProperty { get; set; }
}
Versus a key/value pair design which can be dynamic:
public class Employee
{
public Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<MetaKeyValue> Properties { get; set; }
}
public class MetaKey
{
public Guid Id { get; set; }
public string EntityType { get; set; } // could be a dictionary or enum
public string Name { get; set; }
public string Description { get; set; }
public string Required { get; set; }
}
public class MetaKeyValue
{
public Guid Id { get; set; }
public Guid EntityId { get; set; }
public Guid MetaKeyId { get; set; }
public string Value { get; set; } // string isn't the preferred object type
}
So, the debate is that I'm not sure which one is more efficient. The target persistence is a SQL database using Entity Framework. The beauty of the metadata design is that without modifying code and planning a deployment, new "properties" could be added to an entity and the values could be added and retrieved. The negative is that it's not what I'm accustomed to as I am an old-school coder who likes concrete classes that are statically defined. Would the dynamic design bite me in the rear later down the line?
I want to convert string to json, but all I get is null or error
For example I want to convert this string:
string json =
"{\"$id\":\"1\",\"Result\":{\"$id\":\"2\",\"ListRate\":[{\"$id\":\"3\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":0.0},{\"$id\":\"4\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":0.0},{\"$id\":\"5\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":0.0}],\"MyRate\":[{\"$id\":\"6\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":1.5},{\"$id\":\"7\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":2.0},{\"$id\":\"8\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":2.0}],\"ListRated\":[{\"$id\":\"9\",\"Title\":\"worth of price\",\"ShoppingRateId\":1,\"Rate\":30.0,\"theCount\":1,\"theSum\":1.5},{\"$id\":\"10\",\"Title\":\"Graphic\",\"ShoppingRateId\":2,\"Rate\":40.0,\"theCount\":1,\"theSum\":2.0},{\"$id\":\"11\",\"Title\":\"fetures\",\"ShoppingRateId\":3,\"Rate\":40.0,\"theCount\":1,\"theSum\":2.0}]},\"StatusCode\":\"Created\",\"Description\":null}";
JavaScriptSerializer serializer = new JavaScriptSerializer();
var myclass = Newtonsoft.Json.JsonConvert.DeserializeObject<ProductDto>(json);
And Class of product:
public class RootObject
{
public string id { get; set; }
public ProductDto Result { get; set; }
public string StatusCode { get; set; }
public object Description { get; set; }
}
public class ProductDto
{
public string id { get; set; }
public IQueryable<MyRateDto> ListRate { get; set; }
public IQueryable<MyRateDto> MyRate { get; set; }
public IQueryable<ShoppingRateDto> ListRated { get; set; }
}
public class ShoppingRateDto
{
public string id { get; set; }
public string Title { get; set; }
public long ShoppingRateId { get; set; }
public double Rate { get; set; }
public int theCount { get; set; }
public double theSum { get; set; }
}
public class MyRateDto
{
public string id { get; set; }
public string Title { get; set; }
public long ShoppingRateId { get; set; }
public double Rate { get; set; }
}
I'm a bit confused how should I convert it .
Also I have using "RootObject" instead of "ProductDto", but nothing changed ...
Some of error:
Additional information: The best overloaded method match for
'Newtonsoft.Json.JsonConvert.DeserializeObject(string)'
has some invalid arguments
Additional information: Cannot create and populate list type
System.Linq.IQueryable`1[Site.Model.MyRateDto]. Path 'Result.ListRate'
Seems like the error is a decent hint. Instead of using IQueryable (basically any interface collections), try using a concrete collection. e.g. List<T>
Serializers have to work with concrete classes to be able to instantiate them through reflection. When they are reading class definitions, and they come across interfaces, seldom can the serializer make a choice on the correct implementation. Since there are potentially many many concrete instances of different collection interfaces, you should provide the serializers with concrete types in order to work properly.
I am using automapper for mapping view models and entity models with each other, all was good, but now i have a little different scenario where AutoMapper is not able to map my types.
My View Model:
public class CriminalSearchViewModel
{
public CriminalSearchParamsViewModel SearchParameters { get; set; }
public SelectList GenderSelectList { get; set; }
public SelectList NationalitySelectList { get; set; }
public SelectList CrimeSelectList { get; set; }
public SelectList CriminalStatusSelectList { get; set; }
}
second view model:
public class CriminalSearchParamsViewModel
{
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public int? GenderID { get; set; }
public int? StatusID { get; set; }
public string CNIC { get; set; }
public int? AgeFrom { get; set; }
public int? AgeTo { get; set; }
public double? Height { get; set; }
public int Weight { get; set; }
public int? NationalityID { get; set; }
}
and my Business Model:
public class CriminalSearch
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int? GenderID { get; set; }
public int? StatusID { get; set; }
public string CNIC { get; set; }
public int? AgeFrom { get; set; }
public int? AgeTo { get; set; }
public double? Height { get; set; }
public int Weight { get; set; }
public int? NationalityID { get; set; }
}
I have defined mapping like:
Mapper.CreateMap<CriminalSearch, CriminalSearchParamsViewModel>();
also tried this as well:
Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearchViewModel>()
.ForMember(dest => dest.SearchParameters, opt =>
opt.MapFrom(src => Mapper.Map<CriminalSearchParamsViewModel, CriminalSearch>(src)));
and in controller i am trying like:
public ActionResult Search(CriminalSearchViewModel searchVM)
{
if (ModelState.IsValid)
{
var searchParams = searchVM.SearchParameters;
var criminalSearch = AutoMapper.Mapper.Map<CriminalSearch>(searchParams);
_criminalService.SearchCriminals(criminalSearch);
}
return View();
}
But it always throws exception:
Missing type map configuration or unsupported mapping.
Mapping types:
CriminalSearchParamsViewModel -> CriminalSearch
NationalCriminals.UI.ViewModels.CriminalSearchParamsViewModel -> NationalCriminals.Core.Models.CriminalSearch
Destination path:
CriminalSearch
Source value:
NationalCriminals.UI.ViewModels.CriminalSearchParamsViewModel
Anybody can pint me what is going wrong?
You just need to change the order of the generic args in the method CreateMap:
Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearch>()
Thats because the first generic arg is the Source type and the second is the Destination, it is not two way, you must to declare the both if you want to map from a type to another and viceversa like this:
Mapper.CreateMap<CriminalSearchParamsViewModel,CriminalSearch>()
Mapper.CreateMap<CriminalSearch,CriminalSearchParamsViewModel>()
The method CreateMap is described like this:
AutoMapper.Mapper.CreateMap<SourceClass, DestinationClass>();
Suggest: Using AutoMapper: Creating Mappings
This my Source class
public class Content :IAggregateRoot
{
public Guid Id { get; set; }
public string HeaderImage { get; set; }
public string AboutText { get; set; }
public string Address { get; set; }
public long Phone { get; set; }
public long Mobile { get; set; }
public DateTime CreationTime { get; set; }
}
and this is my Destination class
public class AboutViewModel
{
public string AboutText { get; set; }
public string Address { get; set; }
public long Phone { get; set; }
public long Mobile { get; set; }
}
I just want to ignore extra properties of source and map source to destination with Automapper with this method
public static AboutViewModel ConvertToAboutViewModel(this Content content)
{
// Mapper.CreateMap<Content,AboutViewModel>().ForMember(x=>x.AboutText,)
return Mapper.Map<Content, AboutViewModel>(content);
}
How can I do that??
Automapper would ignore those properties by default as they do not exist in the destination class.
Have you actually tried to run your code? if it is not working could you please post details of the error or exception you are getting.
You also need to define your basic map like this:
Mapper.CreateMap<Content, AboutViewModel>();
Mapper.CreateMap<AboutViewModel, Content>();
Ensure that the above code is ran before you call ConvertToAboutViewModel