We are using SharpRepository on top of EntityFramework 6, that's working great so far. We are also implementing full unit testing, in researching the approved method to unit test with SharpRepository is with the CacheRepository, this lets us populate a repo with consistent test data and then tests services and models against those test repositories. This is also working for the most part.

The Issue

We have populated these repos successfully, with a single object, then we can pull that object out and test against it. but a new test use case, required us to test against multiple entries, when we add them, only the last entry is actually available in the Repo. I see this both from the unit test output, and inspecting the repo in the watch window while debugging, it only ever has one.

I have setup a standard cache strategy and an inmemory provider, i have also tried adding each object individually and as a list, with the Add function.

The Code

the Setup

    [TestInitialize]
    public void Initialize()
    {
        var sharpRepositoryConfiguration = new SharpRepositoryConfiguration();
        sharpRepositoryConfiguration.AddCachingStrategy(new StandardCachingStrategyConfiguration("standard"));
        sharpRepositoryConfiguration.AddCachingProvider(new InMemoryCachingProviderConfiguration("inmemory"));
        sharpRepositoryConfiguration.AddRepository(new CacheRepositoryConfiguration("textFilter", "TextFilter", "standard", "inmemory"));

        textFilterRepo = sharpRepositoryConfiguration.GetInstance<TextFilter, int>("textFilter");
    }

    [TestCleanup]
    public void CleanUp()
    {
        textFilterRepo.Delete(c => true);
    }

these methods should only be called before and after each test method respectively, so it should not cause issue inside the method itself.

The Add

        List<TextFilter> badwords;
        var badword1= new TextFilter
        {
            ID = 0,
            FilterType = 1,
            FilterText = "badword1",
            DateTimeCreated = DateTime.Now
        };
        var badword2 = new TextFilter
        {
            ID = 1,
            FilterType = 1,
            FilterText = "badword2",
            DateTimeCreated = DateTime.Now
        };
        var badword3 = new TextFilter
        {
            ID = 2,
            FilterType = 1,
            FilterText = "badword3",
            DateTimeCreated = DateTime.Now
        };

        badwords = new List<TextFilter>
        {
            badword1,
            badword2,
            badword3
        };

        textFilterRepo.Add(badwords);

As i said before i've tried both individual add calls and now the list, same results.

EDIT: SharpRepositorySettings I'm not sure why CacheUsed is set to false, i do not have additional settings for these, but i'm wondering if this is part of the problem and if I need to call something explicitly to get this to work.

Research

I've researched this fairly extensively, both Sharp Repository in general and unit testing with it specifically.

I've seen this SO, which led me to use the CacheRepository, How can I seed SharpRepository's InMemoryRepository? . But I have not been able to find any info on this information. It could be that i have setup the caching for the repository incorrectly, but this is what it typically looks like in the examples i've seen. I have used this successfully in many unit tests, but have not had to populate multiple values before.

Request

I'm looking for both help in identifying why this setup cannot add multiple objects to it's cache, and how i can change this code, or my unit test setup in general to be able to accommodate multiple objects in my CacheRepository.

有帮助吗?

解决方案

So i finally hopped into the Sharp Repo source code, and found my issues. First i will apologize, much of this could have been avoided, i sanitized my code example and naming conventions to make it easier to read, which covered up that my key value had a naming convention mismatch with Sharp Repo. the issue is that sharp uses a convention based mapping for ID (which is actually a huge convenience).

enter image description here enter image description here

for those interested i found this by tracing through the AddItem method in the CacheRepositoryBase.cs. The convention specific code is in DefaultRepositoryConventions.cs you'd need to pull the source to see that obviously but for future knowledge seekers, if you're having issues that might be worth a check.

It's worth mentioning that i also got the [RepositoryPrimaryKey] Attribute to work, as well as renaming my property to match the convention, both worked equally well. The annotation will be particularly helpful with auto-generated EF entities by using a MetaData partial class.

Many thanks to @JeffTreuting, for is help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top