adjusted namespaces and made separate data dir
This commit is contained in:
@@ -2,31 +2,30 @@
|
||||
using Lookup.Interface;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Lookup.Database
|
||||
namespace Lookup.Database;
|
||||
|
||||
/// <summary>
|
||||
/// Basic <see cref="DbContext"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
public class LookupDbContext<TKey, TValue> : DbContext
|
||||
where TKey : class, IKeyEntity<TKey, TValue>
|
||||
where TValue : class, IRelated<TKey>, IValueEntity<TKey, TValue>
|
||||
{
|
||||
/// <summary>
|
||||
/// Basic <see cref="DbContext"/>
|
||||
/// Stored <see cref="IKeyEntity{TKey,TValue}"/> instances
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
public class LookupDbContext<TKey, TValue> : DbContext
|
||||
where TKey : class, IKeyEntity<TKey, TValue>
|
||||
where TValue : class, IRelated<TKey>, IValueEntity<TKey, TValue>
|
||||
public virtual DbSet<IKeyEntity<TKey, TValue>> Keys { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Stored <see cref="IValueEntity{TKey,TValue}"/> instances
|
||||
/// </summary>
|
||||
public virtual DbSet<IValueEntity<TKey, TValue>> Values { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
/// <summary>
|
||||
/// Stored <see cref="IKeyEntity{TKey,TValue}"/> instances
|
||||
/// </summary>
|
||||
public virtual DbSet<IKeyEntity<TKey, TValue>> Keys { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Stored <see cref="IValueEntity{TKey,TValue}"/> instances
|
||||
/// </summary>
|
||||
public virtual DbSet<IValueEntity<TKey, TValue>> Values { get; set; } = null!;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfiguration(new LookupConfiguration<TKey, TValue>());
|
||||
}
|
||||
modelBuilder.ApplyConfiguration(new LookupConfiguration<TKey, TValue>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,39 @@
|
||||
using Lookup.Memory;
|
||||
|
||||
namespace Lookup.IO
|
||||
namespace Lookup.IO;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Interface.ILookup{TKey,TValue}"/>
|
||||
/// implementation, which stores data in <see cref="ICollection{T}"/>s.
|
||||
/// Supports writing the stored data to a file.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public class FileLookup<TKey, TValue> : MemoryLookup<TKey, TValue>
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="Interface.ILookup{TKey,TValue}"/>
|
||||
/// implementation, which stores data in <see cref="ICollection{T}"/>s.
|
||||
/// Supports writing the stored data to a file.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public class FileLookup<TKey, TValue> : MemoryLookup<TKey, TValue>
|
||||
public string Path { get; set; }
|
||||
|
||||
public FileLookup(string path)
|
||||
{
|
||||
public string Path { get; set; }
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public FileLookup(string path)
|
||||
public void Save()
|
||||
{
|
||||
using var writer = new StreamWriter(System.IO.File.Create(Path));
|
||||
foreach (var kv in this)
|
||||
{
|
||||
Path = path;
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
using var writer = new StreamWriter(System.IO.File.Create(Path));
|
||||
foreach (var kv in this)
|
||||
{
|
||||
writer.WriteLine($"{kv.Key};{string.Join(',', kv.Value)}");
|
||||
}
|
||||
}
|
||||
|
||||
public new void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
|
||||
if (System.IO.File.Exists(Path))
|
||||
{
|
||||
System.IO.File.Delete(Path);
|
||||
}
|
||||
writer.WriteLine($"{kv.Key};{string.Join(',', kv.Value)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public new void Clear()
|
||||
{
|
||||
base.Clear();
|
||||
|
||||
if (System.IO.File.Exists(Path))
|
||||
{
|
||||
System.IO.File.Delete(Path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,72 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Lookup.Interface
|
||||
namespace Lookup.Interface;
|
||||
|
||||
/// <summary>
|
||||
/// Common interface for <see cref="ILookup"/>s,
|
||||
/// storing data as key-value pairs
|
||||
/// </summary>
|
||||
public interface ILookup
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Common interface for <see cref="ILookup{TKey, TValue}"/>s,
|
||||
/// storing data as <typeparamref name="TKey"/>-<typeparamref name="TValue"/> pairs
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public interface ILookup<TKey, TValue>
|
||||
: ILookup,
|
||||
IDictionary<TKey, ICollection<TValue>>,
|
||||
IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Common interface for <see cref="ILookup"/>s,
|
||||
/// storing data as key-value pairs
|
||||
/// Adds a key to the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public interface ILookup
|
||||
{
|
||||
}
|
||||
/// <param name="key">
|
||||
/// The <typeparamref name="TKey"/> to add
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The new <see cref="ICollection{TValue}"/>
|
||||
/// to store <typeparamref name="TValue"/>s in
|
||||
/// </returns>
|
||||
ICollection<TValue> Add(TKey key);
|
||||
|
||||
/// <summary>
|
||||
/// Common interface for <see cref="ILookup{TKey, TValue}"/>s,
|
||||
/// storing data as <typeparamref name="TKey"/>-<typeparamref name="TValue"/> pairs
|
||||
/// Adds a <typeparamref name="TValue"/> to an existing collection
|
||||
/// referenced by <typeparamref name="TKey"/> in the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public interface ILookup<TKey, TValue>
|
||||
: ILookup,
|
||||
IDictionary<TKey, ICollection<TValue>>,
|
||||
IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds a key to the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
/// <param name="key">
|
||||
/// The <typeparamref name="TKey"/> to add
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The new <see cref="ICollection{TValue}"/>
|
||||
/// to store <typeparamref name="TValue"/>s in
|
||||
/// </returns>
|
||||
ICollection<TValue> Add(TKey key);
|
||||
public void Add(TKey key, TValue value);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <typeparamref name="TValue"/> to an existing collection
|
||||
/// referenced by <typeparamref name="TKey"/> in the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public void Add(TKey key, TValue value);
|
||||
/// <summary>
|
||||
/// Adds multiple <typeparamref name="TValue"/>s to
|
||||
/// an existing <see cref="ICollection{TValue}"/>
|
||||
/// referenced by <typeparamref name="TKey"/> in
|
||||
/// the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public void AddRange(TKey key, IEnumerable<TValue> values);
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple <typeparamref name="TValue"/>s to
|
||||
/// an existing <see cref="ICollection{TValue}"/>
|
||||
/// referenced by <typeparamref name="TKey"/> in
|
||||
/// the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public void AddRange(TKey key, IEnumerable<TValue> values);
|
||||
/// <summary>
|
||||
/// Removes a <typeparamref name="TValue"/> from
|
||||
/// an existing <see cref="ICollection{TValue}"/>
|
||||
/// referenced by <typeparamref name="TKey"/> in
|
||||
/// the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public bool Remove(TKey key, TValue value);
|
||||
|
||||
/// <summary>
|
||||
/// Removes a <typeparamref name="TValue"/> from
|
||||
/// an existing <see cref="ICollection{TValue}"/>
|
||||
/// referenced by <typeparamref name="TKey"/> in
|
||||
/// the <see cref="ILookup"/>
|
||||
/// </summary>
|
||||
public bool Remove(TKey key, TValue value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets an existing <see cref="ICollection{TValue}"/> referenced by
|
||||
/// <typeparamref name="TKey"/> or creates it, if it does not already exist
|
||||
/// </summary>
|
||||
/// <param name="key">
|
||||
/// The <typeparamref name="TKey"/> to add
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ICollection{TValue}"/> to
|
||||
/// store <typeparamref name="TValue"/>s in
|
||||
/// </returns>
|
||||
public ICollection<TValue> GetOrAdd(TKey key);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets an existing <see cref="ICollection{TValue}"/> referenced by
|
||||
/// <typeparamref name="TKey"/> or creates it, if it does not already exist
|
||||
/// </summary>
|
||||
/// <param name="key">
|
||||
/// The <typeparamref name="TKey"/> to add
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The <see cref="ICollection{TValue}"/> to
|
||||
/// store <typeparamref name="TValue"/>s in
|
||||
/// </returns>
|
||||
public ICollection<TValue> GetOrAdd(TKey key);
|
||||
}
|
||||
@@ -2,89 +2,88 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace Lookup.Memory
|
||||
namespace Lookup.Memory;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ILookup{TKey,TValue}"/>
|
||||
/// implementation, storing data in <see cref="ICollection{T}"/>s
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public class MemoryLookup<TKey, TValue> : Abstract.Lookup<TKey, TValue>
|
||||
where TKey : notnull
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ILookup{TKey,TValue}"/>
|
||||
/// implementation, storing data in <see cref="ICollection{T}"/>s
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">Type of the key referencing the <typeparamref name="TValue"/>s</typeparam>
|
||||
/// <typeparam name="TValue">Type of the stored values, referenced by <typeparamref name="TKey"/></typeparam>
|
||||
public class MemoryLookup<TKey, TValue> : Abstract.Lookup<TKey, TValue>
|
||||
where TKey : notnull
|
||||
#region Constructors
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup()
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup()
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(IDictionary<TKey, ICollection<TValue>> dictionary) : base(dictionary)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IDictionary<TKey, ICollection<TValue>> dictionary, IEqualityComparer<TKey>? comparer
|
||||
) : base(dictionary, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IEnumerable<KeyValuePair<TKey, ICollection<TValue>>> collection
|
||||
) : base(collection)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IEnumerable<KeyValuePair<TKey, ICollection<TValue>>> collection,
|
||||
IEqualityComparer<TKey>? comparer
|
||||
) : base(collection, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(IEqualityComparer<TKey>? comparer) : base(comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(int capacity) : base(capacity)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(int capacity, IEqualityComparer<TKey>? comparer) : base(capacity, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides of Lookup<TKey,ICollection<TValue>>
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ICollection<TValue> Add(TKey key)
|
||||
{
|
||||
base.Add(key, new List<TValue>());
|
||||
return this[key];
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Add(TKey)" />
|
||||
public ICollection<TValue> Add(TKey key, int collectionCapacity)
|
||||
{
|
||||
base.Add(key, new List<TValue>(collectionCapacity));
|
||||
return this[key];
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(IDictionary<TKey, ICollection<TValue>> dictionary) : base(dictionary)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IDictionary<TKey, ICollection<TValue>> dictionary, IEqualityComparer<TKey>? comparer
|
||||
) : base(dictionary, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IEnumerable<KeyValuePair<TKey, ICollection<TValue>>> collection
|
||||
) : base(collection)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(
|
||||
IEnumerable<KeyValuePair<TKey, ICollection<TValue>>> collection,
|
||||
IEqualityComparer<TKey>? comparer
|
||||
) : base(collection, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(IEqualityComparer<TKey>? comparer) : base(comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(int capacity) : base(capacity)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(int capacity, IEqualityComparer<TKey>? comparer) : base(capacity, comparer)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public MemoryLookup(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Overrides of Lookup<TKey,ICollection<TValue>>
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ICollection<TValue> Add(TKey key)
|
||||
{
|
||||
base.Add(key, new List<TValue>());
|
||||
return this[key];
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Add(TKey)" />
|
||||
public ICollection<TValue> Add(TKey key, int collectionCapacity)
|
||||
{
|
||||
base.Add(key, new List<TValue>(collectionCapacity));
|
||||
return this[key];
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user