Skip to content

Commit

Permalink
Moves
Browse files Browse the repository at this point in the history
  • Loading branch information
disturm committed Oct 2, 2016
1 parent c8f76c7 commit 7e9b0e1
Show file tree
Hide file tree
Showing 41 changed files with 154 additions and 60 deletions.
10 changes: 10 additions & 0 deletions ObjectPrinting/ObjectPrinter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ObjectPrinting
{
public class ObjectPrinter
{
public static PrintingConfig<T> For<T>()
{
return new PrintingConfig<T>();
}
}
}
9 changes: 3 additions & 6 deletions ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Solved\ObjectExtensions.cs" />
<Compile Include="Solved\ObjectPrinter.cs" />
<Compile Include="Solved\PrintingConfig.cs" />
<Compile Include="Solved\PropertyPrintingConfig.cs" />
<Compile Include="Solved\PropertyPrintingConfigExtensions.cs" />
<Compile Include="Tests\Person.cs" />
<Compile Include="ObjectPrinter.cs" />
<Compile Include="PrintingConfig.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\ObjectPrinterAcceptanceTests.cs" />
<Compile Include="Tests\Person.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
41 changes: 41 additions & 0 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Linq;
using System.Text;

namespace ObjectPrinting
{
public class PrintingConfig<TOwner>
{
public string PrintToString(TOwner obj)
{
return PrintToString(obj, 0);
}

private string PrintToString(object obj, int nestingLevel)
{
//TODO apply configurations
if (obj == null)
return "null" + Environment.NewLine;

var finalTypes = new[]
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
if (finalTypes.Contains(obj.GetType()))
return obj + Environment.NewLine;

var identation = new string('\t', nestingLevel + 1);
var sb = new StringBuilder();
var type = obj.GetType();
sb.AppendLine(type.Name);
foreach (var propertyInfo in type.GetProperties())
{
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}
return sb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion ObjectPrinting/Solved/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ObjectPrinting
namespace ObjectPrinting.Solved
{
public static class ObjectExtensions
{
Expand Down
6 changes: 1 addition & 5 deletions ObjectPrinting/Solved/ObjectPrinter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Linq;
using System.Text;

namespace ObjectPrinting
namespace ObjectPrinting.Solved
{
public class ObjectPrinter
{
Expand Down
24 changes: 16 additions & 8 deletions ObjectPrinting/Solved/PrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq.Expressions;
using System.Text;

namespace ObjectPrinting
namespace ObjectPrinting.Solved
{
public class PrintingConfig<TOwner>
{
Expand All @@ -12,7 +12,7 @@ public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>()
return new PropertyPrintingConfig<TOwner, TPropType>(this);
}

public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)
public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)
{
return new PropertyPrintingConfig<TOwner, TPropType>(this);
}
Expand All @@ -35,20 +35,28 @@ public string PrintToString(TOwner obj)
private string PrintToString(object obj, int nestingLevel)
{
//TODO apply configurations
var eoln = Environment.NewLine;
if (obj == null) return "null" + eoln;
if (new[] { typeof(int), typeof(double), typeof(float), typeof(string), typeof(DateTime), typeof(TimeSpan) }.Contains(obj.GetType()))
return obj + eoln;
if (obj == null)
return "null" + Environment.NewLine;

var finalTypes = new[]
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
if (finalTypes.Contains(obj.GetType()))
return obj + Environment.NewLine;

var identation = new string('\t', nestingLevel + 1);
var sb = new StringBuilder();
var type = obj.GetType();
sb.AppendLine(type.Name);
foreach (var propertyInfo in type.GetProperties())
{
sb.Append(identation + propertyInfo.Name + " = " + PrintToString(propertyInfo.GetValue(obj), nestingLevel + 1));
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}
return sb.ToString();
}

}
}
4 changes: 2 additions & 2 deletions ObjectPrinting/Solved/PropertyPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Globalization;

namespace ObjectPrinting
namespace ObjectPrinting.Solved
{
public class PropertyPrintingConfig<TOwner, TPropType> : IPropertyPrintingConfig<TOwner, TPropType>
public class PropertyPrintingConfig<TOwner, TPropType> : IPropertyPrintingConfig<TOwner, TPropType>
{
private readonly PrintingConfig<TOwner> printingConfig;

Expand Down
2 changes: 1 addition & 1 deletion ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ObjectPrinting
namespace ObjectPrinting.Solved
{
public static class PropertyPrintingConfigExtensions
{
Expand Down
40 changes: 40 additions & 0 deletions ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Globalization;
using NUnit.Framework;

namespace ObjectPrinting.Solved.Tests
{
[TestFixture]
public class ObjectPrinterAcceptanceTests
{
[Test]
public void Demo()
{
var person = new Person { Name = "Alex", Age = 19 };

var printer = ObjectPrinter.For<Person>()
//1. Исключить из сериализации свойства определенного типа
.Excluding<Guid>()
//2. Указать альтернативный способ сериализации для определенного типа
.Printing<int>().Using(i => i.ToString("X"))
//3. Для числовых типов указать культуру
.Printing<double>().Using(CultureInfo.InvariantCulture)
//4. Настроить сериализацию конкретного свойства
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
.Printing(p => p.Name).TrimmedToLength(10)
//6. Исключить из сериализации конкретного свойства
.Excluding(p => p.Age);

string s1 = printer.PrintToString(person);

//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
string s2 = person.PrintToString();

//8. ...с конфигурированием
string s3 = person.PrintToString(s => s.Excluding(p => p.Age));
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
}
}
}
12 changes: 12 additions & 0 deletions ObjectPrinting/Solved/Tests/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ObjectPrinting.Solved.Tests
{
public class Person
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Height { get; set; }
public int Age { get; set; }
}
}
31 changes: 7 additions & 24 deletions ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework;

namespace ObjectPrinting.Tests
{
Expand All @@ -14,31 +10,18 @@ public void Demo()
{
var person = new Person { Name = "Alex", Age = 19 };

var printer = ObjectPrinter.For<Person>()
var printer = ObjectPrinter.For<Person>();
//1. Исключить из сериализации свойства определенного типа
.Excluding<Guid>()
//2. Указать альтернативный способ сериализации для определенного типа
.Printing<int>().Using(i => i.ToString("X"))
//3. Для числовых типов указать культуру
.Printing<double>().Using(CultureInfo.InvariantCulture)
//4. Настроить сериализацию конкретного свойства
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
.Printing(p => p.Name).TrimmedToLength(10)
//5. Исключить из сериализации конкретного свойства
.Excluding(p => p.Age);

string s1 = printer.PrintToString(person);

//6. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
string s2 = person.PrintToString();

//7. ...с конфигурированием
string s3 = person.PrintToString(s => s.Excluding(p => p.Age));
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);

//6. Исключить из сериализации конкретного свойства

string s1 = printer.PrintToString(person);

//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
//8. ...с конфигурированием
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.0.5813.39031, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.0.1\lib\net45\nunit.framework.dll</HintPath>
<Reference Include="nunit.framework, Version=3.4.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\..\packages\NUnit.3.4.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -51,15 +51,15 @@
<Compile Include="Unit\SetterSpecTests.cs" />
<Compile Include="Unit\TypeMappingSpecTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FluentMapper\FluentMapping.csproj">
<Project>{feea5afe-459a-4d13-81d0-252e1a2e6f4e}</Project>
<Name>FluentMapping</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.0.1" targetFramework="net452" />
<package id="NUnit" version="3.4.1" targetFramework="net452" />
</packages>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 14 additions & 7 deletions fluent-api.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrinting", "ObjectPrinting\ObjectPrinting.csproj", "{07B8C9B7-8289-46CB-9875-048A57758EEE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{6D308E4A-CEC7-4536-9B87-81CD337A87AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping", "FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping", "Samples\FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "Samples\FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,10 +23,6 @@ Global
{07B8C9B7-8289-46CB-9875-048A57758EEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07B8C9B7-8289-46CB-9875-048A57758EEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07B8C9B7-8289-46CB-9875-048A57758EEE}.Release|Any CPU.Build.0 = Release|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU
{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -33,8 +31,17 @@ Global
{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}.Release|Any CPU.Build.0 = Release|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E} = {6D308E4A-CEC7-4536-9B87-81CD337A87AD}
{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D} = {6D308E4A-CEC7-4536-9B87-81CD337A87AD}
{EFA9335C-411B-4597-B0B6-5438D1AE04C3} = {6D308E4A-CEC7-4536-9B87-81CD337A87AD}
EndGlobalSection
EndGlobal

0 comments on commit 7e9b0e1

Please sign in to comment.