{"id":21456940,"url":"https://github.com/gregonnet/castle.windsor-playground","last_synced_at":"2025-03-17T03:42:34.603Z","repository":{"id":150472454,"uuid":"41750747","full_name":"GregOnNet/castle.windsor-playground","owner":"GregOnNet","description":"A place where I can test features of Castle.Windsor","archived":false,"fork":false,"pushed_at":"2015-09-01T17:55:11.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-23T13:25:10.571Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GregOnNet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-01T16:37:53.000Z","updated_at":"2015-09-01T16:38:19.000Z","dependencies_parsed_at":"2023-04-06T22:27:10.612Z","dependency_job_id":null,"html_url":"https://github.com/GregOnNet/castle.windsor-playground","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GregOnNet%2Fcastle.windsor-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GregOnNet%2Fcastle.windsor-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GregOnNet%2Fcastle.windsor-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GregOnNet%2Fcastle.windsor-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GregOnNet","download_url":"https://codeload.github.com/GregOnNet/castle.windsor-playground/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243971154,"owners_count":20376784,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T05:17:11.323Z","updated_at":"2025-03-17T03:42:34.569Z","avatar_url":"https://github.com/GregOnNet.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Interceptors with Castle.Windsor\r\n\r\nThis demo contains a simple console application.\r\nThe component `Service` uses a interceptor called `ConsoleLoggingInterceptor` used to setup a simple logging.\r\n\r\nMy goal was to remove the [crosscutting concern](https://msdn.microsoft.com/en-us/library/ee658105.aspx) of logging. Looking for an alternative to [PostSharp](https://www.postsharp.net) I read about [Castle.Windsor](https://github.com/castleproject/Windsor)'s concept of [DynamicProxy](https://github.com/castleproject/Core/blob/master/docs/dynamicproxy.md). \r\n\r\n## Setup\r\n\r\n```cmd\r\n$ .paket/paket.bootstrapper.exe\r\n$ .paket/paket.exe install\r\n$\r\n# After that run the project in Visual Studio\r\n```\r\n\r\n## Benefit\r\n\r\n### Before\r\n\r\nWithout interception I needed to write the code for logging where the logic is defined.\r\n\r\n```csharp\r\npublic class Service : IService\r\n{\r\n  public void Do()\r\n  {\r\n    var stopwatch = new Stopwatch();\r\n    stopwatch.Start();\r\n\r\n    Thread.Sleep(3000);\r\n\r\n    stopwatch.Stop();\r\n    Console.WriteLine(\"Operation finished after {0} ms\", stopwatch.ElapsedMilliseconds);\r\n  }\r\n}\r\n```\r\n\r\n### After\r\n\r\nThe logging mechanism is completly removed form the `Service`...\r\n\r\n```csharp\r\npublic class Service : IService\r\n{\r\n  public void Do()\r\n  {\r\n    Thread.Sleep(3000);\r\n  }\r\n}\r\n```\r\n\r\n... It has been moved to the interceptor.\r\n`invocation.Proceed` calls the intercepted method (In this case `Do()`)...\r\n\r\n```csharp\r\n[Serializable]\r\npublic class ConsoleLoggingInterceptor : IInterceptor\r\n{\r\n  public void Intercept(IInvocation invocation)\r\n  {\r\n    var stopwatch = new Stopwatch();\r\n    stopwatch.Start();\r\n\r\n    invocation.Proceed();\r\n\r\n    stopwatch.Stop();\r\n\r\n    Console.WriteLine(\"Operation finished after {0} ms\", stopwatch.ElapsedMilliseconds);\r\n }\r\n}\r\n```\r\n\r\n... The interceptor can be plugged in when the `Component` is registered in the `WindsorContainer`.\r\n\r\n```csharp\r\ncontainer.Register(\r\n  // Register the interceptor\r\n  Component\r\n    .For\u003cConsoleLoggingInterceptor\u003e(),\r\n  // Register the service\r\n  Component\r\n    .For\u003cIService\u003e()\r\n    .ImplementedBy\u003cService\u003e()\r\n    .Interceptors\u003cConsoleLoggingInterceptor\u003e());\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgregonnet%2Fcastle.windsor-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgregonnet%2Fcastle.windsor-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgregonnet%2Fcastle.windsor-playground/lists"}