Skip to content

Commit

Permalink
Updated packages, merged core code of the analytics sample into the c…
Browse files Browse the repository at this point in the history
…ore project
  • Loading branch information
oskardudycz committed Feb 17, 2023
1 parent d775bac commit d8674d2
Show file tree
Hide file tree
Showing 138 changed files with 639 additions and 1,393 deletions.
3 changes: 1 addition & 2 deletions Core.Build.props
Expand Up @@ -6,7 +6,6 @@
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.4.27" PrivateAssets="All"/>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0" PrivateAssets="All" Condition=" '$(TargetFrawework)' == 'netstandard2.0' "/>
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.5.22" PrivateAssets="All"/>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Core.ElasticSearch/Core.ElasticSearch.csproj
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="NEST" Version="7.17.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
Expand Down
@@ -1,25 +1,21 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Nest;

namespace DataAnalytics.Core.ElasticSearch
{
namespace Core.ElasticSearch.Repository;


public static class ElasticSearchRepository
{

public static async Task<T?> Find<T>(this IElasticClient elasticClient, string id, CancellationToken ct)
where T: class =>
(await elasticClient.GetAsync<T>(id, ct: ct))?.Source;
(await elasticClient.GetAsync<T>(id, ct: ct).ConfigureAwait(false))?.Source;

public static async Task Upsert<T>(this IElasticClient elasticClient, string id, T entity, CancellationToken ct)
where T: class =>
await elasticClient.UpdateAsync<T>(id,
u => u.Doc(entity).Upsert(entity).Index(ToIndexName<T>()),
ct
);
).ConfigureAwait(false);

private static readonly ConcurrentDictionary<Type, string> TypeNameMap = new();

Expand All @@ -33,5 +29,3 @@ private static string ToIndexName<TIndex>()
});
}
}
}

12 changes: 6 additions & 6 deletions Core.EventStoreDB.Tests/Core.EventStoreDB.Tests.csproj
Expand Up @@ -11,20 +11,20 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core.EventStoreDB\Core.EventStoreDB.csproj"/>
<ProjectReference Include="..\Core.Testing\Core.Testing.csproj"/>
<ProjectReference Include="..\Core.EventStoreDB\Core.EventStoreDB.csproj" />
<ProjectReference Include="..\Core.Testing\Core.Testing.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<Import Project="..\Tests.Build.props"/>
<Import Project="..\Tests.Build.props" />

</Project>
4 changes: 2 additions & 2 deletions Core.EventStoreDB/Core.EventStoreDB.csproj
Expand Up @@ -8,9 +8,9 @@
<ItemGroup>
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="22.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

Expand Down
98 changes: 98 additions & 0 deletions Core.EventStoreDB/Events/EventStoreDBExtensions.cs
@@ -1,12 +1,39 @@
using Core.EventStoreDB.Serialization;
using Core.Exceptions;
using Core.OpenTelemetry;
using Core.Reflection;
using EventStore.Client;

namespace Core.EventStoreDB.Events;

