我已经实现了 RavenDB非规范化参考模式。我正在努力将静态索引和补丁更新请求连接在一起,以确保在更改引用的实例值时更新我的非规范化引用属性值。
这是我的域:
public class User
{
public string UserName { get; set; }
public string Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class UserReference
{
public string Id { get; set; }
public string UserName { get; set; }
public static implicit operator UserReference(User user)
{
return new UserReference
{
Id = user.Id,
UserName = user.UserName
};
}
}
public class Relationship
{
public string Id { get; set; }
public UserReference Mentor { get; set; }
public UserReference Mentee { get; set; }
}
您可以看到 UserReference 包含被引用用户的 Id 和 UserName。所以现在,如果我更新给定用户实例的用户名,那么我希望所有用户引用中的引用用户名值也更新。为此,我编写了一个静态索引和一个补丁请求,如下所示:
public class Relationships_ByMentorId : AbstractIndexCreationTask<Relationship>
{
public Relationships_ByMentorId()
{
Map = relationships => from relationship in relationships
select new {MentorId = relationship.Mentor.Id};
}
}
public static void SetUserName(IDocumentSession db, User mentor, string userName)
{
mentor.UserName = userName;
db.Store(mentor);
db.SaveChanges();
const string indexName = "Relationships/ByMentorId";
RavenSessionProvider.UpdateByIndex(indexName,
new IndexQuery
{
Query = string.Format("MentorId:{0}", mentor.Id)
},
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Mentor",
Nested = new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "UserName",
Value = userName
},
}
}
},
allowStale: false);
}
最后是由于更新未按预期工作而失败的 UnitTest。
[Fact]
public void Should_update_denormalized_reference_when_mentor_username_is_changed()
{
using (var db = Fake.Db())
{
const string userName = "updated-mentor-username";
var mentor = Fake.Mentor(db);
var mentee = Fake.Mentee(db);
var relationship = Fake.Relationship(mentor, mentee, db);
db.Store(mentor);
db.Store(mentee);
db.Store(relationship);
db.SaveChanges();
MentorService.SetUserName(db, mentor, userName);
relationship = db
.Include("Mentor.Id")
.Load<Relationship>(relationship.Id);
relationship.ShouldNotBe(null);
relationship.Mentor.ShouldNotBe(null);
relationship.Mentor.Id.ShouldBe(mentor.Id);
relationship.Mentor.UserName.ShouldBe(userName);
mentor = db.Load<User>(mentor.Id);
mentor.ShouldNotBe(null);
mentor.UserName.ShouldBe(userName);
}
}
一切运行良好,索引在那里,但我怀疑这没有返回补丁请求所需的关系,但老实说,我已经没有天赋了。你能帮忙吗?
编辑 1
@MattWarrenallowStale=true
没有帮助。但是我注意到了一个潜在的线索。
因为这是一个单元测试,所以我使用的是 InMemory,嵌入式 IDocumentSession -Fake.Db()
在上面的代码中。然而,当调用静态索引时,即在执行 时UpdateByIndex(...)
,它使用一般的 IDocumentStore,而不是特定的假 IDocumentSession。
当我更改索引定义类然后运行单元测试时,索引会在“真实”数据库中更新,并且可以通过 Raven Studio 看到更改。但是,“保存”到 InMemory db 的假域实例(mentor
等mentee
)并未存储在实际数据库中(如预期的那样),因此无法通过 Raven Studio 看到。
会不会是我对 的调用UpdateByIndex(...)
是针对不正确的 IDocumentSession 运行的,它是“真实”的(没有保存的域实例),而不是假的?
编辑 2 - @Simon
我已经针对上面编辑 1 中概述的问题实施了您的修复,我认为我们正在取得进展。IDocumentStore
你是对的,我使用的是对via 的静态引用RavenSessionProvder
。现在情况并非如此。下面的代码已更新为使用Fake.Db()
。
public static void SetUserName(IDocumentSession db, User mentor, string userName)
{
mentor.UserName = userName;
db.Store(mentor);
db.SaveChanges();
const string indexName = "Relationships/ByMentorId";
db.Advanced.DatabaseCommands.UpdateByIndex(indexName,
new IndexQuery
{
Query = string.Format("MentorId:{0}", mentor.Id)
},
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Mentor",
Nested = new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "UserName",
Value = userName
},
}
}
},
allowStale: false);
}
}
你会注意到我也重置了allowStale=false
. 现在,当我运行它时,我收到以下错误:
Bulk operation cancelled because the index is stale and allowStale is false
我认为我们已经解决了第一个问题,现在我使用了正确的 Fake.Db,我们遇到了首先突出显示的问题,即索引过时,因为我们在单元测试中运行速度超快。
现在的问题是:如何让UpdateByIndex(..)
方法等到 command-Q 为空并且索引被认为是“新鲜的”?
编辑 3
考虑到防止过时索引的建议,我将代码更新如下:
public static void SetUserName(IDocumentSession db, User mentor, string userName)
{
mentor.UserName = userName;
db.Store(mentor);
db.SaveChanges();
const string indexName = "Relationships/ByMentorId";
// 1. This forces the index to be non-stale
var dummy = db.Query<Relationship>(indexName)
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.ToArray();
//2. This tests the index to ensure it is returning the correct instance
var query = new IndexQuery {Query = "MentorId:" + mentor.Id};
var queryResults = db.Advanced.DatabaseCommands.Query(indexName, query, null).Results.ToArray();
//3. This appears to do nothing
db.Advanced.DatabaseCommands.UpdateByIndex(indexName, query,
new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Mentor",
Nested = new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "UserName",
Value = userName
},
}
}
},
allowStale: false);
}
从上面编号的评论中:
放入一个虚拟查询以强制索引等到它不是陈旧的作品。消除了有关过时索引的错误。
这是一条测试线,以确保我的索引正常工作。看来没问题。返回的结果是提供的 Mentor.Id ('users-1') 的正确关系实例。
{ "Mentor": { "Id": "users-1", "UserName": "Mr. Mentor" }, "Mentee": { "Id": "users-2", "UserName": "Mr. Mentee " } ... }
尽管索引不是陈旧的并且看似正常运行,但实际的补丁请求似乎什么也没做。Mentor 的非规范化参考中的用户名保持不变。
所以怀疑现在落在了补丁请求本身上。为什么这不起作用?这可能是我设置要更新的 UserName 属性值的方式吗?
...
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "UserName",
Value = userName
}
...
您会注意到,我只是将userName
参数的字符串值直接分配给类型为 的Value
属性RavenJToken
。这可能是一个问题吗?
编辑 4
极好的!我们有一个解决方案。我已经修改了我的代码,以允许你们提供的所有新信息(谢谢)。以防万一有人真正读到这里,我最好放入工作代码以关闭它们:
单元测试
[Fact]
public void Should_update_denormalized_reference_when_mentor_username_is_changed()
{
const string userName = "updated-mentor-username";
string mentorId;
string menteeId;
string relationshipId;
using (var db = Fake.Db())
{
mentorId = Fake.Mentor(db).Id;
menteeId = Fake.Mentee(db).Id;
relationshipId = Fake.Relationship(db, mentorId, menteeId).Id;
MentorService.SetUserName(db, mentorId, userName);
}
using (var db = Fake.Db(deleteAllDocuments:false))
{
var relationship = db
.Include("Mentor.Id")
.Load<Relationship>(relationshipId);
relationship.ShouldNotBe(null);
relationship.Mentor.ShouldNotBe(null);
relationship.Mentor.Id.ShouldBe(mentorId);
relationship.Mentor.UserName.ShouldBe(userName);
var mentor = db.Load<User>(mentorId);
mentor.ShouldNotBe(null);
mentor.UserName.ShouldBe(userName);
}
}
假货
public static IDocumentSession Db(bool deleteAllDocuments = true)
{
var db = InMemoryRavenSessionProvider.GetSession();
if (deleteAllDocuments)
{
db.Advanced.DatabaseCommands.DeleteByIndex("AllDocuments", new IndexQuery(), true);
}
return db;
}
public static User Mentor(IDocumentSession db = null)
{
var mentor = MentorService.NewMentor("Mr. Mentor", "mentor@email.com", "pwd-mentor");
if (db != null)
{
db.Store(mentor);
db.SaveChanges();
}
return mentor;
}
public static User Mentee(IDocumentSession db = null)
{
var mentee = MenteeService.NewMentee("Mr. Mentee", "mentee@email.com", "pwd-mentee");
if (db != null)
{
db.Store(mentee);
db.SaveChanges();
}
return mentee;
}
public static Relationship Relationship(IDocumentSession db, string mentorId, string menteeId)
{
var relationship = RelationshipService.CreateRelationship(db.Load<User>(mentorId), db.Load<User>(menteeId));
db.Store(relationship);
db.SaveChanges();
return relationship;
}
用于单元测试的 Raven 会话提供程序
public class InMemoryRavenSessionProvider : IRavenSessionProvider
{
private static IDocumentStore documentStore;
public static IDocumentStore DocumentStore { get { return (documentStore ?? (documentStore = CreateDocumentStore())); } }
private static IDocumentStore CreateDocumentStore()
{
var store = new EmbeddableDocumentStore
{
RunInMemory = true,
Conventions = new DocumentConvention
{
DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites,
IdentityPartsSeparator = "-"
}
};
store.Initialize();
IndexCreation.CreateIndexes(typeof (RavenIndexes).Assembly, store);
return store;
}
public IDocumentSession GetSession()
{
return DocumentStore.OpenSession();
}
}
索引
public class RavenIndexes
{
public class Relationships_ByMentorId : AbstractIndexCreationTask<Relationship>
{
public Relationships_ByMentorId()
{
Map = relationships => from relationship in relationships
select new { Mentor_Id = relationship.Mentor.Id };
}
}
public class AllDocuments : AbstractIndexCreationTask<Relationship>
{
public AllDocuments()
{
Map = documents => documents.Select(entity => new {});
}
}
}
更新非规范化参考
public static void SetUserName(IDocumentSession db, string mentorId, string userName)
{
var mentor = db.Load<User>(mentorId);
mentor.UserName = userName;
db.Store(mentor);
db.SaveChanges();
//Don't want this is production code
db.Query<Relationship>(indexGetRelationshipsByMentorId)
.Customize(x => x.WaitForNonStaleResultsAsOfLastWrite())
.ToArray();
db.Advanced.DatabaseCommands.UpdateByIndex(
indexGetRelationshipsByMentorId,
GetQuery(mentorId),
GetPatch(userName),
allowStale: false
);
}
private static IndexQuery GetQuery(string mentorId)
{
return new IndexQuery {Query = "Mentor_Id:" + mentorId};
}
private static PatchRequest[] GetPatch(string userName)
{
return new[]
{
new PatchRequest
{
Type = PatchCommandType.Modify,
Name = "Mentor",
Nested = new[]
{
new PatchRequest
{
Type = PatchCommandType.Set,
Name = "UserName",
Value = userName
},
}
}
};
}