Skip to content

Commit

Permalink
#142 - Rewrite PresentationModels for reflection (2).
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Feb 16, 2019
1 parent fc76d83 commit e5836d8
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 151 deletions.
9 changes: 8 additions & 1 deletion Neptuo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neptuo.PresentationModels.T
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neptuo.Windows.Converters", "src\Neptuo.Windows.Converters\Neptuo.Windows.Converters.csproj", "{F9377571-8686-4079-8074-DBF0C73B03BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTest.PresentationModels", "test\UnitTest.PresentationModels\UnitTest.PresentationModels.csproj", "{E6A1053C-6D3A-415D-B599-7B8040C44C9D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -379,6 +381,10 @@ Global
{F9377571-8686-4079-8074-DBF0C73B03BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9377571-8686-4079-8074-DBF0C73B03BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9377571-8686-4079-8074-DBF0C73B03BC}.Release|Any CPU.Build.0 = Release|Any CPU
{E6A1053C-6D3A-415D-B599-7B8040C44C9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6A1053C-6D3A-415D-B599-7B8040C44C9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6A1053C-6D3A-415D-B599-7B8040C44C9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6A1053C-6D3A-415D-B599-7B8040C44C9D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -441,9 +447,10 @@ Global
{07EB063B-F3D5-41BB-BEF0-788F3C7773E7} = {A7CE8ABF-9770-49F4-8969-8AB6957A87E6}
{F43B4223-E136-4AE2-9A57-8220F2452F54} = {5D0CB141-A610-4243-BBDA-7D5A9084FC53}
{F9377571-8686-4079-8074-DBF0C73B03BC} = {A7CE8ABF-9770-49F4-8969-8AB6957A87E6}
{E6A1053C-6D3A-415D-B599-7B8040C44C9D} = {5D0CB141-A610-4243-BBDA-7D5A9084FC53}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.2\lib\NET35
SolutionGuid = {C551EE44-C191-455D-BEFA-6366FEC51A7D}
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.2\lib\NET35
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Neptuo.PresentationModels.Validators
/// <summary>
/// Describes key for selecting <see cref="IFieldMetadataValidator"/>.
/// </summary>
public class FieldMetadataValidatorKey
public class FieldMetadataValidatorKey : IEquatable<FieldMetadataValidatorKey>
{
/// <summary>
/// Identifier of model definition.
Expand Down Expand Up @@ -41,35 +41,21 @@ public FieldMetadataValidatorKey(string modelIdentifier, string fieldIdentifier,

public override int GetHashCode()
{
int value = 13;
if (ModelIdentifier != null)
value ^= ModelIdentifier.GetHashCode();

if (FieldIdentifier != null)
value ^= FieldIdentifier.GetHashCode();

if(MetadataKey != null)
value ^= MetadataKey.GetHashCode();

return value;
var hashCode = -469800716;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ModelIdentifier);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FieldIdentifier);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(MetadataKey);
return hashCode;
}

public override bool Equals(object obj)
{
FieldMetadataValidatorKey other = obj as FieldMetadataValidatorKey;
if (other == null)
return false;

if (ModelIdentifier != other.ModelIdentifier)
return false;

if (FieldIdentifier != other.FieldIdentifier)
return false;
public override bool Equals(object obj) => Equals(obj as FieldMetadataValidatorKey);

if (MetadataKey != other.MetadataKey)
return false;

return true;
public bool Equals(FieldMetadataValidatorKey other)
{
return other != null &&
ModelIdentifier == other.ModelIdentifier &&
FieldIdentifier == other.FieldIdentifier &&
MetadataKey == other.MetadataKey;
}
}
}

This file was deleted.

87 changes: 0 additions & 87 deletions test/TestConsole/PresentationModels/TestPresentationModels.cs

This file was deleted.

16 changes: 0 additions & 16 deletions test/TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,12 @@ class Program
{
private static void Main(string[] args)
{
//TestPresentationModels.Test();
//TestXmlModelDefinition.Test();
//TestFieldMetadataValidatorKey.Test();
//TestConfiguration.Test();
//TestCommands.Test();
//TestEvents.Test();
//TestDelegates.Test();
//TestCompiler.Test();
//TestHash.Test();
//TestBootstrap.Test();
//TestKeyValue.Test();
//TestMultiLockProvider.Test();
//TestDependency.Test();
//TestAppServices.Test();
//TestBehaviors.Test();
//TestObjectSize.Test();
//TestSharpKitCompiler.Test();
//TestLog4net.Test();
//TestLocalization.Test();
//Services.Queries.TestQueries.Test();
//TestDispatcherException.Test();
//TestEventManager.Test();

Console.ReadKey(true);
}
Expand Down
29 changes: 29 additions & 0 deletions test/UnitTest.PresentationModels/Models/RegisterUserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Neptuo.PresentationModels.Models
{
class RegisterUserModel
{
[Required]
public string Username { get; set; }

[Required]
public string Password { get; set; }

[Required]
[Compare("Password")]
public string PasswordAgain { get; set; }

[Required]
public int? Age { get; set; }

public ICollection<int> RoleIDs { get; }

public RegisterUserModel() => RoleIDs = new List<int>();
}
}
82 changes: 82 additions & 0 deletions test/UnitTest.PresentationModels/TestReflection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neptuo.PresentationModels.Models;
using Neptuo.PresentationModels.TypeModels;
using Neptuo.PresentationModels.TypeModels.DataAnnotations;
using Neptuo.PresentationModels.TypeModels.DataAnnotations.Validators;
using Neptuo.PresentationModels.TypeModels.ValueUpdates;
using Neptuo.PresentationModels.Validation;
using Neptuo.PresentationModels.Validators;
using Neptuo.PresentationModels.Validators.Handlers;
using Neptuo.Validators;
using Neptuo.Validators.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Neptuo.PresentationModels
{
[TestClass]
public class TestReflection
{
private AttributeMetadataReaderCollection metadataReaders;
private FieldMetadataValidatorCollection fieldMetadataValidators;
private ReflectionValueUpdaterCollection valueUpdaters;
private TypeModelDefinitionCollection modelDefinitions;

public TestReflection()
{
metadataReaders = new AttributeMetadataReaderCollection()
.Add(new CompareMetadataReader())
.Add(new DataTypeMetadataReader())
.Add(new DefaultValueMetadataReader())
.Add(new DescriptionMetadataReader())
.Add(new DisplayMetadataReader())
.Add(new RequiredMetadataReader())
.Add(new StringLengthMetadataReader());

fieldMetadataValidators = new FieldMetadataValidatorCollection()
.Add(null, null, "Required", new RequiredMetadataValidator())
.Add(null, null, "MatchProperty", new MatchPropertyMetadataValidator());

valueUpdaters = new ReflectionValueUpdaterCollection()
.Add<ICollection<int>>(new CollectionItemReflectionValueUpdater<int>());

modelDefinitions = new TypeModelDefinitionCollection()
.AddReflectionSearchHandler(metadataReaders);
}

[TestMethod]
public void BuildModelDefinition()
{
IModelDefinition modelDefinition = modelDefinitions.Get<RegisterUserModel>();
Assert.AreEqual(5, modelDefinition.Fields.Count());
}

[TestMethod]
public void GetValues()
{
IModelDefinition modelDefinition = modelDefinitions.Get<RegisterUserModel>();

RegisterUserModel model = new RegisterUserModel();
model.Username = "pepa";
model.Password = "x";
model.PasswordAgain = "y";
IModelValueProvider reflection = new ReflectionModelValueProvider(model, valueUpdaters);
DictionaryModelValueProvider dictionary = new DictionaryModelValueProvider();

CopyModelValueProvider copyProvider = new CopyModelValueProvider(modelDefinition, true);
copyProvider.Update(dictionary, reflection);

object value;
Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.Username), out value));
Assert.AreEqual(model.Username, value);

Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.Password), out value));
Assert.AreEqual(model.Password, value);

Assert.IsTrue(dictionary.TryGetValue(nameof(RegisterUserModel.PasswordAgain), out value));
Assert.AreEqual(model.PasswordAgain, value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

<IsPackable>false</IsPackable>

<RootNamespace>Neptuo.PresentationModels</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.Serialization.Xml\Neptuo.PresentationModels.Serialization.Xml.csproj" />
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.TypeModels.DataAnnotations\Neptuo.PresentationModels.TypeModels.DataAnnotations.csproj" />
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.TypeModels\Neptuo.PresentationModels.TypeModels.csproj" />
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.UI\Neptuo.PresentationModels.UI.csproj" />
<ProjectReference Include="..\..\src\Neptuo.PresentationModels.Validators\Neptuo.PresentationModels.Validators.csproj" />
<ProjectReference Include="..\..\src\Neptuo.PresentationModels\Neptuo.PresentationModels.csproj" />
<ProjectReference Include="..\..\src\Neptuo.Validators\Neptuo.Validators.csproj" />
<ProjectReference Include="..\..\src\Neptuo\Neptuo.csproj" />
</ItemGroup>

</Project>

0 comments on commit e5836d8

Please sign in to comment.