Home > Back-end >  How to use classes interchangeably when the properties are different
How to use classes interchangeably when the properties are different

Time:06-29

I am creating a C# console app for reading pipe-delimited text files and import into database. I get a list (a text file) of suppliers, from which the organization buys office accessories along with the accessories they supplied (Drawers to Pins, you name it). The list contains both Organizations and individuals that meet these demands. The text file has FirstName, LastName, MiddleName columns. For individual suppliers' data, this is good. But if its an organization, then only the LastName column has the data, with the organization name. Other than this there is one more column called supplierType that helps me differentiate the individual supplier and organization (SupplierType 1 means individual, 2 means organization)

Now I created a Person and Organization classes to be used in the supplier class.

class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string? MiddleName { get; set; }
        //public string FullName { get; }
    }

class Organization
    {
        public string OrganizationName { get; set; }
    }

In the supplier class I want to use these classes interchangeably based on the supplierType.(like the strategy pattern) Since the properties are different in person and Organization class I am not clear how to accomplish this

class Supplier()
{
   //Read text file
  //int SupplierType = Assign the supplierType value that is read from txt file.
//Something like the below.
ISupplier = if SupplierType is 1 then assign person class, if 2 assign Organization class

if ISupplier is person then ISupplier.firstname, lastname has to be populated
if ISupplier is organization then ISupplier.Organization name has to be populated

 
 List<Product> Products;
}

I dont know whether this is a good design or probably there is a better way. Please help

CodePudding user response:

You could use Generics to add the type to the Supplier class. Then implement some type of interface for validation. e.g.

public interface IValidating {
    bool IsValid();
}

public class Person : IValidating
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string? MiddleName { get; set; }
    //public string FullName { get; }
    public bool IsValid()
    {
        return !string.IsNullOrEmpty(FirstName) && !string.IsNullOrEmpty(LastName);
    }
}

public class Organization : IValidating
{
    public string OrganizationName { get; set; }
    public bool IsValid()
    {
        return !string.IsNullOrEmpty(OrganizationName);
    }
}


public class Supplier<T> where T: IValidating, new() {
    T Entity { get; set; }
    List<Product> Products { get; set; }
}

Then you can check validation by calling IsValid on the Entity object etc...

CodePudding user response:

Person : ISupplier

Organization : ISupplier

ISupplier like this:

SupplierType
IEnumerable<string> GetSupplierInfo() 

or

void SetSupplierInfo(IEnumerable<string> names)

maybe i misunderstand sorry in that case

  • Related