0

I'm unsure of how to go about implementing a certain aspect of my objects.

Given a model class that represents an object, with business functionality based on various events, how can we graph the object without invoking those events?

Basic Model

public class MyObject
{
    ObservableCollection<RelatedEntity> RelatedEntities { get; set; }

    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public MyObject() {
        RelatedEntities.CollectionChanged += RelatedEntities_CollectionChanged;
    }

    private void RelatedEntities_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
        // do business logic stuff here based on change in collection
    }
}

public class RelatedEntity
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Now I'll have some provider that will fill an instance of MyObject so I can use it:

    public static MyObject GetMyObject() {

        MyObject mine = new MyObject();

        // fill properties, including related entities

        return mine;
    }

The problem here is that the provider is going to causing the business logic to be invoked as it graphs my instance, but that's not really what I want. I just want to get a full instance without it acting like it should...

I considered putting an Initializing flag in there, something like this:

public class MyObject
{
    ObservableCollection<RelatedEntity> RelatedEntities { get; set; }

    bool _initializing = false;

    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public MyObject(IProvider provider) {

        RelatedEntities.CollectionChanged += RelatedEntities_CollectionChanged;

        _initializing = true;
        provider.FillObject(this);
        _initializing = false;

    }

    private void RelatedEntities_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
        if (_initializing) return;
        // do business logic stuff here based on change in collection
    }
}

(ok, so the above is a poor example, as I could easily just move the event subscription below the initialization routine... but on a far more general level, there's business logic that could/would be applied throughout the model class.

Doing it this way with some sort of initialization flag becomes of awful lot of work.

Another approach I've tried to take is to drive the business logic through specific methods:

public class MyObject
{
    public ReadOnlyCollection<RelatedEntity> RelatedEntities { get; private set; }  

    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public void AddRelatedEntity(RelatedEntity entity) {
        // add to collection
        // invoke business logic
        // instance graphing would not call on this
    }

}

(again, perhaps not the best example but it gets the point across)

However, this is just as messy and takes away severely from the natural/semantic usability of the model as a business logic representation.

Any advice on the best way to manage these types of scenarios?

  • I have to admit, I don't know what "graphing" is the way you're using it. – Erik Eidt Feb 25 '16 at 19:13
  • If you're going to have to persist your objects, why not start with ORM? They can persist and reload the objects only invoking your logic when things change rather than when they are cached, loaded into memory, or multiple objects are assembled into memory and related to each other. – Erik Eidt Feb 25 '16 at 19:14
  • Why do you need to invoke events / domain logic / business logic for functionality (e.g. graphing) that is unrelated to events / domain logic / business logic? – Lawrence Feb 26 '16 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.