Note that there are some explanatory texts on larger screens.

plurals
  1. POInteresting API Design/Pattern
    text
    copied!<p>I am re-designing part of our internal ORM tool, and I want to expose Field (a class that represents a field in the database, like CustomerFirstName) directly to the end-developer.</p> <p>So that was easy enough to accomplish, however the API got a little ugly because this Field class was previously used internally and is too open. For instance, and this is just one small example: the IsDirty property was not read-only, and that's something end-developers shouldn't be able to tamper with.</p> <p>I thought about maybe creating two interfaces, IPublicField and IPrivateField, and trying to get the field class to implement both of them. However, continuing with the IsDirty example, I didn't want something like this:</p> <pre><code>Public ReadOnly Property PrivateIsDirty Implements IPrivateField.IsDirty ... End Property Public Property IsDirty Implements IPublicField.IsDirty ... End Property </code></pre> <p>...It's just a little ugly, plus you could still cast back to the Field class and get into the non-readonly method. I also didn't want to introduce a seperate setter method because it would be another breaking change that I don't want to think about, and it also would create an inconsistency with other parts of the API.</p> <p>I ended up renaming the Field class to InnerField, and creating a facade/wrapper style structure around it like this:</p> <pre><code>Public Class Field Implements BusinessObjects.IField Private InnerField As BusinessObjects.IInnerField Public Sub New(ByVal field As IInnerField) InnerField = field End Sub ... Public ReadOnly Property IsDirty() As Boolean Implements BusinessObjects.IField.IsDirty Get Return InnerField.IsDirty End Get End Property ... End Class </code></pre> <p>This seems to be working out pretty well. Internally, the InnerField is suitably open and we have the freedom to make it more open in the future without impacting end-developers, and externally the Field class provides the simplified, locked down tool that end-developers need.</p> <p>So, assuming that was coherent, I was wondering how you might have proceeded in this situation, and whether my solution seems reasonable from the outside.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload