From b359c7c4bbbf88a41c12d6adf2a17067e40a5839 Mon Sep 17 00:00:00 2001 From: Imran Chaudhry Date: Tue, 12 Apr 2022 09:49:59 +0100 Subject: [PATCH 1/2] Prettify the output, correct typos --- ServiceScope/Controllers/HomeController.cs | 11 ++++---- ServiceScope/Program.cs | 9 +------ ServiceScope/Services/ScopedService.cs | 2 +- ServiceScope/Services/SingletonService.cs | 2 +- ServiceScope/Services/TransientService.cs | 2 +- ServiceScope/Views/Home/Scoped.cshtml | 24 +++++++++++------ ServiceScope/Views/Home/Singleton.cshtml | 24 +++++++++++++++++ ServiceScope/Views/Home/Transient.cshtml | 29 ++++++++++++++------- ServiceScope/Views/Shared/_Layout.cshtml | 28 +++++++++++--------- ServiceScope/Views/Shared/_Scoped.cshtml | 9 ++++++- ServiceScope/Views/Shared/_Singleton.cshtml | 9 ++++++- ServiceScope/Views/Shared/_Transient.cshtml | 9 ++++++- 12 files changed, 109 insertions(+), 49 deletions(-) create mode 100644 ServiceScope/Views/Home/Singleton.cshtml diff --git a/ServiceScope/Controllers/HomeController.cs b/ServiceScope/Controllers/HomeController.cs index b3b8c29..34c2e85 100644 --- a/ServiceScope/Controllers/HomeController.cs +++ b/ServiceScope/Controllers/HomeController.cs @@ -5,9 +5,9 @@ namespace ServiceScope.Controllers { public class HomeController : Controller { - private ISingletonService _singleton; - private IScopedService _scoped; - private ITransientService _transient; + private readonly ISingletonService _singleton; + private readonly IScopedService _scoped; + private readonly ITransientService _transient; public HomeController( ISingletonService singleton, @@ -19,9 +19,10 @@ public HomeController( _transient = transient; } - public IActionResult Index() + [Route("/")] + public IActionResult Singleton() { - return View("Index", _singleton.GetGuid()); + return View("Singleton", _singleton.GetGuid()); } public IActionResult Scoped() diff --git a/ServiceScope/Program.cs b/ServiceScope/Program.cs index ae7281f..eaaebc3 100644 --- a/ServiceScope/Program.cs +++ b/ServiceScope/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; namespace ServiceScope { diff --git a/ServiceScope/Services/ScopedService.cs b/ServiceScope/Services/ScopedService.cs index 2c8e9b3..1eecc69 100644 --- a/ServiceScope/Services/ScopedService.cs +++ b/ServiceScope/Services/ScopedService.cs @@ -6,7 +6,7 @@ public interface IScopedService : IService { } public class ScopedService : IScopedService { - private string _guid; + private readonly string _guid; public ScopedService() { diff --git a/ServiceScope/Services/SingletonService.cs b/ServiceScope/Services/SingletonService.cs index d444eef..17ce4d6 100644 --- a/ServiceScope/Services/SingletonService.cs +++ b/ServiceScope/Services/SingletonService.cs @@ -6,7 +6,7 @@ public interface ISingletonService : IService { } public class SingletonService : ISingletonService { - private string _guid; + private readonly string _guid; public SingletonService() { diff --git a/ServiceScope/Services/TransientService.cs b/ServiceScope/Services/TransientService.cs index 4a6e392..d2597ba 100644 --- a/ServiceScope/Services/TransientService.cs +++ b/ServiceScope/Services/TransientService.cs @@ -6,7 +6,7 @@ public interface ITransientService : IService { } public class TransientService : ITransientService { - private string _guid; + private readonly string _guid; public TransientService() { diff --git a/ServiceScope/Views/Home/Scoped.cshtml b/ServiceScope/Views/Home/Scoped.cshtml index f868551..b597fc5 100644 --- a/ServiceScope/Views/Home/Scoped.cshtml +++ b/ServiceScope/Views/Home/Scoped.cshtml @@ -1,13 +1,21 @@ @model string -

Guid on page: @Model

+
+
+
1st request to GetGuid() from Scoped service
+
+
+ @Model +
+
-

- Useful for? -

- - +
+
+
When to use Scoped lifetime
+
+
    +
  • Persisting state through out application per request
  • +
+
\ No newline at end of file diff --git a/ServiceScope/Views/Home/Singleton.cshtml b/ServiceScope/Views/Home/Singleton.cshtml new file mode 100644 index 0000000..2309e80 --- /dev/null +++ b/ServiceScope/Views/Home/Singleton.cshtml @@ -0,0 +1,24 @@ +@model string + +
+
+
1st request to GetGuid() from Singleton service
+
+
+ @Model +
+
+ + + +
+
+
When to use Singleton lifetime
+
+
    +
  • Caching services
  • +
  • Global configuration
  • +
  • HttpClients (reason why)
  • +
  • Persisting state that's useful for the runtime of the application
  • +
+
\ No newline at end of file diff --git a/ServiceScope/Views/Home/Transient.cshtml b/ServiceScope/Views/Home/Transient.cshtml index 0336945..0469f6e 100644 --- a/ServiceScope/Views/Home/Transient.cshtml +++ b/ServiceScope/Views/Home/Transient.cshtml @@ -1,15 +1,24 @@ @model string -

Guid on page: @Model

+
+
+
1st request to GetGuid() from Transient service
+
+
+ @Model +
+
-

- Useful for? -

-
    -
  • Database Access
  • -
  • File Access
  • -
  • Services that should dispose of their state
  • -
  • When you need a fresh instance of an object every single time
  • -
+
+
+
When to use Transient lifetime
+
+
    +
  • Database connections
  • +
  • File handles
  • +
  • Services that should dispose of their state
  • +
  • When you need a fresh instance of an object every request
  • +
+
\ No newline at end of file diff --git a/ServiceScope/Views/Shared/_Layout.cshtml b/ServiceScope/Views/Shared/_Layout.cshtml index ab9a575..db5b220 100644 --- a/ServiceScope/Views/Shared/_Layout.cshtml +++ b/ServiceScope/Views/Shared/_Layout.cshtml @@ -1,18 +1,22 @@  - + - - @ViewBag.Title + + @ViewBag.Title + + -
- Singleton - Scoped - Transient -
-
- @RenderBody() -
+
+ + + + @RenderBody() +
- + \ No newline at end of file diff --git a/ServiceScope/Views/Shared/_Scoped.cshtml b/ServiceScope/Views/Shared/_Scoped.cshtml index eb6766b..c583868 100644 --- a/ServiceScope/Views/Shared/_Scoped.cshtml +++ b/ServiceScope/Views/Shared/_Scoped.cshtml @@ -1,3 +1,10 @@ @inject IScopedService Scoped -

From pratial: @Scoped.GetGuid()

\ No newline at end of file +
+
+
2nd request to GetGuid() from Scoped service
+
+
+ @Scoped.GetGuid() +
+
\ No newline at end of file diff --git a/ServiceScope/Views/Shared/_Singleton.cshtml b/ServiceScope/Views/Shared/_Singleton.cshtml index d91bec8..3ae506e 100644 --- a/ServiceScope/Views/Shared/_Singleton.cshtml +++ b/ServiceScope/Views/Shared/_Singleton.cshtml @@ -1,3 +1,10 @@ @inject ISingletonService Singleton -

From pratial: @Singleton.GetGuid()

\ No newline at end of file +
+
+
2nd request to GetGuid() from Singleton service
+
+
+ @Singleton.GetGuid() +
+
\ No newline at end of file diff --git a/ServiceScope/Views/Shared/_Transient.cshtml b/ServiceScope/Views/Shared/_Transient.cshtml index 3b971eb..df76adc 100644 --- a/ServiceScope/Views/Shared/_Transient.cshtml +++ b/ServiceScope/Views/Shared/_Transient.cshtml @@ -1,3 +1,10 @@ @inject ITransientService Transient -

From pratial: @Transient.GetGuid()

\ No newline at end of file +
+
+
2nd request to GetGuid() from Transient service
+
+
+ @Transient.GetGuid() +
+
\ No newline at end of file From f80c8c1643b6ccc8b9423c8003eab80f3ca8d61e Mon Sep 17 00:00:00 2001 From: Imran Chaudhry Date: Tue, 12 Apr 2022 10:00:47 +0100 Subject: [PATCH 2/2] Slight correction to Transient service usage --- ServiceScope/Views/Home/Transient.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ServiceScope/Views/Home/Transient.cshtml b/ServiceScope/Views/Home/Transient.cshtml index 0469f6e..4e73333 100644 --- a/ServiceScope/Views/Home/Transient.cshtml +++ b/ServiceScope/Views/Home/Transient.cshtml @@ -19,6 +19,6 @@
  • Database connections
  • File handles
  • Services that should dispose of their state
  • -
  • When you need a fresh instance of an object every request
  • +
  • When you need a fresh instance of an object each time
  • \ No newline at end of file