gpt4 book ai didi

c# - 可感知时间/更新的集合的枚举器

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:38 24 4
gpt4 key购买 nike

我有一种情况,我希望能够访问历史信息,以便回顾性地生成指标或了解过去某一点的情况,到目前为止,我一直在存储最新的内容(例如已应用所有更新的记录),但现在希望能够让时钟倒转

在推出我自己的解决方案之前:

  • 是否已经存在其他东西?
  • 这是标准模式吗?
  • 我可能在哪里遇到问题?

我不希望记录的消费者能够更改它们,因此任何“更新”都应该通过存储库进行整理,并且它将创建包含完整内容的新记录。

理想情况下,我想将其移至 SQL 后端,因此如果那里存在模式,我希望保持接近它们。


基本设计思路是:

定义一个接口(interface),比如 IUpdatableRecord:

public interface IUpdatableRecord<K>
{
K Key { get; }
DateTime Updated { get; }
}

定义具有枚举功能的存储库:

public class DataRepository : IEnumerable<IUpdateableRecord<K>>
{
// Some internal collection that allows duplicate keys
private IList<IUpdateableRecord<K>> dataStore = ....;

// Some enumerator overloads
public IEnumerator<IUpdateableRecord<K>> GetEnumerator()
{
return dataStore.GetEnumerator();
}

// enumerator for contents as of a specific date-time
public IEnumerator<IUpdateableRecord<K>> GetEnumerator(DateTime refDate)
{
// Group by key (so all versions of a record together)
var groupedByKey = dataStore.GroupBy(r => r.Key);

// Sort the keys within each group for a date/time order
foreach ( var rec in groupedByKey )
{
var sorted = rec.OrderBy(r => r.Updated);

// Ignore updates after the reference date & keep last (or default)
var last = sorted.Where(r => r.Updated < refDate).LastOrDefault();

// yield last record if any
if ( last != null )
{
yield return last;
}
}
}

// code for 'adding/updating' a record.
}

最佳答案

如果您希望此解决方案利用 SQL 后端,则应考虑 ADO.NET Entity Framework 或 Linq-SQL。

您的主要潜在问题是您的枚举器,因此您需要查看多种方法并检查 Linq 生成的 SQL(LinqPad 对此有好处)并确保其高效。

关于c# - 可感知时间/更新的集合的枚举器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4427100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com