Skip to content

Commit

Permalink
fmt: use file-scoped namespaces everywhere (#519)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin <[email protected]>
  • Loading branch information
hf-kklein and Konstantin authored Sep 5, 2024
1 parent 0c9a4ad commit c26169a
Show file tree
Hide file tree
Showing 50 changed files with 5,818 additions and 5,868 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,132 +4,131 @@
using System.Linq;
using BO4E.COM;

namespace BO4E.Extensions.BusinessObjects.Benachrichtigung
namespace BO4E.Extensions.BusinessObjects.Benachrichtigung;

/// <summary>Additional methods for <see cref="Benachrichtigung" />.</summary>
public static class BenachrichtigungExtension
{
/// <summary>Additional methods for <see cref="Benachrichtigung" />.</summary>
public static class BenachrichtigungExtension
/// <summary>
/// checks if <see cref="BO4E.BO.Benachrichtigung.Infos" /> contains a key value pair
/// </summary>
/// <param name="b"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Has(this BO.Benachrichtigung b, string key, string value)
{
/// <summary>
/// checks if <see cref="BO4E.BO.Benachrichtigung.Infos" /> contains a key value pair
/// </summary>
/// <param name="b"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Has(this BO.Benachrichtigung b, string key, string value)
return Has(b, new GenericStringStringInfo
{
return Has(b, new GenericStringStringInfo
{
KeyColumn = key,
Value = value
});
}
KeyColumn = key,
Value = value
});
}

/// <summary>
/// checks if <paramref name="b" /> contains a key value pair specified in <paramref name="gssi" /> in
/// <see cref="BO4E.BO.Benachrichtigung.Infos" />
/// </summary>
/// <param name="b"></param>
/// <param name="gssi"></param>
/// <returns></returns>
public static bool Has(this BO.Benachrichtigung b, GenericStringStringInfo gssi)
/// <summary>
/// checks if <paramref name="b" /> contains a key value pair specified in <paramref name="gssi" /> in
/// <see cref="BO4E.BO.Benachrichtigung.Infos" />
/// </summary>
/// <param name="b"></param>
/// <param name="gssi"></param>
/// <returns></returns>
public static bool Has(this BO.Benachrichtigung b, GenericStringStringInfo gssi)
{
if (b.Infos == null || b.Infos.Count == 0)
{
if (b.Infos == null || b.Infos.Count == 0)
{
return false;
}

// ToDo für Hamid: Bitte prüfen, warum Contains false zurückliefert.
return b.Infos.Any(m => m.KeyColumn == gssi.KeyColumn && m.Value == gssi.Value);
return false;
}

/// <summary>
/// check if a specific info exists
/// </summary>
/// <param name="b">Benachrichtigung</param>
/// <param name="key">key to be checked</param>
/// <returns>true if key is in <see cref="BO4E.BO.Benachrichtigung.Infos" /></returns>
public static bool Has(this BO.Benachrichtigung b, string key)
{
if (b.Infos == null || b.Infos.Count == 0)
{
return false;
}
// ToDo für Hamid: Bitte prüfen, warum Contains false zurückliefert.
return b.Infos.Any(m => m.KeyColumn == gssi.KeyColumn && m.Value == gssi.Value);
}

return b.Infos.Any(gssi => gssi.KeyColumn == key);
/// <summary>
/// check if a specific info exists
/// </summary>
/// <param name="b">Benachrichtigung</param>
/// <param name="key">key to be checked</param>
/// <returns>true if key is in <see cref="BO4E.BO.Benachrichtigung.Infos" /></returns>
public static bool Has(this BO.Benachrichtigung b, string key)
{
if (b.Infos == null || b.Infos.Count == 0)
{
return false;
}

/// <summary>
/// checks if Benachrichtigung <paramref name="b" /> has an entry with key <paramref name="keyName" /> in
/// <see cref="BO4E.BO.Benachrichtigung.Infos" /> which fulfills a predicate
/// </summary>
/// <typeparam name="T">expected type of the info property</typeparam>
/// <param name="b">Benachrichtigung object</param>
/// <param name="keyName">key name of the info property</param>
/// <param name="predicate"></param>
/// <param name="passByDefault">defines default behaviour, e.g. if no such key is present</param>
/// <param name="typeConverter">allows to provide an explicit type converter</param>
/// <returns>
/// true if there's an info object with given key <paramref name="keyName" /> of type <typeparamref name="T" />
/// fulfilling <paramref name="predicate" /> or there's no such property but <paramref name="passByDefault" /> is true
/// </returns>
public static bool Has<T>(this BO.Benachrichtigung b, string keyName, Predicate<T> predicate,
bool passByDefault = true, TypeConverter typeConverter = null) where T : IComparable
return b.Infos.Any(gssi => gssi.KeyColumn == key);
}

/// <summary>
/// checks if Benachrichtigung <paramref name="b" /> has an entry with key <paramref name="keyName" /> in
/// <see cref="BO4E.BO.Benachrichtigung.Infos" /> which fulfills a predicate
/// </summary>
/// <typeparam name="T">expected type of the info property</typeparam>
/// <param name="b">Benachrichtigung object</param>
/// <param name="keyName">key name of the info property</param>
/// <param name="predicate"></param>
/// <param name="passByDefault">defines default behaviour, e.g. if no such key is present</param>
/// <param name="typeConverter">allows to provide an explicit type converter</param>
/// <returns>
/// true if there's an info object with given key <paramref name="keyName" /> of type <typeparamref name="T" />
/// fulfilling <paramref name="predicate" /> or there's no such property but <paramref name="passByDefault" /> is true
/// </returns>
public static bool Has<T>(this BO.Benachrichtigung b, string keyName, Predicate<T> predicate,
bool passByDefault = true, TypeConverter typeConverter = null) where T : IComparable
{
if (!b.Has(keyName))
{
if (!b.Has(keyName))
{
return passByDefault;
}
return passByDefault;
}

foreach (var info in b.Infos.Where(gssi => gssi.KeyColumn == keyName))
try
foreach (var info in b.Infos.Where(gssi => gssi.KeyColumn == keyName))
try
{
if (typeConverter == null)
{
if (typeConverter == null)
{
typeConverter = TypeDescriptor.GetConverter(typeof(T));
}

{
var value = (T)typeConverter.ConvertFromString(info.Value);
return predicate(value);
}
typeConverter = TypeDescriptor.GetConverter(typeof(T));
}
catch (NotSupportedException)

{
var value = (T)typeConverter.ConvertFromString(info.Value);
return predicate(value);
}
}
catch (NotSupportedException)
{
}

return false;
}
return false;
}

/// <summary>
/// moves key value pairs from <see cref="BO4E.BO.Benachrichtigung.Infos" /> to
/// <see cref="BO4E.BO.BusinessObject.UserProperties" /> for more convenient handling.
/// </summary>
/// <param name="b">Benachrichtigung</param>
/// <param name="overwriteExistingKeys">set true to overwrite userProperties with same key</param>
// ToDo: make method generic MoveInfosTouserProperties<boT>(...)
public static void MoveInfosToUserProperties(this BO.Benachrichtigung b, bool overwriteExistingKeys = false)
/// <summary>
/// moves key value pairs from <see cref="BO4E.BO.Benachrichtigung.Infos" /> to
/// <see cref="BO4E.BO.BusinessObject.UserProperties" /> for more convenient handling.
/// </summary>
/// <param name="b">Benachrichtigung</param>
/// <param name="overwriteExistingKeys">set true to overwrite userProperties with same key</param>
// ToDo: make method generic MoveInfosTouserProperties<boT>(...)
public static void MoveInfosToUserProperties(this BO.Benachrichtigung b, bool overwriteExistingKeys = false)
{
if (b.Infos != null && b.Infos.Count > 0)
{
if (b.Infos != null && b.Infos.Count > 0)
if (b.UserProperties == null)
{
if (b.UserProperties == null)
{
b.UserProperties = new Dictionary<string, object>();
}
b.UserProperties = new Dictionary<string, object>();
}

foreach (var info in b.Infos)
foreach (var info in b.Infos)
{
if (b.UserProperties.ContainsKey(info.KeyColumn) && overwriteExistingKeys)
{
if (b.UserProperties.ContainsKey(info.KeyColumn) && overwriteExistingKeys)
{
b.UserProperties.Remove(info.KeyColumn);
}

b.UserProperties.Add(info.KeyColumn,
info.Value); // might throw exception if key exists and !overwriteExistingKeys. That's ok.
b.UserProperties.Remove(info.KeyColumn);
}

b.Infos = null; // set to null after all elements have been moved
b.UserProperties.Add(info.KeyColumn,
info.Value); // might throw exception if key exists and !overwriteExistingKeys. That's ok.
}

b.Infos = null; // set to null after all elements have been moved
}
}
}
25 changes: 12 additions & 13 deletions BO4E.Extensions/BusinessObjects/BusinessObject.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using BO4E.BO;
using Newtonsoft.Json;

namespace BO4E.Extensions.BusinessObjects
namespace BO4E.Extensions.BusinessObjects;

/// <summary>
/// common extensions for all BusinessObjects
/// </summary>
public static class BusinessObjectExtensions
{
/// <summary>
/// common extensions for all BusinessObjects
/// Create a deep copy of a Business Object
/// </summary>
public static class BusinessObjectExtensions
/// <typeparam name="T">Type of the BusinessObject</typeparam>
/// <param name="source">the BO that is copied</param>
/// <returns>the deep copy</returns>
public static T DeepClone<T>(this T source) where T : BusinessObject
{
/// <summary>
/// Create a deep copy of a Business Object
/// </summary>
/// <typeparam name="T">Type of the BusinessObject</typeparam>
/// <param name="source">the BO that is copied</param>
/// <returns>the deep copy</returns>
public static T DeepClone<T>(this T source) where T : BusinessObject
{
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));
}
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));
}
}
Loading

0 comments on commit c26169a

Please sign in to comment.