-
Notifications
You must be signed in to change notification settings - Fork 164
Configuration
.NET core make a whide usage of DependencyInjections and creates a new standard for file configurations, that can't be easyly place in old app.config / web.config files and well be replaced by json files. Every package come with sample json file with sample configurations in it
Before getting into the code for loading up a repository via configuration settings it will be helpful to understand the SharpRepository hierarchy. Each repository has a specific type of backend that it queries (e.g. InMemory, Entity Framework, RavenDb, MongoDb, Db4o, Xml, etc.), a caching strategy (e.g. None, Standard, Timeout), and a caching provider (e.g. Memory, memcached, Azure, etc.).
The caching strategy tells the repository the rules for caching. The caching provider on the other hand is the specific cache technology that is used.
You can create a SharpRepositoryConfiguration object and use that to create your repository. The SharpRepositoryConfiguration object allows you to build a “profile” and then when you create a specific repository you can choose which “profile” to use. I think that the code below will help show what I mean.
var config = new SharpRepositoryConfiguration();
config.AddRepository(new Ef5RepositoryConfiguration("ef5", "DefaultConnection")); // EF repository named ef5 using the DefaultConnection connection string
config.AddRepository(new InMemoryRepositoryConfiguration("inmemory", "standard", "memcached")); // InMemoryRepository named default using the none CachingStrategy
config.AddCachingStrategy(new TimeoutCachingStrategyConfiguration("timeout", 30)); // Timeout strategy named timeout with 30 second cache time
config.AddCachingStrategy(new StandardCachingStrategyConfiguration("standard")); // Standar d strategy named standard
config.AddCachingStrategy(new NoCachingStrategyConfiguration("none")); // No caching named none`
config.AddCachingProvider(new InMemoryCachingProviderConfiguration("inmemory")); // InMemory provider name inmemory
config.AddCachingProvider(new MemCachedCachingProviderConfiguration("memcached", "memcached")); // memcached provider named memcached and using the memcached section in config file
config.DefaultRepository = "ef5";
config.DefaultCachingStrategy = "timeout";
config.DefaultCachingProvider = "inmemory";
var userRepos = RepositoryFactory.GetInstance<User, int>(config); // uses the Ef5Repository
var orderRepos = RepositoryFactory.GetInstance<Order, int>(config, "inmemory"); // uses InMemoryRepository with Standard caching strategy and memcached
As you can see above, the SharpRepositoryConfiguration gets loaded with multiple repositories that can use the various caching strategies and caching providers. Note that everything is referred to by name. The specific repository configuration (e.g. Ef5RepositoryConfiguration and InMemoryRepositoryConfiguration) always takes a name first, then anything specific to that type (like the connection string name for EF5), and optionally a caching strategy and provider can be provided last. If no caching strategy or provider is used, the DefaultCachingStrategy and DefaultCachingProvider are used, and if those aren’t set then no caching is used by default.
This does provide the benefit of being able to setup your configuration in a single location and then pass that configuration in when loading up your repository. You can then make changes in a single location and it will effect everywhere that you create repositories. That being said, there may be some cases where you would need programmatic access to the configuration, but most likely this is not what you should be doing. Well, then why did you show me this? Good question. It leads me into the next way of doing things … Web.config (or App.config).
CONFIGURATION FILE The same basic structure as the configuration object can be used in your Web.config (or App.config). Note: if you install the NuGet packages it will stub out these sections for you. For example, if you install SharpRepository.Ef5Repository it will include the proper configSection at the top of the file and create the specific repository using the DefaultConnection connectionstring name. You can then go in and edit this, but it allows you to not have to code out the assembly type paths, which are kind of a pain.
Here is an example of what the configuration used above would look like in your configuration file:
{
"sharpRepository": {
"repositories": {
"default": "inMemoryNoCaching",
"inMemory": {
"factory": "SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository"
},
"efCoreRepos": {
"factory": "SharpRepository.EfCoreRepository.EfCoreConfigRepositoryFactory, SharpRepository.EfCoreRepository",
"connectionString": "EfCoreConnectionString",
"dbContextType": "SharpRepository.Tests.TestObjects.TestObjectContext, SharpRepository.Tests",
"cachingStrategy": "timeout"
},
"inMemoryNoCaching": {
"factory": "SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository",
"cachingStrategy": "none",
"cachingProvider": "noCachingProvider"
}
},
"cachingProviders": {
"default": "inMemoryProvider",
"inMemoryProvider": {
"factory": "SharpRepository.Repository.Caching.InMemoryConfigCachingProviderFactory, SharpRepository.Repository"
},
"noCachingProvider": {
"factory": "SharpRepository.Repository.Caching.NoCachingConfigCachingProviderFactory, SharpRepository.Repository"
}
},
"cachingStrategies": {
"default": "standard",
"standard": {
"factory": "SharpRepository.Repository.Caching.StandardConfigCachingStrategyFactory, SharpRepository.Repository",
"generational": "true",
"writeThrough": "false"
},
"timeout": {
"factory": "SharpRepository.Repository.Caching.TimeoutConfigCachingStrategyFactory, SharpRepository.Repository",
"timeout": "200"
},
"none": {
"factory": "SharpRepository.Repository.Caching.NoCachingConfigCachingStrategyFactory, SharpRepository.Repository"
}
}
},
"sharpRepository2": {
"repositories": {
"efCore": {
"factory": "SharpRepository.EfCoreRepository.EfCoreConfigRepositoryFactory, SharpRepository.EfCoreRepository",
"connectionString": "EfCoreConnectionString",
"dbContextType": "SharpRepository.Tests.TestObjects.TestObjectContext, SharpRepository.Tests"
},
"inMem": {
"factory": "SharpRepository.InMemoryRepository.InMemoryConfigRepositoryFactory, SharpRepository.InMemoryRepository"
}
}
}
}
Then in your C# code, you would load them up with the similar calls to RepositoryFactory.GetInstance like so:
var userRepos = RepositoryFactory.GetInstance<User, int>(); // uses the Ef5Repository since it's the default
var orderRepos = RepositoryFactory.GetInstance<Order, int>("inmemory"); // uses InMemoryRepository with Standard caching strategy and memcached
This is a much better approach than using the configuration objects because if you need to make a change, like changing your caching or changing from an InMemoryRepository during your prototyping phase to an Ef5Repository, you can just edit your config file and there is no need for a recompile.
Check out this blog post to view more information on how to configure your repository and the available options you have.
http://www.fairwaytech.com/2013/02/sharprepository-configuration/