Changing type, the state pattern and LINQ to SQL
A question I see from time-to-time on LINQ to SQL relates to changing an entity’s class.
C# and VB.NET don’t allow a class to change its type at run-time and LINQ to SQL specifically doesn’t provide a mechanism for changing the underlying discriminator for this reason.
Discarding the current object and creating a new one is fraught with issues. What do we do about existing references, unsaved data, established associations and caches?
Start with an example
Consider an abstract Account class with SavingsAccount and CurrentAccount sub-classes. Bank accounts don’t change type once created (in my experience) so that’s good so far.
When we get into processing and validation logic its tempting to create ClosedAccount and OpenAccount classes but what happens during execution when closing an account?
A further consideration is how exactly ClosedAccount and OpenAccount fit into the hierarchy given the single-inheritance limitation of C# and VB.NET.
Enter the State Pattern
The ever-resourceful Gang of Four describe the State Pattern as:
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Taking the bank accounts example and applying the state pattern gives:
Account no longer needs to change type at run-time yet is still able to have clients call Validate and process Methods that end up in discrete methods as if inheritance had been used.
To achieve this we have:
- Created a State hierarchy that contains the logic we need to change at run-time
- Introduced a private member in Account that points to the current state instance
- Ensured the Account methods call the current state class via the private member
Because the state member is private and only accessed by Account itself we can happily create and dispose it as the conditions that affect the state change as much as we like without worrying about references to it.
This is best illustrated with code. Here rather than just calling the state’s validation logic there is a combination of core Account validation (balance), state validation (closed) and CheckingAccount validation (transaction limits):
public abstract class Account {
private AccountState state;
public virtual Status Validate(ITransaction tx) {
Status result = state.Validate(tx);
if (tx.Amount > Balance)
result.Add(TransactionFailures.InsufficientFunds);
return result;
}
}
public class SavingsAccount : Account {
public override Status Validate(ITransaction tx) {
Status result = base.Validate(tx);
if (Transactions.Count > TransactionLimit)
result.Add(TransactionFailures.TransactionLimitReached);
return result;
}
}
public class ClosedAccountState : AccountState {
public override Status Validate(ITransaction tx) {
return new Status(TransactionFailures.InvalidSourceAccount);
}
}
This is less complex than selectively replacing objects within our application at run-time and can bring additional benefits:
- Single responsibility principle – logic relating to a specific state has its own class to live in
- Favor composition over inheritance – inheritance for OpenSavingsAccount, ClosedCheckingAccount etc. is complex
Like all guidance, patterns and principles do not blindly follow these guidelines or patterns but consider how it affects and fits with your application. For this particular example it not only solves the problem but helps maintainability – at least at this simple stage. Once Validation becomes sufficiently complex it would likely move out entirely into a new set of orchestrated classes just for that.
With LINQ to SQL (and other mappers)
Moving this example into an object-relational mapper requires two – not unexpected – database-mapped properties.
- The inheritance discriminator (Type)
- A state indicator (Active)
The only thing we need to ensure is the Account’s state member always refers to either a ClosedAccountState or OpenedAccountState depending upon the Active flag.
Given that LINQ to SQL code-generates the property for Active we could:
- Make Active private, wrap it in another property and set the state member when it changes and at initialization
- Make the state member a read-only property instead of an instance variable
The second works well here and given that AccountState is itself stateless (perhaps not the best choice of name) we can use a singleton to avoid multiple instances. The state instance variable in the Account class is replaced with:
private AccountState State {
get {
if (Active)
return OpenAccountState.Instance;
else
return ClosedAccountState.Instance;
}
}
The code continues to work and now changing the Active flag results in different behavior.
Best of all we still have the code in separate classes, no switch/case/if statements relating to validation or account types, a clean inheritance hierarchy and no running around trying to invalidate existing references.
Hitting the discriminator directly
There may be times when claims are made that a type has to change – perhaps data was entered incorrectly.
Before delving into the database or providing a tool to flip the underlying discriminator value consider:
- Does the new class interpret the same data in a different manner? Has a $1,000 credit limit just become a 1,000 transactions per-month limit?
- Would the object be valid in the new class? Did a ProposedCustomer just become ApprovedCustomer without a policy-enforced credit check?
- Are associations still honored? Are 300 unshipped orders for a GameProduct still honored for a BookProduct?
If in doubt don’t do it.
An inconsistent database bleeding through your application isn’t good for anyone and will take a lot longer to sort out than setting up a new entity.
[)amien
8 responses