Conversation
| namespace ObjectPrinting.Tests; | ||
|
|
||
| [TestFixture] | ||
| public class ObjectPrinterStandartSerializationTests : TestBase |
There was a problem hiding this comment.
Можно не называть каждый класс с "ObjectPrinter", мы и так в проекте ObjectPrinter
| public class Person | ||
| { | ||
| public Guid Id { get; set; } | ||
| public string Name { get; set; } | ||
| public int Age { get; set; } | ||
| public double Height { get; set; } | ||
| public string Email { get; set; } | ||
| public Person Parent { get; set; } | ||
| public List<Person> Children { get; set; } = []; | ||
| } | ||
|
|
||
| public class Employee : Person | ||
| { | ||
| public string Position { get; set; } | ||
| public decimal Salary { get; set; } | ||
| } | ||
|
|
||
| public class Company | ||
| { | ||
| public string Name { get; set; } | ||
| public Employee CEO { get; set; } | ||
| } |
There was a problem hiding this comment.
Давай лучше создадим отдельную папку для тестовых моделей и туда их вынесем
| TestEmployee = new Employee | ||
| { | ||
| Name = "Jane", | ||
| Age = 25, | ||
| Position = "Developer", | ||
| Salary = 50000.50m | ||
| }; | ||
|
|
||
| TestCompany = new Company | ||
| { | ||
| Name = "Test Corp", | ||
| CEO = new Employee { Name = "CEO", Age = 45 } | ||
| }; |
There was a problem hiding this comment.
Можно сразу при определении создавать объект, вроде нет никакого смысла это в SetUp делать
ObjectPrinting/ObjectExtensions.cs
Outdated
| return ObjectPrinter.For<T>().PrintToString(obj); | ||
| } | ||
|
|
||
| public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) |
There was a problem hiding this comment.
Функция конфигом называется, не порядок
ObjectPrinting/PrintingConfig.cs
Outdated
| public string PrintToString(TOwner obj) | ||
| { | ||
| visitedObjects.Clear(); | ||
| return PrintToString(obj, 0); | ||
| } |
There was a problem hiding this comment.
Почему тут не принимать nestingLevel?
There was a problem hiding this comment.
Действительно. Перепишу что бы можно было принимать
ObjectPrinting/ObjectExtensions.cs
Outdated
| public static string PrintToString<T>(this T obj) | ||
| { | ||
| return ObjectPrinter.For<T>().PrintToString(obj); | ||
| } | ||
|
|
||
| public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) |
|
|
||
| namespace ObjectPrinting; | ||
|
|
||
| public class PropertySerializingConfig<TOwner, TProp> |
There was a problem hiding this comment.
Давай конфиги тоже в отдельную папочку
ObjectPrinting/PrintingConfig.cs
Outdated
| var finalTypes = new[] | ||
| { | ||
| typeof(int), typeof(double), typeof(float), typeof(string), | ||
| typeof(DateTime), typeof(TimeSpan), typeof(Guid), typeof(decimal), | ||
| typeof(long), typeof(short), typeof(byte), typeof(bool), | ||
| typeof(char), typeof(sbyte), typeof(ushort), typeof(uint), | ||
| typeof(ulong) | ||
| }; |
There was a problem hiding this comment.
Можно вынести в "константы", чтобы в методе не создавать каждый раз
ObjectPrinting/PrintingConfig.cs
Outdated
| internal void AddPropertySerializer<TProp>(string propertyName, | ||
| Func<TProp, string> serializer) | ||
| { | ||
| propertySerializers[propertyName] = serializer; | ||
| } | ||
|
|
||
| internal void AddPropertyTrim(string propertyName, int maxLength) | ||
| { | ||
| propertyTrimLengths[propertyName] = maxLength; | ||
| } |
There was a problem hiding this comment.
А почему решил сюда вынести, а не оставить в PropertyConfig?
There was a problem hiding this comment.
Если оставлять в PropertyConfig то придеться извне изменять поля propertyTrimLengths и propertySerializers но мне кажетьс правельно оставить их private поэтому эти методы здесь. Или я как то неправильно понял вопрос?
There was a problem hiding this comment.
Хмм, мне что-то не очень нравится как сейчас Trim реализован, это прям отдельная какая-то логика, хотя кажется можно завести её на рельсах "сериализаторов". То есть сейчас ради этого создаются отдельные методы, словари (propertyTrimLengths). Мб как то иначе можно? Обрезать текст по сути равно же применить какую-то функцию к свойству (функция которая будет обрезать как раз)
| var printer = ObjectPrinter.For<Person>().ExcludeType<Guid>(); | ||
| var result = printer.PrintToString(TestPerson); | ||
|
|
||
| result.Should().NotContain("Id = ").And.Contain("Name = John Doe"); |
There was a problem hiding this comment.
Как думаешь PrintingConfig получиться сделать immutable? Например, какое сейчас будет поведение если сделать так:
var printer = ObjectPrinter.For<Person>().ExcludeType<Guid>();
var printer2 = printer.ExcludeType<int>();
Буду ли printer и printer2 одним и тем же объектом? Если да, то получится избавиться от такого?
There was a problem hiding this comment.
Да сейчас PrintingConfig mutable, поэтому printer и printer2, будут одним и тем же объектом. Попробую переписать чтобы PrintingConfig стал Immutable, тогда каждый метод конфигурации должен создавать новый PrintingConfig а не менять и возвращать существующий
@ksamnole