Nekeniehl
Nekeniehl

Reputation: 1691

Best practices for common logic on classes

I have a big big class, around 4000 thousands lines, and I think it can be another way to do the following.

It is a class for serialize a message, the nested classes are always with the same structure and overrides the ToString and Equal methods. There are simple classes like the following and complex classes which implements those simples classes in properties.

My idea is something like an extension class who implements the override and extend the simples classes with it

Example class

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class ID
{
    #region Public Properties

    [XmlAttribute("ID")]
    public String Value { get; set; }

    #endregion

    #region Public Methods and Operators

    public static bool operator == (ID left, ID right)
    {
        return Equals(left, right);
    }

    public static bool operator != (ID left, ID right)
    {
        return !Equals(left, right);
    }

    public override bool Equals (object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != this.GetType())
        {
            return false;
        }
        return Equals((ID) obj);
    }

    public override int GetHashCode ()
    {
        return (Value != null
                    ? Value.GetHashCode()
                    : 0);
    }

    public override String ToString ()
    {
        return Value;
    }

    #endregion

    #region Methods

    protected bool Equals (ID other)
    {
        return string.Equals(Value, other.Value);
    }

    #endregion
}

My idea

public class FieldExtension
{
    public String Value { get; set; }

    #region Public Methods and Operators

    public static bool operator == (FieldExtension left, FieldExtension right)
    {
        return Equals(left, right);
    }

    public static bool operator != (FieldExtension left, FieldExtension right)
    {
        return !Equals(left, right);
    }

    public override bool Equals (object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (obj.GetType() != this.GetType())
        {
            return false;
        }
        return Equals((ID) obj);
    }

    public override int GetHashCode ()
    {
        return (Value != null
                    ? Value.GetHashCode()
                    : 0);
    }

    public override String ToString ()
    {
        return Value;
    }

    #endregion

    #region Methods

    protected bool Equals (ID other)
    {
        return string.Equals(Value, other.Value);
    }

    #endregion
}

public class ID: FieldExtension
{
    #region Public Properties

    [XmlAttribute("ID")]
    public String Value { get; set; }

    #endregion
}

I don't know if this is possible and I just ask if some did that before or something like that or another ideas, if works will save me a lot of work

Thanks in advance

EDIT Example complex class

    [Serializable]
    [XmlType(AnonymousType = true)]
    [XmlRoot(Namespace = "", IsNullable = false)]
    public class Zählpunkt
    {
        #region Public Properties
        [XmlElement("ID")]
        public ID ID { get; set; }
        [XmlAttribute("Zählpunkt")]
        public String Value { get; set; }
        #endregion
..

Upvotes: 0

Views: 150

Answers (1)

Guffa
Guffa

Reputation: 700670

You would make the base class FieldExtension abstract, and the Value property abstract:

public abstract class FieldExtension
{
    public abstract String Value { get; set; }
    ...

Then in the ID class you override the property:

override public String Value { get; set; }

That makes the code in the base class use the value that you implement in the class that inherits it.

Upvotes: 1

Related Questions