Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore MartenOps.Store and add MartenOps.StoreMany #1158

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions src/Persistence/Wolverine.Marten/IMartenOp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,35 @@ public static class MartenOps
/// <summary>
/// Return a side effect of storing the specified document in Marten
/// </summary>
/// <param name="document"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static StoreDoc<T> Store<T>(T document) where T : notnull
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}

return new StoreDoc<T>(document);
}

/// <summary>
/// Return a side effect of storing many documents of a specific document type in Marten
/// </summary>
/// <param name="documents"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static StoreDoc<T> Store<T>(params T[] documents) where T : notnull
public static StoreManyDocs<T> StoreMany<T>(params T[] documents) where T : notnull
{
if (documents == null)
{
throw new ArgumentNullException(nameof(documents));
}

return new StoreDoc<T>(documents);
return new StoreManyDocs<T>(documents);
}

/// <summary>
Expand Down Expand Up @@ -225,12 +242,27 @@ public void Execute(IDocumentSession session)
}

public class StoreDoc<T> : DocumentOp where T : notnull
{
private readonly T _document;

public StoreDoc(T document) : base(document)
{
_document = document;
}

public override void Execute(IDocumentSession session)
{
session.Store(_document);
}
}

public class StoreManyDocs<T> : DocumentsOp where T : notnull
{
private readonly T[] _documents;

public StoreDoc(params T[] documents) : base(documents)
public StoreManyDocs(IEnumerable<T> documents) : base(documents.Cast<object>())
jay-zahiri marked this conversation as resolved.
Show resolved Hide resolved
{
_documents = documents;
_documents = documents.ToArray();
}

public override void Execute(IDocumentSession session)
Expand Down Expand Up @@ -285,12 +317,24 @@ public override void Execute(IDocumentSession session)
}

public abstract class DocumentOp : IMartenOp
{
public object Document { get; }

protected DocumentOp(object document)
{
Document = document;
}

public abstract void Execute(IDocumentSession session);
}

public abstract class DocumentsOp : IMartenOp
{
public object[] Documents { get; }

protected DocumentOp(params object[] documents)
protected DocumentsOp(IEnumerable<object> documents)
jay-zahiri marked this conversation as resolved.
Show resolved Hide resolved
{
Documents = documents;
Documents = documents.ToArray();
}

public abstract void Execute(IDocumentSession session);
Expand Down
Loading