0

I have a dozen classes that inherit from the same baseclass. For each class, I want to change the state when a property updates, so I have a function called UpdateProperty(oldValue, newValue). I'm looking for a way to make these generic, so when I add a new class, I don't have to add a new function. This is what I have:

    public TicketType UpdateProperty(TicketType oldValue, TicketType newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }

    public Layout UpdateProperty(Layout oldValue, Layout newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }

    public Division UpdateProperty(Division oldValue, Division newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }

    public MasterDataList UpdateProperty(MasterDataList oldValue, MasterDataList newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }

    public MasterDataListItem UpdateProperty(MasterDataListItem oldValue, MasterDataListItem newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }


    public Ticket UpdateProperty(Ticket oldValue, Ticket newValue)
    {
        if (oldValue != newValue && ObjectState != ObjectState.New && ObjectState != ObjectState.Deleted)
        {
            ObjectState = ObjectState.Changed;
        }
        return newValue;
    }

As you can see, they all do the same. Could someone point me in the right direction?

Also see https://dotnetfiddle.net/xXgRuv

4
  • Please add some sample code how you are using this methods Commented Dec 13, 2017 at 10:38
  • I created a fiddle: dotnetfiddle.net/xXgRuv Commented Dec 13, 2017 at 11:06
  • I modified your fiddle with my suggested code dotnetfiddle.net/Edi9xo Commented Dec 13, 2017 at 11:17
  • Thank you! I selected your answer a solution :-) Commented Dec 13, 2017 at 11:31

2 Answers 2

2

Yes, but you have to make a generic method. You can put that in a base class if you need to:

public T UpdateProperty<T>(T oldValue, T newValue)
{
    if (!object.Equals(oldValue, newValue) && this.ObjectState != ObjectState.New && this.ObjectState != ObjectState.Deleted)
    {
        this.ObjectState = ObjectState.Changed;
    }

    return newValue;
}
Sign up to request clarification or add additional context in comments.

10 Comments

Why do you need a „generic base class“?
I meant that the code inside should be general / generic, not like "generics in c#" @SirRufo. Thanks for the hint.
Why does T needs to implement IObjectState?
It seemed to me OP was assigning that value to either oldValue.ObjectState or newValue.ObjectState, like in your answer. @SirRufo
I cannot see that in his or your code. Noone is using the ObjectState from newValue or oldValue. The base class has a property ObjectState and that is set
|
0

I would not only set the ObjectState, but also the property value inside the method.

Place the method inside the base class.

public abstract class BaseClass
{
    public ObjectState ObjectState { get; private set; }

    protected bool Set<T>( ref T field, T value )
    {
        if Equals( field, value ) return false;
        field = value;
        if ( ObjectState == ObjectState.Unchanged )
          ObjectState = ObjectState.Changed;
        return true;
    }
}

and in your inherited class

public class Foo : BaseClass
{
    private TicketType _ticketType;
    public TicketType TicketType 
    {
        get => _ticketType; 
        set => Set( ref _ticketType, value );
    }
}

1 Comment

When I change this, it tells me it cannot convert type bool to type TicketType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.