如何使用 SharpRepository 为我的数据创建本地文件系统存储库

How can I use the SharpRepository to create a local filesystem repository of my data

我正在开发 .net Framework 控制台代码,它用位来做事。我想在完成生成实体后实现一个存储库来保存我的实体,这样我就不必再次生成实体。 首先,我希望存储库将数据文件写入我的本地文件系统,然后如果需要,稍后再放置一个更健壮的 (RMDBS) 后端。当然,我也希望能够将存储库 test/mock 单元化。

我在 github 上找到了 SharpRepository 项目,并且想利用它,而不是滚动我自己的实现。 XMLRepository class 看起来像我想要实现的那个,但我不确定如何实现,而且 wiki 不包含关于它的文档。

如何使用 SharpRepository 库中的 XmlRepository

经过四处探索,这就是我正在使用的,我认为可以满足我在问题中所述的需求:

using Microsoft.Extensions.Caching.Memory;
using SharpRepository.Repository;
using SharpRepository.XmlRepository;
using SharpRepository.Repository.Caching;
using SharpRepository.EfRepository;

public class MyEntity
{
    public DateTime EventDateTime;
    public Dictionary<string, string> attributes = new Dictionary<string, string>();
}
static void Main(string[] args)
{
    MemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
    InMemoryCachingProvider inMemoryCachingProvider = new InMemoryCachingProvider(memoryCache);
    IRepository<MyEntity> myRepo = new XmlRepository<MyEntity>(@"C:\temp\MyLocalRepo", new StandardCachingStrategy<MyEntity>(inMemoryCachingProvider));

    // When I am ready to further develop the data layer, I can swap the repo out for a different one
    //IRepository<MyEntity> myRepo = new EfRepository<MyEntity>(new System.Data.Entity.DbContext("myConnectionString"));

    // And my logic that interacts with the repository will still work
    myRepo.Add(new MyEntity { EventDateTime = new DateTime(2019, 06, 16), attributes = new Dictionary<string, string> { { "WithAttrbutes", "AndTheirValues" } } });
}

我推了一个分支,你可以在其中找到 SharpRepository.Samples.CoreMvc ASP.NET Core 运行 XmlRepository。

你可以在这里找到它https://github.com/SharpRepository/SharpRepository/tree/sample/xml

最后一次提交保留了一些配置以使其正常工作。 https://github.com/SharpRepository/SharpRepository/commit/77c684de40fe432589c940ad042009bbd213f96c

让我保持更新以获得 Xml存储库发布稳定。

顺便说一句,我使用 InMemoryRepository 进行测试。 Xml 序列化会限制模型的属性,并且很难继续使用不同的 DBRMS。