Misha
asked on
C# SOLID and best practices
Hello!
I need advice for SOLID principles and programming best practices.
I have two classes inherited from one main class. Objects of this classes are in List. I need beautifull solution to print information about this classes
Class Entity
Class Person
Class Car
Class Writer
In real case there is complex logic in method ToString. And some other objects are using in method ToString()
I need advice for SOLID principles and programming best practices.
I have two classes inherited from one main class. Objects of this classes are in List. I need beautifull solution to print information about this classes
Class Entity
class Entity
{
}
Class Person
sealed class Person : Entity
{
internal FirstName {get; set;}
internal LastName {get; set;}
public override string ToString()
{
return $"FirstName ={FirstName } LastName = {LastName } ";
}
}
Class Car
sealed class Car: Entity
{
internal Name {get; set;}
internal MaxSpeed {get; set;}
public override string ToString()
{
return $"Name ={Name } MaxSpeed = {MaxSpeed } ";
}
}
Class Writer
sealed class Writer
{
internal void Write()
{
List<Entity> entities= new List<Entity>();
// add some persons..
// add some cars...
StreamWriter writer = new StreamWriter("qwe.txt");
foreach (Entity e in entities)
{
writer.WriteLine(e.ToString());
}
}
}
In real case there is complex logic in method ToString. And some other objects are using in method ToString()
What problem are you trying to solve here. The code you already have will work. It will call ToString() on each of your derived classes, and output the correct info.
ASKER
Thank you for response!
@it_saige
There is complex logic in each overriding method ToString(). There is no identical to all classes method to print each property.
If I use your solutin I should write something like this:
I think it is not the best practices..
@Chris Stanyon
I want to find the best solution. My overriding method ToString() have a complex logic. I think, it is not good to stay this method in classes, which are only intended for storage data.
@All
I think there should be other classes for print Person , Car and other objects.... May be..
And each implements interface for printing special object .... May be...
@it_saige
There is complex logic in each overriding method ToString(). There is no identical to all classes method to print each property.
If I use your solutin I should write something like this:
public static string GetPropertiesString<T>(this T source)
{
if (source is Peson)
{
// print special logic for Peson
}
if (source is Car)
{
// print special logic for Car
}
/// a lot of other classes
}
I think it is not the best practices..
@Chris Stanyon
I want to find the best solution. My overriding method ToString() have a complex logic. I think, it is not good to stay this method in classes, which are only intended for storage data.
@All
I think there should be other classes for print Person , Car and other objects.... May be..
And each implements interface for printing special object .... May be...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very mach!
Open in new window
Produces the following output --saige-