Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm currently learning about various application design approaches and there's one thing I can't really get my head around.

Let's say I have a physical device in my network, an IP surveillance camera. I need a model that represents 'IP surveillance cameras' to work with them in my application, which might look like this:

public class IPCamera {

    public string     Token      { get; set; }
    public IPAddress  IPAddress  { get; set; }
    public string     RtspUri    { get; set; }

}

Now, if I want to store the IPCamera into a database (e.g. with Entity Framework), I need maybe another properties and thus, another model with:

   public int         Id         { get; set; }

Oh.. I want to access my application with via a WCF service. I can't use the object "IPAddress" here because it's not very serialization friendly, hence I need another model with a custom IP class that stores the IP address as string.

And last, an Android clients wants to access my data. I design a RESTful API using WebApi2. The client isn't interested in the cameras RTSPUri, but wants to know about the current state (is online, is offline).

I'm wondering: How to name all these models that they don't come in conflict? Should I design a model per each purpose/layer/service? And how to link/map them? With the adapter pattern?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Now, if I want to store the IPCamera into a database (e.g. with Entity Framework), I need maybe another properties and thus, another model with...

No, you just need an ID property in your IPCamera class:

public class IPCamera {

    public int        ID         { get; set; }
    public string     Token      { get; set; }
    public IPAddress  IPAddress  { get; set; }
    public string     RtspUri    { get; set; }

}

I can't use the object "IPAddress" here because it's not very serialization friendly, hence I need another model ...

No, you just need a ToString() method in your IPAddress class.

The client isn't interested in the cameras RTSPUri, but wants to know about the current state (is online, is offline).

Add an isOnline() method to your IPCamera class?

share|improve this answer
    
For the last one I would suggest another method specifically for getting the status of the camera. Although, both the state of the ipcamera object and the status response may be invalid immediately after the response. –  crad Jan 16 at 23:59
    
@crad: The language that the OP chose seems to be C#; in C# a property would be perfectly valid. However, I made the change. –  Robert Harvey Jan 17 at 0:02

You could use inheritance as in

public class StateFullIPCamera : IPCamera {
  public bool isOnline { get; set; }
}

public class IPCamera : EntityBase {
  public string Token { get; set; }
  public IPAddess IPAddress { get; set; }
  ...
}

public class EntityBase {
  public int Id { get; set; }
}

Be careful though, if there are properties in the base classes which clients do not need, you should be returning them a different model. Never only populate some properties.

I upvoted Robert's answer because it is simple and makes sense. As I commented there, I would suggest another api method specifically for getting the state of the camera and leave it off the model entirely.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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