I am using ValueInjecter to map domain classes to my view models. My domain classes are complex. To borrow an example from this question:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
// VIEW MODEL
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int PersonId { get; set; }
public int AddressId { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
I have looked at FlatLoopInjection, but it expects the view model classes to be prefixed with nested domain model type like so:
public class PersonViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Id { get; set; }
public int AddressId { get; set; }
public string AddressCity { get; set; }
public string AddressState { get; set; }
public string AddressZip { get; set; }
}
The OP in the linked question altered his view models to match the convention expected by FlatLoopInjection. I don't want to do that.
How can I map my domain model to the original unprefixed view model? I suspect that I need to override FlatLoopInjection to remove the prefix, but I am not sure where to do this. I have looked at the source for FlatLoopInjection but I am unsure if I need to alter the Match method or the SetValue method.
you don't need flattening, add the map first:
Mapper.AddMap<Person, PersonViewModel>(src =>
{
var res = new PersonViewModel();
res.InjectFrom(src); // maps properties with same name and type
res.InjectFrom(src.Address);
return res;
});
and after that you can call:
var vm = Mapper.Map<PersonViewModel>(person);
Related
public class BClass
{
public class RClass
{
public string stjCd { get; set; }
public string lgnm { get; set; }
public string stj { get; set; }
public string dty { get; set; }
public List<object> adadr { get; set; }
public string cxdt { get; set; }
public string gstin { get; set; }
public List<string> nba { get; set; }
public string lstupdt { get; set; }
public string rgdt { get; set; }
public string ctb { get; set; }
public Pradr pradr { get; set; }
public string tradeNam { get; set; }
public string sts { get; set; }
public string ctjCd { get; set; }
public string ctj { get; set; }
}
public class AClass
{
public string id { get; set; }
public string consent { get; set; }
public string consent_text { get; set; }
public int env { get; set; }
public string response_code { get; set; }
public string response_msg { get; set; }
public int transaction_status { get; set; }
public string request_timestamp { get; set; }
public string response_timestamp { get; set; }
public RClass result { get; set; }
}
}
//COntroller
BClass.AClass btr = new BClass.AClass();
var lst = JsonConvert.DeserializeObject<BClass.AClass>(strresult);
btr.response_code = lst.response_code;
btr.response_msg = lst.response_msg;
btr.result.lgnm = lst.result.lgnm;
The property btr.result.lgnm = lst.result.lgnm; Gives null value error object reference not set to instance of an object. but the lst variable has a value in the response received.Please provide suggesion
You can solve this by adding one line into your code.
btr.result = new BClass.RClass(); //This one. You need to initialize instance before assigning anything to it.
btr.result.lgnm = lst.result.lgnm;
or else, you can also create default constructor for class A.
public AClass()
{
result = new RClass();
}
I would suggest you to please have a look at below web resources for naming conventions widely used for c# language.
Properties naming conventions: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members
class naming conventions: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces
Assigning this way does not give null reference exception
RClass rclass=new RClass();
rclass.lgnm=lst.result.lgnm
I'm having a hard time passing the value of an object to a model.
I wanted to pass the data from this obj to the model class
SingleTransactResponse obj = JsonConvert.DeserializeObject<SingleTransactResponse>(await response.Content.ReadAsStringAsync());
SaveTransaction(JsonConvert.SerializeObject(obj));
I used this function to get the data from the model and save it to the database
PayoutEntities payoutdb = new PayoutEntities();
public String SaveTransaction(payout_transaction model)
{
payoutdb.payout_transaction.Add(model);
payoutdb.SaveChanges();
return "Success";
}
SingleTransactResponse Class
public class SingleTransactResponse {
public String senderRefId { get; set; }
public String tranRequestDate { get; set; }
public String particulars { get; set; }
public List<Beneficiary> beneficiary { get; set; }
}
Beneficiary Class
public class Beneficiary
{
public String accountNumber { get; set; }
public String name { get; set; }
public List<Address> address { get; set; }
}
Address Class
public class Address
{
public String line1 { get; set; }
public String line2 { get; set; }
public String city { get; set; }
public String province { get; set; }
public String zipCode { get; set; }
public String country { get; set; }
}
payout_transaction class
public partial class payout_transaction
{
public string transid { get; set; }
public string batchid { get; set; }
public string senderRefId { get; set; }
public string requestDate { get; set; }
public string benefName { get; set; }
public string benefacctno { get; set; }
public string status { get; set; }
public string errdesc { get; set; }
public string transaction_fee { get; set; }
}
I'm just having a hard time converting the obj to the model. I've tried JsonConvert.SerializeObject(obj) but it only converts it to string. Is there any possible way to do this or any work around to solve this problem?
you can do like this
var payoutModel = new payout_transaction
{
senderRefId = obj.senderRefId,
requestDate = obj.tranRequestDate,
.... other properties
}
SaveTransaction(payoutModel);
I have relationship one to one
public class Book
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual File File { get; set; }
public int? SeriesId { get; set; }
public DateTime UploadDate { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual ICollection<Mark> Marks { get; set; }
public Book()
{
Comments = new List<Comment>();
Authors = new List<Author>();
Genres = new List<Genre>();
}
}
public class File
{
[Key,ForeignKey("Book")]
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual Book Book { get; set; }
}
And I want to transfer data to classes:
public class BookDO
{
public int BookId { get; set; }
public string Name { get; set; }
public string Annotation { get; set; }
public virtual FileDO File { get; set; }
}
public class FileDO
{
public int BookId { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
public virtual BookDO Book { get; set; }
}
in such way:
var books = Database.Books.GetAll().ToList();
Mapper.Initialize(cf => cf.CreateMap<Book, BookDO>());
return Mapper.Map<List<Book>, List<BookDO>>(books);
but i'm getting Missing type map configuration or unsupported mapping.
Mapping types:
File -> FileDO
Domain.File -> BusinessLogic.Data_Objects.FileDO
Maybe i need to initialize one more mapper to map File to FileDO or modify existing mapper configuration? help me please.
Yes, you also need to create a map for File -> FileDo. This map must be configured for the same mapper as used for Book -> BookDo.
It is good practice to wrap your mapping configuration into an AutoMapper.Profile:
using AutoMapper;
public class BookMappingProfile: Profile {
public BookMappingProfile() {
CreateMap<Book, BookDo>();
CreateMap<File, FileDo>();
}
}
And then initialize the mapper with these profiles:
Mapper.Initialize(cfg => {
cfg.AddProfile<BookMappingProfile>();
cfg.AddProfile<MyOtherProfile>();
});
I'm using AutoMapper to map Domain objects to ViewModel objects in my controller. Everything was working fine until I tried adding double? properties. I've started getting the following error:
Missing type map configuration or unsupported mapping.
Mapping types:
Address -> AddressModel
Domain.Address -> Web.Models.AddressModel
Destination path:
AccountAddressModel.Address.Address
Source value:
Domain.Address
My Address class and AddressModel class both have two properties called Longitude and Latitude. These properties (in both classes) are defined as double?. If I comment out these properties, everything works fine. If I make all of these properties simply double, then everything works fine. It's only double? that causes the problem.
I'm using AutoMapper 2.2.1 downloaded via NuGet.
I've read in other posts that this problem with nullables was supposed to be fixed. This leads me to believe I might be doing something different so I'm going to post my code to see if anyone can see something that might be an issue:
DOMAIN MODELS
public class AccountAddress
{
public int Id { get; set; }
public int AccountId { get; set; }
public int AddressId { get; set; }
public Address Address { get; set; }
...
}
public class Address : IUserTrackingEntity
{
public int Id { get; set; }
public string Addressee { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
...
public double? Longitude { get; set; }
public double? Latitutde { get; set; }
}
VIEW MODELS
public class AccountAddressEditModel
{
public string AccountName { get; set; }
public AccountAddressModel Address { get; set; }
public IList<Country> CountriesList { get; set; }
...
}
public class AccountAddressModel
{
public AccountAddressModel()
{
Address = new AddressModel();
}
public int AccountId { get; set; }
public int AddressId { get; set; }
public int Id { get; set; }
public AddressModel Address { get; set; }
}
public class AddressModel
{
public int Id { get; set; }
public string Addressee { get; set; }
[Required(ErrorMessage="A street address is required.")]
public string Street1 { get; set; }
public string Street2 { get; set; }
...
public double? Longitude { get; set; }
public double? Latitude { get; set; }
}
MAPPING CODE IN MY CONTROLLER
//Get an AccountAddress
address = _context.AccountAddresses.SingleOrDefault(ad => ad.Id == 12345);
model = new AccountAddressEditModel();
Mapper.CreateMap<AccountAddress, AccountAddressModel>();
Mapper.CreateMap<AccountAddress, AddressModel>();
Mapper.Map(address, model.AccountAddress);
Has anyone else experienced this or found a solution for this?
The line
Mapper.CreateMap<AccountAddress, AddressModel>();
should be changed to
Mapper.CreateMap<Address, AddressModel>();
This because you are mapping the Address class to the AddressModel class.
I can't understand what i'm doing wrong. Every time I'm getting this error:
The entity or complex type 'BusinessLogic.CompanyWithDivisionCount' cannot be constructed in a LINQ to Entities query.
I need to get info from 'Company' table and divisions count of each company from 'Division' table, and then make PagedList. Here is my 'Company' table:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using BusinessLogic.Services;
using BusinessLogic.Models.ValidationAttributes;
namespace BusinessLogic.Models
{
public class Company
{
public Company()
{
Country = "US";
Status = true;
}
public int Id { get; set; }
[Required]
[UniqueCompanyName]
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
public string ContactInfo { get; set; }
[Required]
public DateTime EffectiveDate { get; set; }
public DateTime TerminationDate { get; set; }
public bool Status { get; set; }
[Required]
public string URL { get; set; }
public string EAP { get; set; }
public string EAPCredentials { get; set; }
public string BrandingColors { get; set; }
public string Comments { get; set; }
}
}
Here is my domain model:
public class Company
{
public Company()
{
Country = "US";
Status = true;
}
public int Id { get; set; }
[Required]
[UniqueCompanyName]
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
public string Country { get; set; }
public string ContactInfo { get; set; }
[Required]
public DateTime EffectiveDate { get; set; }
public DateTime TerminationDate { get; set; }
public bool Status { get; set; }
[Required]
public string URL { get; set; }
public string EAP { get; set; }
public string EAPCredentials { get; set; }
public string BrandingColors { get; set; }
public string Comments { get; set; }
}
public class CompanyWithDivisionCount: Company // I'm using this
{
public int DivisionCount { get; set; }
}
Here is my controller:
public ActionResult CompaniesList(int? page)
{
var pageNumber = page ?? 1;
var companies = companyService.GetCompaniesWithDivisionsCount2();
var model = companies.ToPagedList(pageNumber, PageSize);
return View(model);
}
And here is my service part:
public IQueryable<CompanyWithDivisionCount> GetCompaniesWithDivisionsCount2()
{
return (from c in dataContext.Companies.AsQueryable()
select new CompanyWithDivisionCount
{
Id = c.Id,
Name = c.Name,
Status = c.Status,
EffectiveDate = c.EffectiveDate,
URL = c.URL,
EAP = c.EAP,
EAPCredentials = c.EAPCredentials,
Comments = c.Comments,
DivisionCount = (int)dataContext.Divisions.Where(b => b.CompanyName == c.Name).Count()
});
}
}
Thanks for help!!!
Creator of PagedList here. This has nothing to do with PagedList, but rather is an Entity Framework issue (I'm no expert on Entity Framework, so can't help you there). To confirm that this is true, write a unit test along the following lines:
[Test]
public void ShouldNotThrowAnException()
{
//arrange
var companies = companyService.GetCompaniesWithDivisionsCount2();
//act
var result = companies.ToList();
//assert
//if this line is reached, we win! no exception on call to .ToList()
}
I would consider changing you data model if possible so that instead of relating Companies to Divisions by name strings, instead use a properly maintained foreign key relationship between the two objects (Divisions should contain a CompanyID foreign key). This has a number of benefits (including performance and data integrity) and will almost certainly make your life easier moving forward if you need to make further changes to you app (or if any company ever decides that it may re-brand it's name).
If you create a proper foreign key relationship then your domain model could look like
public class Company
{
...
public virtual ICollection<Division> Divisions{ get; set; }
public int DivisionCount
{
get
{
return this.Divisions.Count()
}
}
...
}