I have a class that upon construction, loads it’s info from a database. The info is all modifiable, and then the developer can call Save() on it to make it Save that information back to the database.
I am also creating a class that will load from the database, but won’t allow any updates to it. (a read only version.) My question is, should I make a separate class and inherit, or should I just update the existing object to take a readonly parameter in the constructor, or should I make a separate class entirely?
The existing class is already used in many places in the code.
Thanks.
Update:
Firstly, there’s a lot of great answers here. It would be hard to accept just one. Thanks everyone.
The main problems it seems are:
- Meeting expectations based on class names and inheritance structures.
- Preventing unnecessary duplicate code
There seems to be a big difference between Readable and ReadOnly. A Readonly class should probably not be inherited. But a Readable class suggests that it might also gain writeability at some point.
So after much thought, here’s what I’m thinking:
public class PersonTestClass
{
public static void Test()
{
ModifiablePerson mp = new ModifiablePerson();
mp.SetName("value");
ReadOnlyPerson rop = new ReadOnlyPerson();
rop.GetName();
//ReadOnlyPerson ropFmp = (ReadOnlyPerson)mp; // not allowed.
ReadOnlyPerson ropFmp = (ReadOnlyPerson)(ReadablePerson)mp;
// above is allowed at compile time (bad), not at runtime (good).
ReadablePerson rp = mp;
}
}
public class ReadablePerson
{
protected string name;
public string GetName()
{
return name;
}
}
public sealed class ReadOnlyPerson : ReadablePerson
{
}
public class ModifiablePerson : ReadablePerson
{
public void SetName(string value)
{
name = value;
}
}
Unfortunately, I don’t yet know how to do this with properties (see StriplingWarrior’s answer for this done with properties), but I have a feeling it will involve the protected keyword and asymmetric property access modifiers.
Also, fortunately for me, the data that is loaded from the database does not have to be turned into reference objects, rather they are simple types. This means I don’t really have to worry about people modifying the members of the ReadOnlyPerson object.
Update 2:
Note, as StriplingWarrior has suggested, downcasting can lead to problems, but this is generally true as casting a Monkey to and Animal back down to a Dog can be bad. However, it seems that even though the casting is allowed at compile time, it is not actually allowed at runtime.
A wrapper class may also do the trick, but I like this better because it avoids the problem of having to deep copy the passed in object / allow the passed in object to be modified thus modifying the wrapper class.
The Liskov Substitution Principle says that you shouldn’t make your read-only class inherit from your read-write class, because consuming classes would have to be aware that they can’t call the Save method on it without getting an exception.
Making the writable class extend the readable class would make more sense to me, as long as there is nothing on the readable class that indicates its object can never be persisted. For example, I wouldn’t call the base class a
ReadOnly[Whatever], because if you have a method that takes aReadOnlyPersonas an argument, that method would be justified in assuming that it would be impossible for anything they do to that object to have any impact on the database, which is not necessarily true if the actual instance is aWriteablePerson.Update
I was originally assuming that in your read-only class you only wanted to prevent people calling the
Savemethod. Based on what I’m seeing in your answer-response to your question (which should actually be an update on your question, by the way), here’s a pattern you might want to follow:This ensures that a truly
ReadOnlyPersoncannot simply be cast as a ModifiablePerson and modified. If you’re willing to trust that developers won’t try to down-cast arguments in this way, though, I prefer the interface-based approach in Steve and Olivier’s answers.Another option would be to make your
ReadOnlyPersonjust be a wrapper class for aPersonobject. This would necessitate more boilerplate code, but it comes in handy when you can’t change the base class.One last point, since you enjoyed learning about the Liskov Substitution Principle: By having the Person class be responsible for loading itself out of the database, you are breaking the Single-Responsibility Principle. Ideally, your Person class would have properties to represent the data that comprises a “Person,” and there would be a different class (maybe a
PersonRepository) that’s responsible for producing a Person from the database or saving a Person to the database.Update 2
Responding to your comments:
ReadablePersonclassabstractbecause it seemed like you’d only ever want to create a person that is read-only or one that is writeable. Even though both of the child classes could be considered to be a ReadablePerson, what would be the point of creating anew ReadablePerson()when you could just as easily create anew ReadOnlyPerson()? Making the class abstract requires the user to choose one of the two child classes when instantiating them.In my mind, the Person class would just be a POCO, with no logic in it: just properties. The repository would be responsible for building the Person object. Rather than saying:
… and allowing the Person object to go to the database to populate its various properties, you would say:
The PersonRepository would then get the appropriate information out of the database and construct the Person with that data.
If you wanted to call a method that has no reason to change the person, you could protect that person from changes by converting it to a Readonly wrapper (following the pattern that the .NET libraries follow with the
ReadonlyCollection<T>class). On the other hand, methods that require a writeable object could be given thePersondirectly:The benefit of using interfaces is that you’re not forcing a specific class hierarchy. This will give you better flexibility in the future. For example, let’s say that for the moment you write your methods like this:
… but then in two years you decide to change to the wrapper implementation. Suddenly
ReadOnlyPersonis no longer aReadablePerson, because it’s a wrapper class instead of an extension of a base class. Do you changeReadablePersontoReadOnlyPersonin all your method signatures?Or say you decide to simplify things and just consolidate all your classes into a single
Personclass: now you have to change all your methods to just take Person objects. On the other hand, if you had programmed to interfaces:… then these methods don’t care what your object hierarchy looks like, as long as the objects you give them implement the interfaces they ask for. You can change your implementation hierarchy at any time without having to change these methods at all.