GeezerVison is showing evolutionarydeveloper.blogspot.com — ServiceStack.Redis and StructureMap
Tuesday, 8 April 2014
ServiceStack.Redis and StructureMap
I've tripped up on this a couple of times so I've just decided to layout the right way to make use of the ServiceStack.Redis client library when using StructureMap DI Container. The mistake I used to make was to have my controller or service depend upon an IRedisClient. StructureMap would be configured to provide an instance of an IRedisClient by invoking GetClient() on the whatever singleton instance of IRedisClientsManager I was using, usually the PooledRedisClientsManager. Much was learned from this SO answer.
This was a problem because it limited the re-usability of the consuming controller/service. Such a service might provide a method that used the injected IRedisClient to interact with the Redis instance. The IRedisClient implements IDisposable and should be wrapped in a using statement in order to release the resource and close the channel when it is has completed the task. In doing so, you make the method on the service non-idempotent: it has the side-effect of changing the state of the service by disposing of its injected IRedisClient instance. You are only able to successfully call the service method once, calling it a second time throws an exception.
The solution is fairly simple. Instead of having the service depend directly on the IRedisClient, the service instead needs to depend on the IRedisClientsManager. This then allows the service to call the GetClient() method on the IRedisClientsManager itself and create an instance of IRedisClient every time the method is called. Idempotency is achieved because a client of the service can call the method any number of times and not cause any unintended side-effects. The state of the service is no longer affected by disposing of its only instance of IRedisClient. The IRedisClient instances that are created are still disposed of, but the service has the ability to create a new one for itself.
The pattern is very similar to the RavenDB IDocumentStore and IDocumentSession that I am familiar with, so I surprised it took me so long to figure out!
41 comments:
This comment has been removed by the author. ReplyDeleteWEB DesigningServices in Dehradun I explore your all post and found some great information which is definitely useful for me in the future. herefor all students on here we have an all assignment service to help you guys to complete their assignment.
ReplyDeleteyou have executed an uproarious undertaking upon this text. Its completely change and very subjective. you have even figured out how to make it discernible and simple to make a get accord of into. you have a couple of definite composing dexterity. much appreciated likewise a lot. Activation Key For Movavi ReplyDeleteI go to your blog every now and again and counsel it to the total of people who wanted to highlight on happening their comprehension resulting to ease. The way of composing is perfect and in addition to the substance material is highest point score. gratitude for that insight you give the perusers! Stellar Data Recovery Software With Crack ReplyDeleteThis comment has been removed by the author. ReplyDeleteReally appreciate the note on scope: {} creating an isolate scope and preventing accidental data leaks between directives. That kind of encapsulation thinking is also crucial when working with large datasets and writing clean analysis code. For developers in Electronic City wanting to pivot into data roles, I’ve been researching data analytics with python training electronic city because Python is great for both back-end logic and data processing. Has anyone here moved from AngularJS development to a data engineering role? What helped the most?
ReplyDeleteThis comment has been removed by the author. ReplyDeleteThe explanation of identity management and network security and compliance really made it clear why AZ-500 Training is so valuable. This article is a resource for people who want to be Azure Security Engineers. It explains things in a way that's easy to understand which is really helpful for people who are interested, in AZ-500 Training and want to learn more about it.
ReplyDeleteI really like this article because it is easy to understand. The person who wrote this article did a job of explaining things in a simple way. I am looking for the Best Power BI Course in Electronic City It is nice to see people making content to help others learn about business intelligence and Power BI. When people work on projects and use real examples they feel more confident about what they are doing with Power BI. I think the Power BI course in Electronic City should have hands-on projects and real world examples. The Power BI course is very useful. I am glad I found this article, about Power BI. ReplyDeleteI really appreciate the clarity of this article. Every section is easy to follow and understand.ba online training ReplyDeleteTableau Training in Mumbai helps learners develop practical skills in data visualization, data analytics, dashboard creation, calculated fields, data blending, filters, charts, and interactive reporting. With industry-focused training and hands-on exercises, participants gain experience connecting multiple data sources, analyzing complex datasets, creating interactive dashboards, and presenting meaningful business insights using Tableau. The course is suitable for students and working professionals looking to strengthen their data analytics expertise, develop job-ready skills, and advance their careers in Mumbai's business intelligence and analytics industry. Tableau Training in Mumbai ReplyDelete
Add comment Load more...
|