Brandon McClure
Brandon McClure

Reputation: 1410

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. 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. 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. 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.

How do you use the XmlRepository in the SharpRepository library?

Upvotes: 0

Views: 239

Answers (2)

fiorebat
fiorebat

Reputation: 3451

I pushed a branch where you can find SharpRepository.Samples.CoreMvc ASP.NET Core running with XmlRepository.

You can can find it here 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

Keep me update in order to get XmlRepository released stable.

BTW I use InMemoryRepository for testing. Xml serializations creates limitations in the properties of your model and its difficult to keep worging with a different DBRMS.

Upvotes: 1

Brandon McClure
Brandon McClure

Reputation: 1410

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

Upvotes: 0

Related Questions