开发者

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

开发者 https://www.devze.com 2022-12-25 14:56 出处:网络
In an ETL application I am working on, we have three basic processes: Validate and parse an XML file of customer information from a third party

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 w开发者_如何学运维ould 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...


Add a class that inherits from Customer called MatchedCustomer (e.g.). Then step #2 becomes the process of promoting a Customer to a MatchedCustomer. You still need to write the code to do this; typically it's done in the constructor:

public class MatchedCustomer : Customer
{
    public MatchedCustomer(Customer customer)
    {
        // set properties from customer, i.e.
        FirstName = customer.FirstName;
    }
}


I didn't understand absolutely clear but it seemed that you need just to create a Proxy-class for your class with data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号