I have an unknown domain model that uses abstract repositories to load domain objects. The specific implementation of my repositories (data access level (DAL)) uses the entity infrastructure to retrieve data from the sql server database. The database has length limits for many varchar columns. Now imagine that I have the following domain class:
public class Case { public Case(int id, string text) { this.Id = id; this.Text = text; } public int Id { get; private set; } public string Text { get; set; } }
And the abstract repository is defined as follows:
public abstract class CaseRepository { public abstract void CreateCase(Case item); public abstract Case GetCaseById(int id); }
The [text] column of a table in sqlserver is defined as nvarchar(100)
Now I know that I mentioned that my domain class ( Case ) was insatiable, but I feel that it is wrong that it allows for text parameter values ββthat ultimately cannot be stored in my specific repository implementation, since the structure entities will throw an exception when assigning the text property to a class of the generated entity environment if it is longer than 100 characters. Therefore, I decided that I want to check this restriction in the domain model, since this allows me to check the validity of the data before trying to transfer it to the DAL and, thus, make the error message more oriented to the domain object. I think you could argue that I can just check the constraint in my constructor and in the property adjuster, but since I have hundreds of classes, all have similar limitations, I wanted a more general way to solve the problem
Now what I came up with is a class called ConstrainedString , which is defined as follows:
public abstract class ConstrainedString { private string textValue; public ConstrainedString(uint maxLength, string textValue) { if (textValue == null) throw new ArgumentNullException("textValue"); if (textValue.Length > maxLength) throw new ArgumentException("textValue may not be longer than maxLength", "textValue"); this.textValue = textValue; this.MaxLength = maxLength; } public uint MaxLength { get; private set; } public string Value { get { return this.textValue; } set { if (value == null) throw new ArgumentNullException("value"); if (value.Length > this.MaxLength) throw new ArgumentException("value cannot be longer than MaxLength", "value"); this.textValue = value; } } }
In addition, I have an implementation of ConstrainedString called String100 :
public class String100 : ConstrainedString { public String100(string textValue) : base(100, textValue) { } }
Thus, this will lead to another Case implementation that will look like this:
public class Case { public Case(int id, String100 text) { this.Id = id; this.Text = text; } public int Id { get; private set; } public String100 Text { get; set; } }
Now, my question is: Can I ignore some built-in classes or some other approach that I could use instead? Or is this a reasonable approach?
Any comments and suggestions are welcome.
Thank you in advance