Reputation: 1049
One of my classes require that a default constructor be used for serialization purposes. However, since some of the properties are required, what is the best way to approach this such that I can require that properties be set.
From where I am standing, I see two possible scenarios:
Upvotes: 2
Views: 549
Reputation: 5083
I would keep it simple and don't mess with attributes.
public interface IDataValidator
{
void ValidateData();
}
public string Serialize<T>(T obj):where T:IDataValidator
{
obj.ValidateData();
return Serialize(obj);
}
public T Deserialize<T>(string serializedObj):where T:IDataValidator
{
T obj = Deserialize(serializedObj);
obj.ValidateData();
}
public class Book : IDataValidator
{
public string Isbn {get;set;}
public Book(){}
public Book(string isbn)
{
Isbn = isbn;
}
public void ValidateData()
{
if(string.IsNullOrEmptyOrWhiteSpace(Isbn)
throw new ApplicationException("...");
}
}
Upvotes: 3