Building one object given another

Say I am calling a third-party API which returns a Post, and I want to take that and transfer properties from it into my own Post class. I have in the past had a method like public static my.Post build(their.Post post) which maps the properties how I want.

However, is it better/valid to have a constructor that accepts their.Post and does the property mapping in there? Or should there always be a separate class that does the converting, and leaves my.Post in a more POJO state?

Thanks for your thoughts!

-------------Problems Reply------------

These answers always starts with "it depends."

People generally argue against using public static methods, based on the fact that it is hard to mock them (I don't buy into that bandwagon).

This comes down to design, do you want their post to be part of your class? If you add it as a "copy" constructor then it will now be part of your class and you are dependent on changes to post. If they change their post, your code has to adapt.

The better solution is to decouple it. You would need to find some extenal method to map the two. One way is to use a static builder method (like you mentioned) or if you want to take it a step further, a more complicated solution would be to extract the information you want from their post into some type of generic collection class. Then create a constructor that will accept that constructor class. This way if they change their design your class stays in tact and all you have to do is update the mappings from their post to your generic representation of it.

public class MyPost{

public MyPost(ICollectionOfProperties props){
//copy all properties.


public static class TheirPostExtensions{
public static ICollectionOfProperties ExtractProperties(this TheirPost thePost){
return new CollectionOfProperties(){
A = thePost.PropA,
B = thePost.PropB

public class Example{
public Example(){
TheirPost tp = new TheirPost();
ICollectionOfProperties props = tp.ExtractProperties();
MyPost mp = new MyPost(props);


Category:oop Views:0 Time:2010-09-29
Tags: oop

Related post

  • How do I build an object that stores pictures? 2009-02-16

    If you were building an object to store a photo (for a program like photoshop) how would you design it? What properties would you take into account? --------------Solutions------------- One of the major things I would consider is whether the photo ac

  • How can I use linq to build an object from 1 row of data? 2010-06-17

    I have a quick linq question. I have a stored proc that should return one row of data. I would like to use a lambda to build an object. Here's what I'm currently doing which works, but I know I should be able to use First instead of Select except I c

  • How to build an Objective-C static library? 2011-01-11

    I have some Objective-C classes, which I am currently using in both a Cocoa application (Mac OS X) and a Cocoa-Touch application (iOS). Currently, when I update those classes, I have to copy those updated .h and .m files to both projects. Not that bi

  • Build XML object on iOS with XCode 4, i.e. with KissXML 2011-05-14

    The official NSXML stuff doesn't work on iOS, according to manual (and trial and error). There are some other projects, but most are just XML parsers. But I want to create XML objects. There is KissXML, but the tutorial on the website only shows how

  • Trying To Publish An Action To Facebook Timeline Using The Build-in Object 2011-10-18

    I'm trying to publish an action to facebook timeline using the Build-in object types video.episode and video.tv_show. The 'object' i'm trying to publish is I do have the publish_action permissio

  • Are there any efforts building an object-CMIS-model-mapper / other higher level abstraction under way? 2012-02-06

    Building an object-oriented application on top of CMIS can feel just about as low level as using raw SQL. For SQL databases, we have OR-Mappers such as hibernate or libraries such as ibatis in the java world to provide us with basic CRUD functionalit

  • How to build an object of friends and likes 2012-03-12

    This is partially a javascript technique question. I am trying to build an object with facebook id as the key, and an array of likes as the value. My issue is the in my innermost function, I cannot access the variable fbid that I need for setting the

  • Building hierarchy objects from flat list of parent/child 2008-11-25

    I have a list of items in a hierarchy, and I'm attempting to parse this list out into an actual hierarchy of objects. I'm using modified pre-order tree traversal to store/iterate through this list, and so what I have is a subset of the tree, includin

  • Building HtmlElement object trees 2009-03-10

    I'm using the MSIE WebBrowser control in a C# desktop application and am looking for a way to build and maintain trees of HtmlElement objects outside of this control. I am trying to quickly switch between multiple complex pages without incurring the

  • Linq query, how to build nested objects from single table 2009-06-10

    I have a single table and I need to build a bunch of nested objects based on the single table. Data: PointA PointB Month Time Price 1 2 11 11:00 10.99 1 2 12 11:00 9.99 Objects are POINTS {PointA, PointB, Details} Details {Month, ExtraDetails} ExtraD

  • Design Pattern for building an object from a more complex object 2009-07-16

    I'm in the middle of refactoring some code on my current project, and I'd like some input on if any design pattern exists for the following scenario. I have a class which executes some logic and returns an object containing the results of said logic;

  • JavaScript - Building JSON object 2010-03-26

    I'm trying to understand how to build a JSON object in JavaScript. This JSON object will get passed to a JQuery ajax call. Currently, I'm hard-coding my JSON and making my JQuery call as shown here: $.ajax({ url: "/services/myService.svc/PostComment"

  • ORM tool OR build the object to relational mapping layer manually 2010-07-01

    I am trying to determine what if any advantage or disadvantage there is to using an object to relational mapping layer like hibernate or the Microsoft Entity framework when building the data layer. Is using sql and mapping objects by hand better in t

  • build json object in javascript to post to php 2010-08-17

    I have a group of radio buttons in a form, I need to pass the values of the selected radion buttons to a php script. So I need to build a json object to post. I tried with this code, but I'm not sure. Please could anyone help me. result = []; for (i=

  • Building TimeZone object in Java 2010-12-22

    I build a Java TimeZone object using the TimeZone String like GMT-8,GMT,PST,EST etc. This did not consider whether the timezone is daylight saving enabled or not.Now there is a requirement to include this check and instead of giving the input as PDT

  • How can I build an Objective-C static library to distribute as a single binary and header file? 2010-12-30

    I'm building a static library, MyLibrary, for iOS in Objective-C that bundles together a dozen useful classes, each with its own .h file. I'd like to distribute MyLibrary as a single compiled binary, libMyLibrary.a, and a single .h header file, MyLib

  • UISplitView Interface Builder nib objects not allocated 2011-02-04

    I'm currently playing with UISplitView Controller as I've got to have some of them working in a UITabBarController. After a few tries, I've finally found a convenient way od doing it, the only issue I get is that I have to manually instanciate my det

  • How to build an object file in XCode 4? 2011-03-12

    How can I build a C/C++ Object file in XCode 4? In other words, I want to do the equivalent of: gcc -c main.c to generate a main.o. XCode 3.x used to have a "BSD/Object File" target type, but this seems to be absent from XCode 4.0. I've tried several

  • GUI Form Building uppon object (like Propertygrid) 2011-03-30

    I am wondering, what the design pattern is, when I have an object and want to be able to edit it in a GUI (dialogue, etc). I'm using C#.NET... Perhaps there are also some integrated features with visual studio 2008? By using a PropertyGrid, I can att

  • How can I use a foreach loop to build json objects and call jquery functions in razor 2011-05-31

    I want to take a list of objects and iterate through the list and build a json object and call a jquery function in a foreach loop of razor. @foreach (var item in Model.CoordinatesObj) { var pinpoint = { "top": item.Top, "left": item.Left, "width": i

Copyright (C), All Rights Reserved.

processed in 0.114 (s). 11 q(s)