public static class EventStoreDBExtensions
{
public static async Task<TEntity?> Find<TEntity>(
this EventStoreClient eventStore,
Func<TEntity, object, TEntity> when,
string id,
CancellationToken cancellationToken
) where TEntity: class
{
var readResult = eventStore.ReadStreamAsync(
Direction.Forwards,
id,
StreamPosition.Start,
cancellationToken: cancellationToken
);

if (await readResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
return null;

return await readResult
.Select(@event => @event.Deserialize()!)
.AggregateAsync(
ObjectFactory<TEntity>.GetDefaultOrUninitialized(),
when,
cancellationToken
).ConfigureAwait(false);
}

public static async Task<TEntity> Find<TEntity>(
this EventStoreClient eventStore,
Func<TEntity> getDefault,
Expand Down Expand Up @@ -88,4 +115,75 @@ CancellationToken cancellationToken

return result.NextExpectedStreamRevision;
}

public static async Task<TEvent?> ReadLastEvent<TEvent>(
this EventStoreClient eventStore,
string id,
CancellationToken ct
) where TEvent : class
{
var resolvedEvent = await eventStore.ReadLastEvent(id, ct).ConfigureAwait(false);

return resolvedEvent?.Deserialize<TEvent>();
}

public static async Task<ResolvedEvent?> ReadLastEvent(
this EventStoreClient eventStore,
string id,
CancellationToken ct
)
{
var result = eventStore.ReadStreamAsync(
Direction.Backwards,
id,
StreamPosition.End,
maxCount: 1,
cancellationToken: ct
);

if (await result.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound)
{
return null;
}

return await result.FirstAsync(ct).ConfigureAwait(false);
}

public static async Task AppendToStreamWithSingleEvent(
this EventStoreClient eventStore,
string id,
object @event,
CancellationToken ct
)
{
var eventData = new[] { @event.ToJsonEventData() };

var result = await eventStore.AppendToStreamAsync(
id,
StreamState.StreamExists,
eventData,
options =>
{
options.ThrowOnAppendFailure = false;
},
cancellationToken: ct
).ConfigureAwait(false);

if (result is SuccessResult)
return;

await eventStore.SetStreamMetadataAsync(
id,
StreamState.NoStream,
new StreamMetadata(maxCount: 1),
cancellationToken: ct
).ConfigureAwait(false);

await eventStore.AppendToStreamAsync(
id,
StreamState.NoStream,
eventData,
cancellationToken: ct
).ConfigureAwait(false);
}
}
6 changes: 3 additions & 3 deletions Core.Kafka.Tests/Core.Kafka.Tests.csproj
Expand Up @@ -15,15 +15,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<Import Project="..\Tests.Build.props"/>
<Import Project="..\Tests.Build.props" />

</Project>
4 changes: 2 additions & 2 deletions Core.Kafka/Core.Kafka.csproj
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="1.9.3" />
<PackageReference Include="Confluent.Kafka" Version="2.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions Core.Marten/Core.Marten.csproj
Expand Up @@ -6,9 +6,9 @@

<ItemGroup>
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="MediatR" Version="11.1.0" />
<PackageReference Include="MediatR" Version="12.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions Core.Testing/Core.Testing.csproj
Expand Up @@ -13,9 +13,9 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Open.ChannelExtensions" Version="6.2.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="7.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.3" />
<PackageReference Include="Ogooreck" Version="0.6.0" />
</ItemGroup>

Expand Down
12 changes: 6 additions & 6 deletions Core.Tests/Core.Tests.csproj
Expand Up @@ -11,20 +11,20 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj"/>
<ProjectReference Include="..\Core\Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.8.0"/>
<PackageReference Include="Marten" Version="6.0.0-alpha.4"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="FluentAssertions" Version="6.10.0" />
<PackageReference Include="Marten" Version="6.0.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<Import Project="..\Tests.Build.props"/>
<Import Project="..\Tests.Build.props" />

</Project>
4 changes: 2 additions & 2 deletions Core.WebApi/Core.WebApi.csproj
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Core/Core.csproj
Expand Up @@ -9,15 +9,15 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="RestSharp" Version="108.0.3" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.9" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.1" />
<PackageReference Include="Scrutor" Version="4.2.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.2" />
<PackageReference Include="Scrutor" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions Core/Events/Config.cs
Expand Up @@ -13,4 +13,22 @@ public static class Config
.AddTransient<TEventHandler>()
.AddTransient<IEventHandler<TEvent>>(sp => sp.GetRequiredService<TEventHandler>());
}

public static IServiceCollection AddEventHandler<TEvent>(
this IServiceCollection services,
Func<IServiceProvider, TEvent, CancellationToken, Task> handler
)
{
return services
.AddTransient<IEventHandler<TEvent>>(sp => new EventHandler<TEvent>((e, ct) => handler(sp, e, ct)));
}

public static IServiceCollection AddEventHandler<TEvent>(
this IServiceCollection services,
Func<TEvent, CancellationToken, Task> handler
)
{
return services
.AddTransient<IEventHandler<TEvent>>(_ => new EventHandler<TEvent>(handler));
}
}
11 changes: 11 additions & 0 deletions Core/Events/IEventHandler.cs
Expand Up @@ -4,3 +4,14 @@ public interface IEventHandler<in TEvent>
{
Task Handle(TEvent @event, CancellationToken ct);
}

public class EventHandler<TEvent> : IEventHandler<TEvent>
{
private readonly Func<TEvent,CancellationToken,Task> handler;

public EventHandler(Func<TEvent, CancellationToken, Task> handler) =>
this.handler = handler;

public Task Handle(TEvent @event, CancellationToken ct) =>
handler(@event, ct);
}
31 changes: 31 additions & 0 deletions Core/Reflection/ObjectFactory.cs
@@ -0,0 +1,31 @@
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization;

namespace Core.Reflection;

public static class ObjectFactory<T>
{
public static readonly Func<T> GetDefaultOrUninitialized = Creator();

private static Func<T> Creator()
{
var t = typeof(T);
if (t == typeof(string))
return Expression.Lambda<Func<T>>(Expression.Constant(string.Empty)).Compile();

if (t.HasDefaultConstructor())
return Expression.Lambda<Func<T>>(Expression.New(t)).Compile();

return () => (T)FormatterServices.GetUninitializedObject(t);
}
}

public static class ObjectFactory
{
public static bool HasDefaultConstructor(this Type t)
{
return t.IsValueType || t.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null, Type.EmptyTypes, null) != null;
}
}

0 comments on commit d8674d2

Please sign in to comment.