3

I've a domain class named Campaign.

Class Campaign {
    public long CampaignID { get; set; }
    public string CampaignName { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

In addition to that, i'm using Asp.net MVC, and I defined a model class.

[DataContract]
public class CampaignModel
{
    [DataMember(Name="id")]
    public long Id { get; set; }

    [DataMember(Name = "name")]
    public string CampaignName { get; set; }

    [DataMember(Name = "isDummy")]
    public bool IsDummy { get; set; }
} 

My goal is to convert from one class to another and vice versa.

I'm using the ASP.NET for REST calls from my Angular 2 app.

My app has two use cases, one is to create Campaign, therefore I need to convert from CampaignModel to Campaign class and store the campaign. The second use-case is to load existing Campaign into the UI, therefore convert from Campaign to CamapignModel.

  • If that's matter, the conversation between the two isn't trivial and involves using complex data structures.

I've thought of several options:

  1. Create two methods in the CampaignModel.

    public Campaign Convert();
    public CampaignModel Convert(Campaign);
    
  2. Create separate class e.g. CampaignConverter

  3. Use dedicated known library that facilitates the conversion between the two, or known design pattern?
  4. any other ideas?

Thanks

  • 1
    You can use a T4 template to generate both classes and the conversion from a common source. – Erik Eidt Jan 17 '17 at 16:59
  • How are IsDummy of the model, StartTime and EndTime of the domain class expected to get their values during conversion? How to deal with converting and converting back? – Bernhard Hiller Jan 18 '17 at 11:33
4

If this is a simple cast job (it doesn't seem to be), I'd use an explicit operator. There are some nice examples in MSDN documentation about this:

// Must be defined inside a class called Fahrenheit:
public static explicit operator Celsius(Fahrenheit fahr)
{
    return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32));
}

This cast is done in the client class, the class that's the destination of the cast. The source doesn't have to know who's gonna consume it. It's a responsibility of the consumer to do the proper casting.

However, this part of your question got me thinking:

If that's matter, the conversation between the two isn't trivial and involves using complex data structures.

If this transformation is complex, time-consuming, involves IO and prone to launch exceptions, then the best way is to create a specific class in a Business layer (or in your Domain layer), responsible to do the transformation and handle properly the possible exceptions.

But just be aware that a Data Contract should not be an inner part of your Domain model, because it's at the boundary. And "At the Boundaries, Applications are Not Object-Oriented".

|improve this answer|||||
  • Thanks, I did go with the second option to create a new class that will deal with the transformation. Is there any naming convention for such a class? if there is can you please name one? – MasterOfPuppets Jan 23 '17 at 14:58
  • Aside from this: martinfowler.com/bliki/TwoHardThings.html , what is the class doing ? It converting what to what ? – Machado Jan 23 '17 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.