I like controlling object access through returned interfaces. Its nice that the compiler controls access and visibility.
However when it comes to serialization the compiler isn't there to referee. So I think my question is a moot one. As soon as the Json.net serializer issues...
return base.Serializer._contractResolver.ResolveContract(value.GetType());
It's pretty much a done deal.
Sure, you can jack with things and poke things, but if you can't do it globally based on type I don't really see the point. So I'm going with something like this based on Cam's suggestion.
public interface IClientDTO {
string FirstName { get; }
string LastName { get; }
}
public interface IAdminDTO {
string FirstName { get; }
string LastName { get; }
string SSN { get; }
}
class ClientDTO : IClientDTO {
public string FirstName { get; set; }
public string LastName { get; set; }
}
class AdminDTO : IAdminDTO {
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
}
class Model {
class DTO {
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
}
private DTO dto = new DTO() { FirstName = "John", LastName = "Doe", SSN = "111001111" };
public IClientDTO ClientDTO { get { return new ClientDTO() { FirstName = dto.FirstName, LastName = dto.LastName }; } }
public IAdminDTO AdminDTO { get { return new AdminDTO() { SSN = dto.SSN, FirstName = dto.FirstName, LastName = dto.LastName }; } }
}
class Program {
static void Main(string[] args) {
Model model = new Model();
string sClientDTO = JsonConvert.SerializeObject(model.ClientDTO);
string sAdminDTO = JsonConvert.SerializeObject(model.AdminDTO);
Debug.WriteLine(sClientDTO);
Debug.WriteLine(sAdminDTO);
}
}
Sure it's a little clunky and repetitive, but it still allows me to maintain access through the interfaces and I can let the serializer do its thing unhindered, and I suspect it will be easier to maintain in the long run.