If you’ve been following my posts you would know that I love Castle Windsor.
One of the many useful features I have found is the Facility and I’m going to try and give a good example how you can make use of this.
In a recent post I showed how you can add Cross-Cutting concerns to your application by using Interceptors.
Now when configuring the Container you can explicitly configure each Interceptor per Service but when you have lot’s of components it can get pretty hard to maintain after a while and can also introduce subtle issues if someone forgets to configure it correctly.
Below is how you would configure your Container without using a Facility. On the last line we are specifying the Interceptor explicitly.
public void Configure() { container = new WindsorContainer(); container.Register( Component.For<CacheInterceptor>(), Component.For<ICacheProvider>() .ImplementedBy<WebCacheProvider>().LifeStyle.Singleton, Component.For<ICatalogQueryService>() .ImplementedBy<CatalogQueryService>() .LifeStyle.Transient .Interceptors(new InterceptorReference(typeof (CacheInterceptor))).Anywhere); }
Or the XML version
<?xml version="1.0" encoding="utf-8" ?> <castle> <components> <component id="CacheInterceptor" service="MyApp.Aop.CacheInterceptor, MyApp" type="MyApp.Aop.CacheInterceptor, MyApp" lifestyle="transient"> </component> <component id="ICatalogQueryService" service="MyApp.Services.ICatalogQueryService, MyApp" type="MyApp.Services.Impl.CatalogQueryService, MyApp" lifestyle="transient"> <interceptors> <interceptor>${CacheInterceptor}</interceptor> </interceptors> </component> <component id="ICacheProvider" service="MyApp.Core.Cache.ICacheProvider, MyApp" type="MyApp.Core.Cache.Impl.WebCacheProvider, MyApp" lifestyle="transient"> </component> </components> </castle>
What would be nice would be to be able to specify the Interceptor on all Components automagically and Castle allows for this with Facilities. Below is an implementation which achieves the desired result.
public class CacheInterceptionFacility : AbstractFacility { protected override void Init() { Kernel.ComponentRegistered += Kernel_ComponentRegistered; } private void Kernel_ComponentRegistered(string key, IHandler handler) { if (typeof(IMustBeCached).IsAssignableFrom(handler.ComponentModel.Implementation)) { handler.ComponentModel.Interceptors .Add(new InterceptorReference(typeof(CacheInterceptor))); } } }
All you have to do is Inherit from the Castle.MicroKernel.Facilities.AbstractFacility and then override the Init.
You’ll see that we use the Interface IMustBeCached to identify the Component to apply the Interceptor too.
Now in your Bootstrapper all you have to do is call AddFacility on the WindsorContainer and you’re good to go.
public void Configure() { container = new WindsorContainer(); container.Register( Component.For<CacheInterceptor>(), Component.For<ICacheProvider>() .ImplementedBy<WebCacheProvider>().LifeStyle.Singleton, Component.For<ICatalogQueryService>() .ImplementedBy<CatalogQueryService>().LifeStyle.Transient); container.AddFacility("CacheInterceptionFacility", new CacheInterceptionFacility()); }
Or the XML version
<?xml version="1.0" encoding="utf-8" ?> <castle> <facilities> <facility id="CacheInterceptionFacility" type="MyApp.Aop.CacheInterceptionFacility, MyApp"> </facility> </facilities> <components> <component id="CacheInterceptor" service="MyApp.Aop.CacheInterceptor, MyApp" type="MyApp.Aop.CacheInterceptor, MyApp" lifestyle="transient"> </component> <component id="ICatalogQueryService" service="MyApp.Services.ICatalogQueryService, MyApp" type="MyApp.Services.Impl.CatalogQueryService, MyApp" lifestyle="transient"> </component> <component id="ICacheProvider" service="MyApp.Core.Cache.ICacheProvider, MyApp" type="MyApp.Core.Cache.Impl.WebCacheProvider, MyApp" lifestyle="transient"> </component> </components> </castle>
Aside from reducing the configuration overhead another benefit of using this approach is that in your Debug or Test Environments you can easily deploy without any Interceptors and only enable them in your Staging and Production environments.
Nice Interview Questions
ReplyDeleteIot Online Training
Spring Interview Questions
Iot Interview Questions
MSBI Interview Questions
Thank you for excellent article.You made an article that is interesting.
DeleteTavera car for rent in coimbatore|Indica car for rent in coimbatore|innova car for rent in coimbatore|mini bus for rent in coimbatore|tempo traveller for rent in coimbatore|kodaikanal tour package from chennai
Keep on the good work and write more article like this...
Great work !!!!Congratulations for this blog
Nice Post. Thanks for the post.
ReplyDeleteSEO Specialist in Bangalore
This is very good blog for learners, Thanks for sharing valuable content on MSBI Online Training
ReplyDeleteReally Good blog post.provided a helpful information.I hope that you will post more updates like this.
ReplyDeleteSelenium Training in HRBR Layout
Selenium Training in Kalyan Nagar
Best Selenium Training Institute in kalyan Nagar Bangalore
I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
ReplyDeleteAWS Training in Bangalore
Really Thanks For Posting Such a Useful Content. Really Thanks For Sharing Such an Informative Post.
ReplyDeleteUSMLE
This Blog Provides Very Useful and Important Information Digital Transformation Consulting Thanks for sharing.
ReplyDeleteNice Post. Thanks for the post.
ReplyDeleteBest Training and Real Time Support
Sap Hybris Training From India
Sap Sd Training From India
Nice ...Thanks for sharing
ReplyDeleteUI UX Design Courses in Chennai
Thank you so much for your information,its very useful and helful to me.Keep updating and sharing. Thank you.
ReplyDeleteRPA training in chennai | UiPath training in chennai
I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
ReplyDeleteWeb Designing Course in chennai
Web Designing training in chennai
Hadoop Training in Chennai
Python Training in Chennai
Web designing Training in Porur
Web designing Training in Adyar
Web designing Training in Tnagar
Hearty thanks to you admin, your blog is awesome and helpful. Keep your blog with latest information.
ReplyDeleteRPA Training in Chennai
Robotics Process Automation Training in Chennai
Blue Prism Training in Chennai
UiPath Training in Chennai
Data Science Course in Chennai
RPA Training in Anna Nagar
RPA Training in Chennai
RPA course in Chennai
very nice post thankyou for sharing.
ReplyDeleteSobha Royal Pavilion Sarjapur Road
The given information was excellent and useful. This is one of the excellent blog, I have come across. Do share more.
ReplyDeleteAzure Training in Chennai
Azure Training center in Chennai
Cloud Computing Courses in Chennai
Cloud Computing Training in Velachery
AWS Training in Chennai
AWS course in Chennai
DevOps Certification in Chennai
Cloud computing Training in Chennai
This blog is very fantastic! Its much more useful information to me and Thank you for your wonderful post. Please posting...
ReplyDeleteExcel Training in Chennai
Advanced Excel Training in Chennai
Unix Training in Chennai
corporate training in chennai
Tableau Training in Chennai
Oracle Training in Chennai
Primavera Training in Chennai
Power BI Training in Chennai
Excel Training in Chennai
Advanced Excel Training in Chennai
You wrote a such a nice blog!! It is very useful for my future reference. All the info you shared with us are truly tremendous.
ReplyDeleteAngularjs Training in Chennai
Angularjs course in Chennai
AWS Training in Chennai
Big Data Analytics Courses in Chennai
Blue Prism Training in Chennai
Cloud Computing Training in Chennai
Data Science Course in Chennai
AngularJS Training in T Nagar
thanks for the information best msbi training institutes in hyderabad
ReplyDeletethanks for sharing this information
ReplyDeletepython training in bangalore
python training in bangalore marathahalli
best python training institute in bangalore
python training institutes in bangalore marathahalli
python training in jayanagar bangalore
python training in btm Layout
python training in btm
Artificial Intelligence training in Bangalore
Artificial Intelligence training in BTM
I do have a habit of reading a lot of blogs and I am really happy that I have read this too. Thanks for sharing.
ReplyDeleteSpoken English Classes in Chennai
Spoken English in Chennai
Top 10 Spoken English Classes in Chennai
Best IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Classes in Mumbai
English Speaking Classes in Mumbai
Best IELTS Coaching in Mumbai
IELTS Coaching in Mumbai
Spoken English Classes in Anna Nagar
Are you currently scratching your head and stuck together with your QuickBooks related issues, you'll be only one click definately not our expert tech support team for your QuickBooks Support Phone Number related issues.
ReplyDeletenice blog
ReplyDeleteaws training center in chennai
aws training in chennai
aws training institute in chennai
aws training in sholinganallur
QuickBooks Support Phone Number (+1 -833-441-8848) for related suggestions and instructions. Due to the revolution it has caused in the business world,visit us:-https://tinyurl.com/y4tcwakq
ReplyDeleteawesome blog it's very nice and useful i got many more information it's really nice i like your blog styleweb design company in velachery
ReplyDeleteGreat Article
ReplyDeleteIEEE Projects on Cloud Computing
Final Year Projects for CSE
Very nice post with lots of information. Thanks for this updates.
ReplyDeleteui ux design course in Chennai
ui ux designer course in Chennai
ui ux course in chennai
ReactJS Training in Chennai
Web Designing Course in chennai
web designing training in chennai
Tally course in Chennai
ui ux design course in Porur
ui ux design course in Tambaram
ui ux design course in Adyar
Washing Powder Pcakaging
ReplyDeleteWashing Powder Pcakaging bags
Detergent packaging pouch
Lyrics with music
we have provide the best fridge repair service.
ReplyDeleteWashing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Bosch Washing Machine Repair In Faridabad
Whirlpool Washing Machine Repair In Faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida
iso certification in noida
ReplyDeleteiso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida
iso 9001 certification in Delhi
iso 27001 certification services
ISO 9001 Certification in Noida
iso 22000 certification in Delhi
website designing services
SEO Service Consultant
ReplyDeleteThis content of information has
helped me a lot. It is very well explained and easy to understand.
seo training classes
seo training course
seo training institute in chennai
seo training institutes
seo courses in chennai
seo institutes in chennai
seo classes in chennai
seo training center in chennai
ReplyDeleteYou write this post very carefully I think, which is easily understand to me. Not only this, other post is also good. As a newbie this info is really helpful for me. Thanks to you.
Tally Training in Chennai
Tally ERP 9 Training
tally classes
Tally Training institute in Chennai
Tally course in Chennai
Your article is very informative. Thanks for sharing the valuable information.
ReplyDeleteData Science Course in Chennai
Data Science Courses in Bangalore
Data Science Course in Marathahalli
Best Data Science Training in Marathahalli
Data Science Institute in Marathahalli
AWS Training in Bangalore
Data Science Training in Bangalore
Data Science Institute in Bangalore
Spoken English Classes in Bangalore
DevOps Training in Bangalore
Your post is very good and unique. I am eagerly waiting for your new post.
ReplyDeletestring operations in java
string handling functions in java
string manipulation in java
how to format date in java
aws in cloud computing
software testing interview questions and answers pdf