简体   繁体   English

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

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

I am developing .net framework console code that does stuff with bits. 我正在开发.net框架控制台代码,用位填充。 I want to implement a repository to hold my entities after I am done generating them so that I do not have to generate the entities again. 在生成实体之后,我想实现一个存储库来保存我的实体,这样就不必再次生成实体。 To start with, I would like the repository to write data file(s) to my local filesystem, and then if needed later on put a more robust (RMDBS) back end. 首先,我希望存储库将数据文件写入本地文件系统,然后在以后需要时将更健壮的(RMDBS)后端放入。 I also want to be able to unit test/mock the repository as well of course. 我当然也希望能够对存储库进行单元测试/模拟。

I found the SharpRepository project on github, and would like to leverage it, instead of rolling my own implementation. 我在github上找到了SharpRepository项目,并且想利用它,而不是滚动自己的实现。 The XMLRepository class looks like the one that I want to implement but I am not sure how, and the wiki does not include documentation on it. XMLRepository类看起来像我要实现的类,但是我不确定如何实现,并且Wiki不包含有关它的文档。

How do you use the XmlRepository in the SharpRepository library? 你如何使用XmlRepositorySharpRepository库?

I pushed a branch where you can find SharpRepository.Samples.CoreMvc ASP.NET Core running with XmlRepository. 我推送了一个分支,您可以在其中找到运行XmlRepository的SharpRepository.Samples.CoreMvc ASP.NET Core。

You can can find it here https://github.com/SharpRepository/SharpRepository/tree/sample/xml 您可以在这里找到它https://github.com/SharpRepository/SharpRepository/tree/sample/xml

Last commit holds some configurations in order to get it working. 最后一次提交保留一些配置,以使其正常工作。 https://github.com/SharpRepository/SharpRepository/commit/77c684de40fe432589c940ad042009bbd213f96c https://github.com/SharpRepository/SharpRepository/commit/77c684de40fe432589c940ad042009bbd213f96c

Keep me update in order to get XmlRepository released stable. 保持更新以使XmlRepository稳定发布。

BTW I use InMemoryRepository for testing. 顺便说一句,我使用InMemoryRepository进行测试。 Xml serializations creates limitations in the properties of your model and its difficult to keep worging with a different DBRMS. Xml序列化会限制模型的属性,并且很难与其他DBRMS一起工作。

From poking around, this is what I am using which I think will meet my needs stated in the question: 从一开始,这就是我正在使用的东西,我认为它将满足我在问题中所述的需求:

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" } } });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM