Search code examples
class-design

Class Design, which one is best design approach for this?


I am new to architecture design and need some help on this.

I have two class namely 'Part' and 'Supplier'. A part will have supplier.

In my class design, should i have 'int SupplierID' (type is 'int') or 'Supplier supplier' (type is 'Supplier' ) as my property in Part class ?

Which one is better? What is the Pros and Cons of them?

Kinldy provide your input on this.


Solution

  • Supplier supplier 
    

    Having Supplier as a type and having SupplierID as a property of Supplier would make more sense to me. The initial benefit is that you can do some basic validation on the supplier ID. Sure you are representing it as an int now but this could (and probably will) change in the future. For example, you may decide to represent the ID as a string and int internally but when reporting it you will represent it as a string: XYZ1234, where XYZ is the Supplier company name(string) and 1234 is the unique ID (int) (bad contrived example maybe, but it is still likely to change in some way)/

    The real advantage of having Supplier as a type is due to the fact you will be able to use Dependancy Injection to assign the Supplier to the Part when you create an instance of Part. So your constructor for Part should look like:

    Part(Supplier supplier)
    {
       _supplier = supplier;
    }
    

    Now your Part class is not dependant on changes in your Supplier class. I.e. it is not dependant on it.

    Note: If your not familiar with Dependancy Injection, this article from Martin Fowler should explain:

    http://martinfowler.com/articles/injection.html