How to restrict access to a class's data based on state?

Posted by Marcus Swope on Stack Overflow See other posts from Stack Overflow or by Marcus Swope
Published on 2010-04-01T14:02:22Z Indexed on 2010/04/01 16:33 UTC
Read the original article Hit count: 336

In an ETL application I am working on, we have three basic processes:

  1. Validate and parse an XML file of customer information from a third party
  2. Match values received in the file to values in our system
  3. Load customer data in our system

The issue here is that we may need to display the customer information from any or all of the above states to an internal user and there is data in our customer class that will never be populated before the values have been matched in our system (step 2). For this reason, I would like to have the values not even be available to be accessed when the customer is in this state, and I would like to have to avoid some repeated logic everywhere like:

if (customer.IsMatched) DisplayTextOnWeb(customer.SomeMatchedValue);

My first thought for this was to add a couple interfaces on top of Customer that would only expose the properties and behaviors of the current state, and then only deal with those interfaces. The problem with this approach is that there seems to be no good way to move from an ICustomerWithNoMatchedValues to an ICustomerWithMatchedValues without doing direct casts, etc... (or at least I can't find one).

I can't be the first to have come across this, how do you normally approach this?

As a last caveat, I would like for this solution to play nice with FluentNHibernate :)

Thanks in advance...

© Stack Overflow or respective owner

Related posts about design-patterns

Related posts about fluent-nhibernate