更新具有相同属性的不同对象

时间:2017-09-12 07:48:11

标签: c# object

在我的代码中,我有三个(或更多......)个不同的对象,它们都具有相同的属性(例如TrackingItemType,ReaderID等)

它们来自CounterBase(它没有所需的属性,但是其他属性)。

现在我想迭代这些对象的集合。在Collection中,可以发生所有三个对象。因此,目前我已经实施了" 更新"对于每个对象分开。

  

问题:如何避免写入重复的代码?有没有   模式可用于更新具有相同的不同对象   属性?!

谢谢!

感兴趣的方法:

    private void UpdateTrackingCounters(Reading readingData, TrackingItemType trackingItemType, string trackingCounterType)
    {
        try
        {
            foreach (CounterBase counter in this.TrackingCounters)
            {
                if (typeof(TrackingSimpleCounter).IsInstanceOfType(counter))
                {
                    TrackingSimpleCounter trackingCounter = (TrackingSimpleCounter)counter;

                    if (trackingCounter.TrackingCounterType == trackingCounterType)
                    {
                        if ((trackingCounter.ReaderID == 0
                            && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == 0
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            )
                        {
                            trackingCounter.IncreaseOne();
                        }
                    }
                }
                else if (typeof(TrackingIntervalBasedRollingCounter).IsInstanceOfType(counter))
                {
                    TrackingIntervalBasedRollingCounter trackingCounter = (TrackingIntervalBasedRollingCounter)counter;

                    if (trackingCounter.TrackingCounterType == trackingCounterType)
                    {
                        if ((trackingCounter.ReaderID == 0
                            && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == 0
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            )
                        {
                            trackingCounter.IncreaseOne();
                        }
                    }
                }
                else if (typeof(TrackingTriggerBasedRollingCounter).IsInstanceOfType(counter))
                {
                    TrackingTriggerBasedRollingCounter trackingCounter = (TrackingTriggerBasedRollingCounter)counter;

                    if (trackingCounter.TrackingCounterType == trackingCounterType)
                    {
                        if ((trackingCounter.ReaderID == 0
                            && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == 0
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == null)
                            || (trackingCounter.ReaderID == readingData.ReaderId
                                && trackingCounter.TrackingItemType == trackingCounter.TrackingItemType)
                            )
                        {
                            trackingCounter.IncreaseOne();
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.Trace.Error(ex);
        }
    }

2 个答案:

答案 0 :(得分:1)

定义包含所需属性和方法的接口:

interface ITrackable
{
    int ReaderID;
    string TrackingItemType;
    void IncreaseOne();
}

并将其添加到包含它们的每个类的声明中:

class TrackingSimpleCounter : CounterBase, ITrackable

class TrackingIntervalBasedRollingCounter: CounterBase, ITrackable

class TrackingTriggerBasedRollingCounter : CounterBase, ITrackable

如果所有类真正共享这些属性,则不必实现任何接口,因为它已经存在。

然后您需要的只是

foreach (ITrackable counter in this.TrackingCounters)
{
    if ((counter.ReaderID == 0 && counter.TrackingItemType == null)
    || (counter.ReaderID == 0 && counter.TrackingItemType == counter.TrackingItemType)
    || (counter.ReaderID == readingData.ReaderId && counter.TrackingItemType == null)
    || (counter.ReaderID == readingData.ReaderId && counter.TrackingItemType == counter.TrackingItemType)
    )
    {
        counter.IncreaseOne();
    }
}

虽然除非我误解你的逻辑,否则你真正需要的是:

foreach (ITrackable counter in this.TrackingCounters)
{
    if (counter.ReaderID == 0 || counter.ReaderID == readingData.ReaderID)
    {
        counter.IncreaseOne();
    }
}

或者如果你想使用LINQ:

foreach (var counter in this.TrackingCounters.OfType<ITrackable>().Where(c => c.ReaderID == 0 || c.ReaderID = readingData.ReaderID))
{
    counter.IncreaseOne();
}

答案 1 :(得分:0)

比化

您可以将公共属性提取到一个新的公共对象中,并使您的两个类都具有该对象的引用属性。

这样,当您更新此对象时,两个原始对象都将具有最新值,而不会有忘记更新其中一个属性的风险。