improvemets, fixes, etc...
This commit is contained in:
parent
68b20be91e
commit
cfd8af537f
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AyCode.Core.Enums
|
||||
{
|
||||
public enum DataChangeMode
|
||||
{
|
||||
Add = 1,
|
||||
Update = 2,
|
||||
Remove = 3,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
using AyCode.Core.Enums;
|
||||
using AyCode.Core.Interfaces;
|
||||
using AyCode.Utils.Extensions;
|
||||
|
||||
namespace AyCode.Core.Extensions
|
||||
{
|
||||
public static class CollectionExtensions
|
||||
{
|
||||
public static DataChangeMode UpdateCollection<TDataItem>(this IList<TDataItem> source, TDataItem dataItem, bool isRemove) where TDataItem : IId<Guid>
|
||||
{
|
||||
if (dataItem.Id.IsNullOrEmpty()) throw new ArgumentNullException(nameof(dataItem), "UpdateCollection->dataItem.Id.IsNullOrEmpty()");
|
||||
|
||||
var transferIndex = source.FindIndex(x => x.Id == dataItem.Id);
|
||||
|
||||
if (isRemove)
|
||||
{
|
||||
if (transferIndex > -1) source.RemoveAt(transferIndex);
|
||||
|
||||
return DataChangeMode.Remove;
|
||||
}
|
||||
|
||||
if (transferIndex > -1)
|
||||
{
|
||||
source[transferIndex] = dataItem;
|
||||
return DataChangeMode.Update;
|
||||
}
|
||||
|
||||
source.Add(dataItem);
|
||||
return DataChangeMode.Add;
|
||||
}
|
||||
|
||||
public static int FindIndex<T>(this IList<T> list, Predicate<T> predicate)
|
||||
{
|
||||
for (var index = 0; index < list.Count; ++index)
|
||||
{
|
||||
if (predicate(list[index]))
|
||||
return index;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static bool IsValidIndex<T>(this IList<T>? array, int index)
|
||||
{
|
||||
return array != null && index >= 0 && index < array.Count;
|
||||
}
|
||||
|
||||
public static bool TryGetValue<T>(this IList<T> array, int index, out T? value)
|
||||
{
|
||||
if (array.IsValidIndex<T>(index))
|
||||
{
|
||||
value = array[index];
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default(T);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int BinarySearch<TValue, TKey>(this IList<TValue> list, TKey key, Func<TValue, TKey> getKey) where TKey : IComparable
|
||||
{
|
||||
var index1 = 0;
|
||||
var index2 = list.Count - 1;
|
||||
|
||||
while (index2 >= index1)
|
||||
{
|
||||
var obj1 = list[index1];
|
||||
if (getKey(obj1).CompareTo((object)key) == 0) return index1;
|
||||
|
||||
var obj2 = list[index2];
|
||||
if (getKey(obj2).CompareTo((object)key) == 0) return index2;
|
||||
|
||||
var index3 = (index1 + index2) / 2;
|
||||
var obj3 = list[index3];
|
||||
var num = getKey(obj3).CompareTo((object)key);
|
||||
|
||||
if (num == 0) return index3;
|
||||
|
||||
if (num < 0)
|
||||
{
|
||||
index1 = index3 + 1;
|
||||
--index2;
|
||||
}
|
||||
else
|
||||
{
|
||||
index2 = index3 - 1;
|
||||
++index1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AyCode.Core.Interfaces
|
||||
{
|
||||
public interface IId<T>
|
||||
{
|
||||
T Id { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
namespace AyCode.Interfaces.Addresses;
|
||||
using AyCode.Interfaces.Entities;
|
||||
|
||||
public interface IAcAddressForeignKey
|
||||
namespace AyCode.Interfaces.Addresses;
|
||||
|
||||
public interface IAcAddressForeignKey : IEntityGuid
|
||||
{
|
||||
public Guid AddressId { get; set; }
|
||||
}
|
||||
|
|
@ -9,9 +9,9 @@ namespace AyCode.Interfaces.Entities
|
|||
{
|
||||
}
|
||||
|
||||
public interface IEntity<TPKey> : IEntity
|
||||
public interface IEntity<TPKey> : IEntity, IId<TPKey>
|
||||
{
|
||||
[Key] TPKey Id { get; set; }
|
||||
//[Key] TPKey Id { get; set; }
|
||||
//T SetId(T id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
namespace AyCode.Interfaces.Users;
|
||||
|
||||
public interface IAcUsersRelation<TUser, TUserToServiceProvider>
|
||||
where TUser : IAcUserBase
|
||||
where TUserToServiceProvider : IAcUserToCompanyBase
|
||||
public interface IAcUsersRelation<TUser, TUserToServiceProvider> where TUser : IAcUserBase where TUserToServiceProvider : IAcUserToCompanyBase
|
||||
{
|
||||
public List<TUser> Users { get; set; }
|
||||
public List<TUserToServiceProvider> UserToServiceProviders { get; set; }
|
||||
|
|
|
|||
Loading…
Reference in New Issue