- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试将更新推广到我的 mongodb,这样我就可以更新我的数据 - 存储不可知。使用 EntityFramework,这非常容易,因为它具有更改跟踪功能,因此我可以将业务逻辑和特定于数据库的操作分开。至于 MongoDb,就没那么容易了,因为我必须创建这些 UpdateDefinitions,才能对文档进行更新。
模仿此操作的一种简单方法是使用 ReplaceOneAsync
,但这不是一种选择,因为不止一方可能同时写入我的集合。例如,我在我的应用程序中将它用于聊天系统。
我想做这样的事情:
public class MongoActionWrapper<TCollection>
{
public MongoActionWrapper(Action<TCollection> updateAction)
{
UpdateAction = updateAction;
}
public Action<TCollection> UpdateAction { get; }
}
然后我有我的 MongoRepository:
public abstract class BaseMongoRepository<TAggregate, TCollection> : IRepository<TAggregate> where TAggregate : BaseAggregateRoot, IMongoAggregate<TCollection>
{
private readonly IMongoCollection<TCollection> _mongoCollection;
protected BaseMongoRepository(IMongoCollection<TCollection> mongoCollection)
{
_mongoCollection = mongoCollection;
}
public async void SaveAsync(TAggregate aggregate)
{
var state = aggregate.GetState();
foreach (var action in state.ActionsToExecuteOnCollection)
{
await _mongoCollection.UpdateOneAsync<TCollection>(aggregate.GetSelector(), action.UpdateAction);
}
}
}
我想在集合而不是 UpdateDefinition 上执行操作的位置。或者以某种方式将我的 Action 转换为 UpdateDefinition。
这将使我能够在列表(或我想存储数据的任何其他集合)上应用相同的更新,这样我就不会被迫留在 mongodb 上。
到目前为止,我唯一的解决方案是让我的 UpdateAction
更像一个描述更新的对象,然后在我想将其保存在 mongodb 中时转换这些对象。这样我就可以将它们转换为 Actions 并将它们应用到我的收藏或我选择的任何其他数据库。
我希望有人有想法,因为我在这里没有其他选择。
最佳答案
我不确定你是如何使用 Action delegate 来做到这一点的,但是如果你想在单个 AggregateRoot 上重现 EntityFramework,比如 Read-Through、Write-Through 缓存和多个并发写入,你可以尝试这样的事情:
public abstract class AggregateRoot<T> where T : AggregateRoot<T>
{
internal UpdateDefinition<T> UpdateDefinition { get; internal set; }
internal void Aggregate(UpdateDefinition<T> component)
{
if (component == null)
{
throw new ArgumentNullException("component");
}
if (this.UpdateDefinition == null)
{
this.UpdateDefinition = component;
}
else
{
this.UpdateDefinition = Builders<T>.Update.Combine
(this.UpdateDefinition, component);
}
}
}
并且比存储库基类中的:
public class Repository<T> where T : AggregateRoot<T>
{
private ConcurrentDictionary<ObjectId, T> cache
= new ConcurrentDictionary<ObjectId, T>();
private IMongoCollection<T> collection;
public Repository(string connectionString, string databaseName, string collectionName)
{
collection = new MongoClient(connectionString)
.GetDatabase(databaseName)
.GetCollection<T>(collectionName);
}
public T Find(ObjectId aggregateRootId)
{
return cache.GetOrAdd(
key: aggregateRootId,
valueFactory: key => findById(key));
}
private T findById(ObjectId aggregateRootId)
{
return collection
.Find(Builders<T>.Filter
.Eq(x => x.Id, aggregateRootId))
.SingleOrDefault();
}
public void SaveChanges()
{
var writeModels = generateWriteModels();
collection.BulkWrite(writeModels);
}
private IEnumerable<WriteModel<T>> generateWriteModels()
{
List<WriteModel<T>> writeModels = new List<WriteModel<T>>();
foreach (var cached in cache)
{
if (cached.Value != null)
{
if (cached.Value.UpdateDefinition != null)
{
writeModels.Add(new UpdateOneModel<T>(
filter: Builders<T>.Filter.Eq(x => x.Id, cached.Value.Id),
update: cached.Value.UpdateDefinition){ IsUpsert = true });
cached.Value.UpdateDefinition = null;
}
}
}
return writeModels;
}
}
通过此实现,您可以与 Aggregate(UpdateDefinition<T> component)
进行交互AggregateRoot<T>
的方法|直接在实现中使用基类(注意不要在 setter 上使用它——BsonSerializer 也使用 setter)。 Repository<T>
将消耗更改.
如果你想让你的领域模型尽可能地与商店实现分离,你可以构建一个等价于 MongoActionWrapper<TCollection>
的模型。作为聚合的包装器。这些包装器可以将聚合方法与 Aggregate(UpdateDefinition<T> component)
一起使用使更新定义与对聚合所做的更改保持同步。
关于C# Mongodb 驱动程序 : Translate Action into an UpdateDefinition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40343759/
我听说 Translate API 需要付费,但究竟是什么阻止了我们使用免费的 Google 翻译服务 here免费 ?否则,免费服务的限制是什么? 最佳答案 根据下面的链接,没有什么可以阻止您。 h
我正在修复我的 Karma 配置以运行 Angular2 - rc 1 版本的测试。我可以运行测试,但如果我在 html 中有一个翻译管道,它们就会失败。(我可以让它工作的配置是从 [这里][1] 得
我正在使用以下代码: GttService myService = new GttService("ex1cor.ex1Ap.1"); myService.setUserCredentials("ex
是否可以在 Silverstripe 3 中翻译数据对象? 我使用这个模块: http://www.silverstripe.org/translatable-module/ 在我的配置中是否定义了以
我有以下三个问题 我想使用 Google 的 API 来翻译文本。我知道谷歌对翻译和检测单独收费。谷歌翻译也支持翻译两种翻译方式 i) 通过指定源和目标,如 https://www.googleapi
我一直在使用 Microsoft Translator 的 HTTP API 来翻译我网站上的文本。 作为the documentation describes , 有一个选项可以指定翻译的类别。找不
当您在 localhost 上开发应用程序时,是否有机会在不获取 key 的情况下使用 Use Google Translation API? 我希望这样的事情能够奏效' https://www.go
我正在尝试翻译实体的某些字段,但在尝试创建对象时出现以下错误... id; } /** * Set name * * @param string $nam
当用户访问我们的网站时,我们使用 Google Translate API 将我们的内容翻译成用户的语言。 (当然,我们遵循署名和链接要求,以便用户知道内容是 Google 的翻译。) 为了优化,我们
我非常频繁地使用谷歌翻译 API V2,在大约 2000 个请求之后,我开始在返回的 JSON 中得到这个: Array ( [error] => Array (
我刚开始使用 Google 翻译 API,在测试过程中我们注意到,对于某些翻译(我尚未找到模式),我们会在响应中收到\u200b 字符。这会导致很多问题,最重要的是,它似乎没有任何目的或没有任何意义。
从laravel 5.8升级到laravel 6.0后,发现这个错误。 Method Illuminate\Translation\Translator::getFromJson does not e
这是一个基本的移动滑入/滑出菜单。 我发现很难调试,但基本上我的问题是,当我按下菜单按钮时,菜单会顺利打开,再次按下它会顺利关闭。然而,当我再次按下它(第三次)时,它不顺利打开,它只是出现。但是它仍然
我的 Android Studio 的翻译编辑器无法正常工作。如果我打开翻译编辑器确实列出了字符串的正确键,但是找不到默认值和翻译。所有键都有一个默认值,其中大多数也有一个翻译。 我重新启动了 And
我正在尝试从 python 控制台而不是通过 bazel -build 运行 Tensorflow 的 translate.py,但我在这两行出现错误: from tensorflow.models.
本文整理了Java中org.apache.tika.language.translate.YandexTranslator.translate()方法的一些代码示例,展示了YandexTranslat
我需要来自 Google Transle API 的同义词信息。有没有可能得到它? 最佳答案 抱歉不行。看到这个 post .看起来他们opened a PIT添加功能。你应该给它加星号来增加重量!
我在我的一个项目中使用 Google Cloud Translation API。我想指定翻译的性别。我无法在 Google Cloud Translation 中找到相关信息。我也在互联网上搜索了很
我已经在我的 Angular-Cli 应用程序中实现了 ngx-translate 并且在我执行以下操作时工作正常: {{ 'some.value' | translate }} 但是我该如何翻译 H
我决定在我的项目中使用 Google Cloud Translation API。在我尝试运行他们的脚本之前,一切似乎都很好。它总是说我需要“使用 require([])”。 在我在 require
我是一名优秀的程序员,十分优秀!