Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions ServiceScope/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down
9 changes: 1 addition & 8 deletions ServiceScope/Program.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion ServiceScope/Services/ScopedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface IScopedService : IService { }

public class ScopedService : IScopedService
{
private string _guid;
private readonly string _guid;

public ScopedService()
{
Expand Down
2 changes: 1 addition & 1 deletion ServiceScope/Services/SingletonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface ISingletonService : IService { }

public class SingletonService : ISingletonService
{
private string _guid;
private readonly string _guid;

public SingletonService()
{
Expand Down
2 changes: 1 addition & 1 deletion ServiceScope/Services/TransientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface ITransientService : IService { }

public class TransientService : ITransientService
{
private string _guid;
private readonly string _guid;

public TransientService()
{
Expand Down
24 changes: 16 additions & 8 deletions ServiceScope/Views/Home/Scoped.cshtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
@model string

<h1>Guid on page: @Model</h1>
<div class="card">
<div class="card-header">
<h5>1st request to GetGuid() from Scoped service</h5>
</div>
<div class="card-body">
<code>@Model</code>
</div>
</div>

<partial name="_Scoped" />

<p>
Useful for?
</p>

<ul>
<li>Persisting state throughtout application per request.</li>
</ul>
<div class="card">
<div class="card-header">
<h5>When to use Scoped lifetime</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Persisting state through out application per request</li>
</ul>
</div>
24 changes: 24 additions & 0 deletions ServiceScope/Views/Home/Singleton.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@model string

<div class="card">
<div class="card-header">
<h5>1st request to GetGuid() from Singleton service</h5>
</div>
<div class="card-body">
<code>@Model</code>
</div>
</div>

<partial name="_Singleton" />

<div class="card">
<div class="card-header">
<h5>When to use Singleton lifetime</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Caching services</li>
<li class="list-group-item">Global configuration</li>
<li class="list-group-item">HttpClients <a target="_" href="https://medium.com/@@nuno.caneco/c-httpclient-should-not-be-disposed-or-should-it-45d2a8f568bc">(reason why)</a></li>
<li class="list-group-item">Persisting state that's useful for the runtime of the application</li>
</ul>
</div>
29 changes: 19 additions & 10 deletions ServiceScope/Views/Home/Transient.cshtml
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
@model string

<h1>Guid on page: @Model</h1>
<div class="card">
<div class="card-header">
<h5>1st request to GetGuid() from Transient service</h5>
</div>
<div class="card-body">
<code>@Model</code>
</div>
</div>

<partial name="_Transient" />
<p>
Useful for?
</p>

<ul>
<li>Database Access</li>
<li>File Access</li>
<li>Services that should dispose of their state</li>
<li>When you need a fresh instance of an object every single time</li>
</ul>
<div class="card">
<div class="card-header">
<h5>When to use Transient lifetime</h5>
</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">Database connections</li>
<li class="list-group-item">File handles</li>
<li class="list-group-item">Services that should dispose of their state</li>
<li class="list-group-item">When you need a fresh instance of an object each time</li>
</ul>
</div>
28 changes: 16 additions & 12 deletions ServiceScope/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<!DOCTYPE html>

<html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>

<body>
<div>
<a asp-controller="Home" asp-action="Index">Singleton</a>
<a asp-controller="Home" asp-action="Scoped">Scoped</a>
<a asp-controller="Home" asp-action="Transient">Transient</a>
</div>
<div>
@RenderBody()
</div>
<div class="container">

<div class="btn-group">
<a asp-controller="Home" asp-action="Singleton" class="btn btn-primary">Singleton</a>
<a asp-controller="Home" asp-action="Scoped" class="btn btn-primary">Scoped</a>
<a asp-controller="Home" asp-action="Transient" class="btn btn-primary">Transient</a>
</div>

@RenderBody()
</div>
</body>
</html>
</html>
9 changes: 8 additions & 1 deletion ServiceScope/Views/Shared/_Scoped.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
@inject IScopedService Scoped

<h2>From pratial: @Scoped.GetGuid()</h2>
<div class="card">
<div class="card-header">
<h5>2nd request to GetGuid() from Scoped service</h5>
</div>
<div class="card-body">
<code>@Scoped.GetGuid()</code>
</div>
</div>
9 changes: 8 additions & 1 deletion ServiceScope/Views/Shared/_Singleton.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
@inject ISingletonService Singleton

<h2>From pratial: @Singleton.GetGuid()</h2>
<div class="card">
<div class="card-header">
<h5>2nd request to GetGuid() from Singleton service</h5>
</div>
<div class="card-body">
<code>@Singleton.GetGuid()</code>
</div>
</div>
9 changes: 8 additions & 1 deletion ServiceScope/Views/Shared/_Transient.cshtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
@inject ITransientService Transient

<h2>From pratial: @Transient.GetGuid()</h2>
<div class="card">
<div class="card-header">
<h5>2nd request to GetGuid() from Transient service</h5>
</div>
<div class="card-body">
<code>@Transient.GetGuid()</code>
</div>
</div>