Nul*_*ion 0 c# asynchronous ravendb
我正在尝试利用RavenDB的DocumentConvention API在我的域对象上自动设置增量ID.
我在IDocumentStore上使用这行代码完美地工作了:
DocumentStore.Conventions.RegisterIdConvention<User>
((dbname, commands, user) => "users/" + commands.NextIdentityFor("users"));
Run Code Online (Sandbox Code Playgroud)
每当我存储没有已设置ID的新User对象时,这将创建具有"users/1","users/2"等的用户对象的顺序ID .
但是,我想为Async文档会话执行此操作,但是在调用RegisterAsyncIdConvention()时无法找到有关如何从会话中获取"Next Identity"的任何文档...
docStore.Conventions.RegisterAsyncIdConvention<User>
((dbname, commands, user) => "users/" + commands.NextIdentityFor("users"));
Run Code Online (Sandbox Code Playgroud)
......不编译,因为NextIdentityFor不可用的IAsyncDatabaseCommands接口.
任何人都可以给我任何提示吗?还有人试过这个吗?
谢谢.
您使用顺序ID描述的行为已经是默认行为.您不必对约定执行任何特殊操作即可获得此行为.
它也适用于异步会话 - 但最初它实际上并没有在首次存储时设置id属性的值.这在本主题的邮件列表中进行了讨论.我相信pull请求进入了最新的不稳定版本,但我还没有验证.
更新 (来自评论)
默认情况下,约定是使用类型名称的复数形式.如果类型名称是单个单词,则以小写形式返回.如果它有多个单词,则保留外壳.
考虑:
var user = new User { Name = "Joe" };
session.Store(user);
var fooBar = new FooBar { Name = "Whatever" };
session.Store(fooBar);
Debug.WriteLine(user.Id);
Debug.WriteLine(fooBar.Id);
Run Code Online (Sandbox Code Playgroud)
如果没有更改约定,则会输出:
users/1
FooBars/1
Run Code Online (Sandbox Code Playgroud)
如果要更改此约定,只需提供一个新的lambda函数:
documentStore.Conventions.FindTypeTagName = type => type.Name.ToLower();
Run Code Online (Sandbox Code Playgroud)
运行与以前相同的代码,您现在将得到:
user/1
foobar/1
Run Code Online (Sandbox Code Playgroud)
但也许你想保持多元化,只想要全部小写?
documentStore.Conventions.FindTypeTagName = type =>
DocumentConvention.DefaultTypeTagName(type).ToLower();
Run Code Online (Sandbox Code Playgroud)
输出:
users/1
foobars/1
Run Code Online (Sandbox Code Playgroud)
也许你只想要PascalCase?
documentStore.Conventions.FindTypeTagName = type =>
{
var s = DocumentConvention.DefaultTypeTagName(type);
return s.Substring(0, 1).ToUpper() + s.Substring(1);
};
Run Code Online (Sandbox Code Playgroud)
输出:
Users/1
FooBars/1
Run Code Online (Sandbox Code Playgroud)
或perhasps camelCase总是?
documentStore.Conventions.FindTypeTagName = type =>
{
var s = DocumentConvention.DefaultTypeTagName(type);
return s.Substring(0, 1).ToLower() + s.Substring(1);
};
Run Code Online (Sandbox Code Playgroud)
输出:
users/1
fooBars/1
Run Code Online (Sandbox Code Playgroud)