-
-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'oskardudycz-feature/AddPossibilityToInjectClassesToProj…
…ection'
- Loading branch information
Showing
4 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Marten.Testing/Events/Projections/lazy_loaded_projection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Marten.Services; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Marten.Testing.Events.Projections | ||
{ | ||
public class lazy_loaded_projection : DocumentSessionFixture<IdentityMap> | ||
{ | ||
public class Logger | ||
{ | ||
public List<string> Logs { get; } = new List<string>(); | ||
|
||
public void Log(string message) | ||
{ | ||
Logs.Add(message); | ||
} | ||
} | ||
|
||
public class QuestPaused | ||
{ | ||
public string Name { get; set; } | ||
public Guid QuestId { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"Quest {Name} paused"; | ||
} | ||
} | ||
|
||
// SAMPLE: viewprojection-from-class-with-injection | ||
public class PersistViewProjectionWithInjection : PersistViewProjection | ||
{ | ||
private readonly Logger logger; | ||
|
||
public PersistViewProjectionWithInjection() : base() | ||
{ | ||
ProjectEvent<QuestPaused>(@event => @event.QuestId, LogAndPersist); | ||
} | ||
|
||
public PersistViewProjectionWithInjection(Logger logger) : this() | ||
{ | ||
this.logger = logger; | ||
} | ||
|
||
private void LogAndPersist<T>(PersistedView view, T @event) | ||
{ | ||
logger.Log($"Handled {typeof(T).Name} event: {@event.ToString()}"); | ||
view.Events.Add(@event); | ||
} | ||
} | ||
// ENDSAMPLE | ||
|
||
private static readonly Guid streamId = Guid.NewGuid(); | ||
|
||
private QuestStarted started = new QuestStarted { Id = streamId, Name = "Find the Orb" }; | ||
private MembersJoined joined = new MembersJoined { QuestId = streamId, Day = 2, Location = "Faldor's Farm", Members = new[] { "Garion", "Polgara", "Belgarath" } }; | ||
private QuestPaused paused = new QuestPaused { QuestId = streamId, Name = "Find the Orb" }; | ||
|
||
[Fact] | ||
public void from_projection() | ||
{ | ||
var logger = new Logger(); | ||
|
||
// SAMPLE: viewprojection-from-class-with-injection-configuration | ||
StoreOptions(_ => | ||
{ | ||
_.AutoCreateSchemaObjects = AutoCreate.All; | ||
_.Events.InlineProjections.AggregateStreamsWith<QuestParty>(); | ||
_.Events.InlineProjections.Add(() => new PersistViewProjectionWithInjection(logger)); | ||
}); | ||
// ENDSAMPLE | ||
|
||
theSession.Events.StartStream<QuestParty>(streamId, started, joined); | ||
theSession.SaveChanges(); | ||
|
||
var document = theSession.Load<PersistedView>(streamId); | ||
document.Events.Count.ShouldBe(2); | ||
logger.Logs.Count.ShouldBe(0); | ||
|
||
//check injection | ||
theSession.Events.Append(streamId, paused); | ||
theSession.SaveChanges(); | ||
|
||
var document2 = theSession.Load<PersistedView>(streamId); | ||
document2.Events.Count.ShouldBe(3); | ||
|
||
logger.Logs.Count.ShouldBe(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Marten.Events.Projections.Async; | ||
using Marten.Storage; | ||
|
||
namespace Marten.Events.Projections | ||
{ | ||
public class LazyLoadedProjection<T> : IProjection | ||
where T : IProjection, new() | ||
{ | ||
private readonly Func<T> factory; | ||
|
||
public LazyLoadedProjection(Func<T> factory) | ||
{ | ||
this.factory = factory; | ||
var definition = new T(); | ||
|
||
Consumes = definition.Consumes; | ||
AsyncOptions = definition.AsyncOptions; | ||
} | ||
|
||
public Type[] Consumes { get; } | ||
|
||
public AsyncOptions AsyncOptions { get; } | ||
|
||
public void Apply(IDocumentSession session, EventPage page) | ||
{ | ||
factory().Apply(session, page); | ||
} | ||
|
||
public Task ApplyAsync(IDocumentSession session, EventPage page, CancellationToken token) | ||
{ | ||
return factory().ApplyAsync(session, page, token); | ||
} | ||
|
||
public void EnsureStorageExists(ITenant tenant) | ||
{ | ||
factory().EnsureStorageExists(tenant); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters