Kliver Max
Kliver Max

Reputation: 5299

How to handle classes with different properties but same meanings?

I have a lot of classes that represent a Person, like:

public class Person1 {
   public string Name {set;get;}
   public string FamilyName {set;get;}
   public string Patronymic {set;get;}
}

public class Person2 {
   public string FirstName {set;get;}
   public string SurName {set;get;}
   public string Patronymic {set;get;}
}

public class Person3 {
   public string PersonName {set;get;}
   public string Surname {set;get;}
   public string Patr {set;get;}
}

etc..

Now I want to combine fields and get fullname = FamilyName + " " + Name + " " + Patronymic;, but I want to do it in the same code.
I see two ways to do it.

  1. Make helper class with method:
public class PersonHelper
{
  public string GetFullName(string familyName, sting name, string patronymic) 
  {
     return FamilyName + " " + Name + " " + Patronymic;
  }
}
  1. Use interface and make realisation of GetFullName in each class:
interface IFullNamed
{
   string GetFullName()
}

public class Person1: IFullNamed{
   public string Name {set;get;}
   public string FamilyName {set;get;}
   public string Patronymic {set;get;}

  string FullName(){
     return FamilyName + " " + Name + " " + Patronymic;
  }
}

public class Person2:IFullNamed{
   public string FirstName {set;get;}
   public string SurName {set;get;}
   public string Patronymic {set;get;}

   string FullName(){
     return SurName + " " + FirstName + " " + Patronymic;
   }
}

What way is correct or both wrong?

Upvotes: 1

Views: 106

Answers (3)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23208

You can extend the approach with helper method and create an extension method to build a full name for every person

public static class Ext
{
    public static string GetFullName(this Person1 person)
    {
        return $"{person.FamilyName} {person.Name} {person.Patronymic}";
    }

    public static string GetFullName(this Person2 person)
    {
        return $"{person.SurName} {person.FirstName} {person.Patronymic}";
    }
}

Since string class is immutable in C#, it's better to use string interpolation or string.Format() method rather then concatenate them (to avoid an additional allocation of strings).

But in terms of basic architecture principles make sense to create a base class or interface to encapsulate the fields, identifying a Person entity

public class Person 
{
    public string Name { set; get; }
    public string FamilyName { set; get; }
    public string Patronymic { set; get; }

    public override string ToString()
    {
        return $"{FamilyName} {Name} {Patronymic}";
    }
}

If making a single entity isn't an option here, you can just override ToString() method to encapsulate a logic of getting a full name.

Upvotes: 1

Nguyen Van Thanh
Nguyen Van Thanh

Reputation: 825

I think your trouble is mount of properties in each classes are different.

You can create class PersonHelper and write GetFullname method use params string[] in args:

In this method, you can:

Pass mount of args you want

// You can pass many arguments you want here
string fullNameOfPerson3 = PersonHelper.GetFullName(person3.FirstName, person3.SurName, person3.Patronymic);

// And get them by using this method
string GetFullName(params string[] args)

Full method:

    public static class PersonHelper
    {
        public static string GetFullName(params string[] args)
        {
            string result = "";
            for (int i = 0; i < args.Length; i++)
            {
                result += i == args.Length - 1 ? args[i] : args[i] + " ";
            }
            return result;
        }
    }

Full code: (this is only a very simple example. Try the way with your project)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayFromTextFile
{
    class Program
    {
        static void Main(string[] args)
        {
            Person1 person1 = new Person1();
            Person2 person2 = new Person2();
            Person2 person3 = new Person2();

            string fullNameOfPerson1 = PersonHelper.GetFullName(person1.Name, person1.FamilyName, person1.Patronymic);
            string fullNameOfPerson2 = PersonHelper.GetFullName(person2.FirstName, person2.SurName, person2.Patronymic);
            string fullNameOfPerson3 = PersonHelper.GetFullName(person3.FirstName, person3.SurName, person3.Patronymic);

        }
    }

    public static class PersonHelper
    {
        public static string GetFullName(params string[] args)
        {
            string result = "";
            for (int i = 0; i < args.Length; i++)
            {
                result += i == args.Length - 1 ? args[i] : args[i] + " ";
            }
            return result;
        }
    }
    public class Person1
    {
        public string Name { set; get; }
        public string FamilyName { set; get; }
        public string Patronymic { set; get; }
    }

    public class Person2
    {
        public string FirstName { set; get; }
        public string SurName { set; get; }
        public string Patronymic { set; get; }
    }

    public class Person3
    {
        public string PersonName { set; get; }
        public string Surname { set; get; }
        public string Patr { set; get; }
    }
}

Upvotes: 0

Gauravsa
Gauravsa

Reputation: 6514

I would do:

interface IFullNamed
{
    string GetFullName();
}

public abstract class Person : IFullNamed
{
    public virtual string Name { set; get; }
    public virtual string FamilyName { set; get; }
    public virtual string Patronymic { set; get; }

    string IFullNamed.GetFullName()
    {
        return $"{FamilyName} {Name} {Patronymic}";
    }
}

public class Person1 : Person
{
    [DisplayName("First Name")]
    public override string Name { set; get; }
    [DisplayName("SurName")]
    public override string FamilyName { set; get; }
}

public class Person2 : Person
{
    [DisplayName("PersonName")]
    public override string Name { set; get; }
    [DisplayName("Surname")]
    public override string FamilyName { set; get; }
    [DisplayName("Patr")]
    public override string Patronymic { set; get; }
}

public class Person3 : Person
{
    [DisplayName("PersonName")]
    public override string Name { set; get; }
    [DisplayName("Surname")]
    public override string FamilyName { set; get; }
    [DisplayName("Patr")]
    public override string Patronymic { set; get; }
}

Upvotes: 1

Related Questions