In a C# (visual Studio 2010) project, I have a class with large number of properties, and I populate objects of this class with data and add to list object to pass it to display list of items (Something like results of a search).
My problem is there I don’t need all the properties of the class to display the above list, so do I have to create another class only with required field to display the (results) list? Is it correct according to the OOP concepts?
In a C# (visual Studio 2010) project, I have a class with large number
Share
Your properties object can implement multiple interfaces, as requested by different parts of your program.
If you define these interfaces:
And your properties class implements them like this:
Then you can pass this class to a different part of your app which has a method like this:
And the calling code will only see properties which belong to
IBasicInfo. Note that onlyIBasicInfoneeds to be public, and even thePropertiesclass is internal to ensure that no calling code can cast back to the actual implementation and mess with it.Alternatively, if your calling code contains completely different interfaces, then it is perfectly reasonable to convert them into Data Transfer Objects, and even modify their contents to match caller’s expectations (adapter pattern). There are tools which automate this task (Automapper, for example).