Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/YesSql.Abstractions/Entites/IVersionable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace YesSql.Entites
{
public interface IVersionable
{
[JsonIgnore]
long Version { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/YesSql.Core/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using YesSql.Commands;
using YesSql.Data;
using YesSql.Entites;
using YesSql.Indexes;
using YesSql.Services;

Expand Down Expand Up @@ -569,6 +570,11 @@ public IEnumerable<T> Get<T>(IList<Document> documents, string collection) where
accessor = defaultAccessor;
}

if (item is IVersionable versionedEntity)
{
versionedEntity.Version = d.Version;
}

accessor?.Set(item, d.Id);

if (_withTracking)
Expand Down
19 changes: 19 additions & 0 deletions test/YesSql.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,25 @@ public async Task ShouldQueryNullVariables()
}
}

[Fact]
public async Task ShouldAccessVersion()
{
_store.RegisterIndexes<ArticleBydPublishedDateProvider>();

await using (var session = _store.CreateSession())
{
var article = new Article { Title = TestConstants.Strings.SomeString, PublishedUtc = new DateTime(2011, 11, 1) };
await session.SaveAsync(article);
await session.SaveChangesAsync();
Assert.Equal(1, article.Version);

article.Title = "Version2";
await session.SaveAsync(article);
await session.SaveChangesAsync();
Assert.Equal(2, article.Version);
}
}

[Fact]
public async Task ShouldCompareWithConstants()
{
Expand Down
4 changes: 3 additions & 1 deletion test/YesSql.Tests/Models/Article.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using YesSql.Entites;

namespace YesSql.Tests.Models
{
public class Article
public class Article : IVersionable
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime PublishedUtc { get; set; }
public bool IsPublished { get; set; }
public long Version { get; set; } = 0;
}
}