How to format my class? With toString, in the GUI or in another way?

I have several classes in the library that present the results of analysis and verification of some files.

These classes contain enumerations, lists of invalid properties, etc.

I wrote a GUI application that uses a library, and wrote some functions for writing these classes in human-readable form in a rich text field.

It just seemed to me that I might have to write this formatting in an override of the ToString class.

However, all this formatting is very long, includes inserting tabs and new lines, includes several iterations over lists, retrieving description of enumerations, etc.

So, I was wondering - what is the standard for toString size and complexity? Should I write complex formatting in toString? Or maybe I should provide some other common interface - is there a common interface for formatted output for class printing? Or will I do this in a GUI application?

Thank!

+3
source share
2 answers

Things like user interface formatting should not be baked in any library except user interface libraries.

Instead, you can provide an agnostic set of user interface classes that can format your entities, as in, can execute the complex code needed to create a format that expects display in the user interface.

, :

public interface IEntityFormatter<T>
{
    string GetFormattedValue(T myEntity);
}

public class Customer
{
    public string FullName {get;set;}
}

public class CustomerFormatter : IEntityFormatter<Customer>
{
    public string GetFormattedValue(Customer myEntity)
    {
        return myEntity.FullName;
    }
}
+2

, ToString , ( , ). . , - , ToString .

0

All Articles