Skip to content

Commit

Permalink
add 'ModelStateList' type
Browse files Browse the repository at this point in the history
  • Loading branch information
hatami57 committed May 18, 2019
1 parent b568476 commit d26ce97
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 84 deletions.
1 change: 1 addition & 0 deletions DDDCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Company>Meteor</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<PackageVersion>1.0.2</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
30 changes: 0 additions & 30 deletions Domain/Types/ModelState.cs

This file was deleted.

71 changes: 22 additions & 49 deletions Domain/Types/ValueObject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DDDCommon.Domain.Types;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand All @@ -9,20 +8,12 @@ namespace DDDCommon.Domain.Types
// source: https://github.com/jhewlett/ValueObject
public abstract class ValueObject : IEquatable<ValueObject>
{
private List<PropertyInfo> properties;
private List<FieldInfo> fields;
private List<PropertyInfo> _properties;
private List<FieldInfo> _fields;

public static bool operator ==(ValueObject obj1, ValueObject obj2)
{
if (Equals(obj1, null))
{
if (Equals(obj2, null))
{
return true;
}
return false;
}
return obj1.Equals(obj2);
return obj1?.Equals(obj2) ?? Equals(obj2, null);
}

public static bool operator !=(ValueObject obj1, ValueObject obj2)
Expand Down Expand Up @@ -55,9 +46,9 @@ private bool FieldsAreEqual(object obj, FieldInfo f)

private IEnumerable<PropertyInfo> GetProperties()
{
if (properties == null)
if (_properties == null)
{
properties = GetType()
_properties = GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetCustomAttribute(typeof(IgnoreMemberAttribute)) == null)
.ToList();
Expand All @@ -66,49 +57,31 @@ private IEnumerable<PropertyInfo> GetProperties()
// !Attribute.IsDefined(p, typeof(IgnoreMemberAttribute))).ToList();
}

return properties;
return _properties;
}

private IEnumerable<FieldInfo> GetFields()
{
if (fields == null)
{
fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetCustomAttribute(typeof(IgnoreMemberAttribute)) == null)
.ToList();
}

return fields;
}
private IEnumerable<FieldInfo> GetFields() =>
_fields ?? (_fields = GetType().GetFields(BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.GetCustomAttribute(typeof(IgnoreMemberAttribute)) == null)
.ToList());

public override int GetHashCode()
{
unchecked // allow overflow
{
int hash = 17;
foreach (var prop in GetProperties())
{
var value = prop.GetValue(this, null);
hash = HashValue(hash, value);
}

foreach (var field in GetFields())
{
var value = field.GetValue(this);
hash = HashValue(hash, value);
}

return hash;
}
var hash = GetProperties().Select(prop => prop.GetValue(this, null))
.Aggregate(17, HashValue);

return GetFields().Select(field => field.GetValue(this))
.Aggregate(hash, HashValue);
}

private int HashValue(int seed, object value)
private static int HashValue(int seed, object value)
{
var currentHash = value != null
? value.GetHashCode()
: 0;
var currentHash = value?.GetHashCode() ?? 0;

return seed * 23 + currentHash;
unchecked
{
return seed * 23 + currentHash;
}
}
}
}
10 changes: 5 additions & 5 deletions Infrastructure/Types/PostgreSqlDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Npgsql;
using Npgsql.NameTranslation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -176,12 +174,14 @@ private void ConvertToSnake(INpgsqlNameTranslator mapper, object entity)
}

private string ConvertKeyToSnake(INpgsqlNameTranslator mapper, string keyName) =>
ConvertGeneralToSnake(mapper, KeysRegex.Replace(keyName, match => match.Value.ToLower()));
ConvertGeneralToSnake(mapper, KeysRegex.Replace(keyName,
match => match.Value.ToLower()));

private string ConvertGeneralToSnake(INpgsqlNameTranslator mapper, string entityName) =>
mapper.TranslateMemberName(ModifyNameBeforeConvertion(mapper, entityName));
mapper.TranslateMemberName(ModifyNameBeforeConversion(mapper, entityName));

protected virtual string ModifyNameBeforeConvertion(INpgsqlNameTranslator mapper, string entityName) => entityName;
protected virtual string ModifyNameBeforeConversion(INpgsqlNameTranslator mapper, string entityName) =>
entityName;

}
}
15 changes: 15 additions & 0 deletions Utils/IReadOnlyModelStateList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace DDDCommon.Utils
{
public interface IReadOnlyModelStateList<T>
{
IReadOnlyList<T> Items { get; }

IReadOnlyList<T> AddedItems { get; }

IReadOnlyList<(T Old, T New)> UpdatedItems { get; }

IReadOnlyList<T> RemovedItems { get; }
}
}

0 comments on commit d26ce97

Please sign in to comment.