简体   繁体   English

如何为SharpRepository的InMemoryRepository提供种子?

[英]How can I seed SharpRepository's InMemoryRepository?

As the title says, how can I seed SharpRepository's InMemoryRepository with some dummy data? 正如标题所说,我如何使用一些虚拟数据为SharpRepository's InMemoryRepository提供种子?

I tried this in my configuration: 我在配置中尝试了这个:

var sharpRepositoryConfiguration = new SharpRepositoryConfiguration();
sharpRepositoryConfiguration.AddRepository(new InMemoryRepositoryConfiguration("inmemory"));
sharpRepositoryConfiguration.DefaultRepository = "inmemory";
var repo = sharpRepositoryConfiguration.GetInstance<Account>().Add(new [] { .... });

return new StructureMapDependencyResolver(sharpRepositoryConfiguration);

But, when the repository is resolved in my controllers, the data was not there. 但是,当在我的控制器中解析存储库时,数据不存在。 I was in the understanding that InMemoryRepository would function as a data store that was in memory - but is it just the case that InMemoryRepository is purely just storing data in the repositories and never pushing them to an underlying in-memory data store? 我理解InMemoryRepository可以作为内存中的数据存储 - 但是InMemoryRepository纯粹只是将数据存储在存储库中并且永远不会将它们推送到底层的内存数据存储中吗?

It's all very well being able to unit test and mock SharpRepository, but I can't really develop without being able to see some seeded data coming into my views :( 能够进行单元测试和模拟SharpRepository非常好,但是如果没有能够看到一些种子数据进入我的视图,我就无法真正开发:(

EDIT: I managed to do this by setting my repository implementation as a Singleton: 编辑:我设法通过将我的存储库实现设置为单例:

x.For<IAccountRepository>()
    .Singleton()
    .Use<SharpRepositoryAccountRepository>();

This is then injected into my controller. 然后将其注入我的控制器。 By setting it as singleton the data persists across requests. 通过将其设置为单例,数据会在请求之间保持不变。

The InMemoryRepositoryConfiguration returns a new repository every time (see the source for the factory here ), so it is not suitable for dependency injection if it's set up as transient (versus singleton). InMemoryRepositoryConfiguration每次都返回一个新的存储库(请参阅此处的工厂源代码),因此如果将其设置为transient(而不是singleton),则不适合依赖注入。

If you're trying to use this in unit tests and are trying to stay in-memory, have a look at the CacheRepository , I believe that will work how you'd like out of the box: 如果你试图在单元测试中使用它并试图保持在内存中,看看CacheRepository ,我相信这将是你想开箱即用的方式:

var sharpRepositoryConfiguration = new SharpRepositoryConfiguration();
sharpRepositoryConfiguration.AddRepository(new CacheRepositoryConfiguration("inmemory"));
sharpRepositoryConfiguration.DefaultRepository = "inmemory";
var repo = sharpRepositoryConfiguration.GetInstance<Account>().Add(new [] { .... });

return new StructureMapDependencyResolver(sharpRepositoryConfiguration);

It uses an InMemoryCachingProvider by default, which uses the MemoryCache for persistence. 它默认使用InMemoryCachingProvider ,它使用MemoryCache进行持久化。 Add a prefix to the CacheRepositoryConfiguration constructor if you have more repositories to avoid them stomping on each other. 如果您有更多的存储库以避免它们相互踩踏,请向CacheRepositoryConfiguration构造函数添加前缀。

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

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