My domain model consists mostly of simple DTOs, i.e. 'Data Transfer Objects' which this article distinguishes from 'Plain Old C# Objects', like this one:
public class Settings
{
public bool FullScreen = false;
public int WindowWidth = 800;
public int WindowHeight = 600;
}
I realize this model is "anemic," but it works well with the JSON serializer I use for persistence. Also, I'm still in the exploratory stage of development and I read that it's okay at this point to have a bunch of "property bags."
Other classes look less like DTOs and more like POCOs (or POJOs although I'm using C#):
public class Sprite
{
[JsonProperty]
private readonly string assetName;
[JsonProperty]
private Vector2 position;
// other properties...
public Sprite( string assetName, Vector2 position )
{
this.assetName = assetName;
this.position = position;
}
public void Move( Vector2 direction )
{
// do move
}
// other methods...
}
Because this type of object needs to be persisted by the JSON serializer, the properties need either to be public or have a JsonProperty attribute. I went with the attribute in this case because I want to restrict access to its properties.
Is this a good design? Does it make sense for my "entities" (I'm not sure if that's the correct term) to look so different from one another? My gut told me no, so I tried extracting the stuff not needed by the serializer into a separate class outside of the namespace:
public class SpriteModel
{
private readonly Sprite sprite;
public SpriteModel( Sprite sprite )
{
this.sprite = sprite;
}
public void Move( Vector2 direction )
{
// do move
}
public void GetSprite()
{
return sprite;
}
// other methods...
}
Now the Sprite class looks more like a DTO:
public class Sprite
{
public string AssetName;
public Vector2 Position;
// other properties...
}
Someone told me that my domain model is actually the SpriteModel
class now and not the Sprite
class itself. I'm obviously confused about what to call these things exactly, and any clarification would be appreciated.
But my main question is: which of these approaches is better and why? Are they both terrible? And in that case is there another approach that I should use instead? If I go with the second approach, then I could have SpriteModel
implement ISpriteModel
, and the classes that use it could be more loosely coupled, which is a nice side benefit I think. But I don't want to go that route just for a side benefit that might not actually pay off (and could just add more complexity).