Files
mixtape/zero.Core/Options/RavenOptions.cs
T

126 lines
3.0 KiB
C#
Raw Normal View History

2021-09-02 22:44:20 +02:00
using Raven.Client.Documents;
using Raven.Client.Documents.Indexes;
2021-08-27 11:46:50 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2021-09-01 23:09:22 +02:00
using System.Linq.Expressions;
2021-08-27 11:46:50 +02:00
using zero.Core.Database;
2021-09-02 22:44:20 +02:00
using zero.Core.Extensions;
2021-08-27 11:46:50 +02:00
namespace zero.Core.Options
{
public class RavenOptions
{
public string Url { get; set; }
public string Database { get; set; }
public string CollectionPrefix { get; set; }
public RavenIndexesOptions Indexes { get; set; } = new();
}
2021-09-02 22:44:20 +02:00
public class RavenIndexesOptions : ZeroBackofficeCollection<RavenIndexesOptions.Map>, IZeroCollectionOptions
2021-08-27 11:46:50 +02:00
{
2021-09-02 22:44:20 +02:00
public class Map
{
internal Type Type { get; set; }
internal Expression<Func<IZeroIndexDefinition>> CreateIndex { get; set; }
internal Map(Type type, Expression<Func<IZeroIndexDefinition>> create)
{
Type = type;
CreateIndex = create;
}
}
2021-09-01 23:09:22 +02:00
public RavenIndexModifiersOptions Modifiers { get; private set; } = new();
2021-08-27 11:46:50 +02:00
public void Add<T>() where T : IZeroIndexDefinition, new()
{
2021-09-02 22:44:20 +02:00
Items.Add(new(typeof(T), () => new T()));
2021-08-27 11:46:50 +02:00
}
public void Add(Type indexType)
{
2021-09-02 22:44:20 +02:00
Items.Add(new(indexType, () => (IZeroIndexDefinition)Activator.CreateInstance(indexType)));
}
public void Add<T>(T index) where T : IZeroIndexDefinition
{
Items.Add(new(typeof(T), () => index));
2021-08-27 11:46:50 +02:00
}
public void AddRange(params Type[] indexes)
{
2021-09-02 22:44:20 +02:00
foreach (Type type in indexes)
{
Add(type);
}
2021-08-27 11:46:50 +02:00
}
public void Replace<T, TReplaceWith>()
where T : IZeroIndexDefinition, new()
where TReplaceWith : IZeroIndexDefinition, new()
{
2021-09-02 22:44:20 +02:00
Replace(typeof(T), typeof(TReplaceWith));
2021-08-27 11:46:50 +02:00
}
public void Replace(Type origin, Type replaceWith)
{
2021-09-02 22:44:20 +02:00
var item = Items.FirstOrDefault(x => x.Type == origin);
if (item != null)
{
Items.Remove(item);
}
Add(replaceWith);
2021-08-27 11:46:50 +02:00
}
2021-09-02 22:44:20 +02:00
public IEnumerable<IZeroIndexDefinition> BuildAll(IZeroOptions options, IDocumentStore store)
2021-08-27 11:46:50 +02:00
{
2021-09-02 22:44:20 +02:00
foreach (Map map in Items)
2021-08-27 11:46:50 +02:00
{
2021-09-02 22:44:20 +02:00
IZeroIndexDefinition index = map.CreateIndex.Compile().Invoke();
index.Setup(options, store);
index.RunModifiers(options);
2021-09-01 23:09:22 +02:00
yield return index;
}
}
}
public class RavenIndexModifiersOptions : ZeroBackofficeCollection<RavenIndexModifiersOptions.Modifier>, IZeroCollectionOptions
{
public class Modifier
{
public Type Type { get; set; }
public Expression<Action<IZeroIndexDefinition>> Modify { get; set; }
}
public void Add<T>(Action<T> modify) where T : IZeroIndexDefinition, new()
{
Items.Add(new()
{
Type = typeof(T),
Modify = x => modify((T)x)
});
}
public IEnumerable<Modifier> GetAllForType<T>() where T : IZeroIndexDefinition, new() => GetAllForType(typeof(T));
public IEnumerable<Modifier> GetAllForType(Type type)
{
foreach (Modifier modifier in Items.Where(x => x.Type.IsAssignableFrom(type)))
{
yield return modifier;
2021-08-27 11:46:50 +02:00
}
}
}
}