How to use classes interchangeably when the properties are different

Viewed 63

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()
{
 int SupplierType
//Something like the below.
 ISupplier supplier;
 List<Product> Products;
}

Void Main()
{
Read a line from the text file
Supplier purchases = new Supplier()
purchases.SupplierType = Get the supplierType from the line.
purchases.supplier = if SupplierType is 1 then assign person class, if 2 assign Organization class

//How to accomplish the above when the properties are changing between person and Organization class

}

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

2 Answers

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...

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