开发者

How do I use automapper to conditionally map key / values to a DTO?

开发者 https://www.devze.com 2023-01-29 13:55 出处:网络
I have the following scenario: Due to an unfortunate legacy database, I have some classes mapped to NHibernate that I want to flatten into a DTO.It looks something like this:

I have the following scenario:

Due to an unfortunate legacy database, I have some classes mapped to NHibernate that I want to flatten into a DTO. It looks something like this:

Here is what I ultimately want:

public class ProteinSampleDto
    {
        public virtual SampleType SampleType { get; set; }
        public virtual Project Project { get; set; }
        public virtual long Variant { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual User User { get; set; }

        public virtual string PrepNumber { get; set; }
        public virtual string Host { get; set; }
        public virtual string NTermTag { get; set; }
        public virtual string CTermTag { get; set; }
        public virtual string Buffer { get; set; }
        public virtual double ConcentrationMgPerMl { get; set; }
        public virtual double StorageTemperatureCelsius { get; set; }
        public virtual string PurificationProtocol { get; set; }
        public virtual string Comments { get; set; }
        public virtual string ProteinSequence { get; set; }
    }

Here is what I have post database mapping:

public class ProteinSample : PersistentObject
    {

        public virtual SampleType SampleType { get; set; }
        public virtual Project Project { get; set; }
        public virtual long Variant { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual User User { get; set; }
        public virtual IList<SampleMetadata> Metadata { get; set; }

    }

public class SampleMetadata : PersistentObject
    {
        public virtual ProteinSample ProteinSample { get; set; }
        public virtual MetadataType MetadataType { get; set; }
        public virtual string Value { get; set; }
        public virtual string LargeValue { get; set; }
    }

 public class MetadataType : PersistentObject
    {
        public virtual string Name { get; set; }
    }

If you look closely, the SampleMetadata type has a MetadataType and two Values (Large / Regular). A MetadataType has a Name, which is basically a question being asked to the user about a sample (what was the Prep Number, What was the N-Term Amino Acid, etc...)

I want to conditionally look at the MetadataType's Name, in the following way:

If Name contains: "Prep Number"
        populate the "PrepNumber" property on ProteinSampleDTO with SampleMetadata.Value
else if Name contains: "N-Term AA"
        populate the "NTermTag" property on ProteinSampleDTO with SampleMetadata.LargeValue
//etc

You get the idea. I want to match what's in MetdataType.Name to the DTO property name, and use the value from Value or Large Value in the SampleMetadata class.

I can change the names of the properties to more closely match the Name string if having a conventi开发者_如何转开发on helps make this easier.

It seems like there should be a slick way to do this without having to manually craft a big monster switch statement (There are about 50 different questions).

Can automapper handle this?


It sounds like you need a custom resolver or perhaps a custom type converter:

https://automapper.readthedocs.io/en/latest/Custom-value-resolvers.html

https://automapper.readthedocs.io/en/latest/Custom-type-converters.html

0

精彩评论

暂无评论...
验证码 换一张
取 消