diff --git a/BlazorApp.sln.DotSettings.user b/BlazorApp.sln.DotSettings.user index 6b0943b..4d00110 100644 --- a/BlazorApp.sln.DotSettings.user +++ b/BlazorApp.sln.DotSettings.user @@ -1,4 +1,5 @@  + ForceIncluded ForceIncluded ForceIncluded ForceIncluded diff --git a/BlazorApp/Components/Layout/NavMenu.razor b/BlazorApp/Components/Layout/NavMenu.razor index b8b9b9d..28cbe9e 100644 --- a/BlazorApp/Components/Layout/NavMenu.razor +++ b/BlazorApp/Components/Layout/NavMenu.razor @@ -37,5 +37,12 @@ Validation + + + \ No newline at end of file diff --git a/BlazorApp/Components/Pages/BlogPage/Blog.razor b/BlazorApp/Components/Pages/BlogPage/Blog.razor new file mode 100644 index 0000000..28b5f78 --- /dev/null +++ b/BlazorApp/Components/Pages/BlogPage/Blog.razor @@ -0,0 +1,56 @@ +@page "/blog" +@rendermode InteractiveServer + +

Blog

+ +@* +Dependency Injection (DI)를 사용하는 블로그 앱 + + 목표: Blazor의 DI 패턴을 이해하고 데이터 관리 + + 포인트 + • @inject 사용 + • 서비스 라이프사이클 관리 (싱글턴, 트랜지언트, 스코프드) + + 설명: 블로그 포스트를 추가, 삭제, 수정할 수 있는 간단한 블로그 앱을 만들어보세요. 데이터를 관리하는 서비스 클래스를 DI로 주입해 데이터를 처리합니다. + + *@ + +@if (BlogService.Instance.ReadPosts() == null || BlogService.Instance.ReadPosts().Count == 0) +{ +

No posts available.

+} +else +{ + +} +
+ +@* *@ +

@(isEdit ? "Edit Post" : "Add New Post")

+@* *@ + +
+ + +
+@* *@ +
+ + +
+@* *@ + + diff --git a/BlazorApp/Components/Pages/BlogPage/Blog.razor.cs b/BlazorApp/Components/Pages/BlogPage/Blog.razor.cs new file mode 100644 index 0000000..f179a6f --- /dev/null +++ b/BlazorApp/Components/Pages/BlogPage/Blog.razor.cs @@ -0,0 +1,102 @@ +using BlazorApp.PrefixCSharp; +using Microsoft.AspNetCore.Components; + + +namespace BlazorApp.Components.Pages.BlogPage; + +public class PostItem +{ + public int Id { get; set; } + public string Title { get; set; } + public string Content { get; set; } +} + + +public partial class Blog : ComponentBase +{ + // BlogService BS = BlogService.Instance; + + // private PostItem post = new PostItem(); + private string title = string.Empty; + private string content = string.Empty; + private int id = 0; + private bool isEdit = false; + + public void SavePost() + { + if (isEdit) + { + Console.WriteLine(this.id); + EditPost(this.id); + } + else + { + PostItem post = new PostItem + { + Title = this.title, + Content = this.content + }; + CreatePost(post); + } + + + title = string.Empty; + content = string.Empty; + } + public void CreatePost(PostItem post) + { + BlogService.Instance.CreatePost(post); + BlogService.Instance.ReadPosts(); + } + + public void RemovePost(int id) + { + BlogService.Instance.DeletePost(id); + } + + public void EditPost(int id) + { + isEdit = Prefix.Toggle(isEdit); + if (isEdit) + { + PostItem post = BlogService.Instance.ReadPost(id); + title = post.Title; + content = post.Content; + this.id = post.Id; + } + else + { + BlogService.Instance.UpdatePost(BlogService.Instance.ReadPost(id),this.title, this.content); + } + } + +} + + +public class Singleton +{ + // Lazy 선언으로 사용시에만 객체를 생성하게 하면서 외부에서 접근할 수 없는 단일 인스턴스를 저장한다. + private static Lazy _instance = new Lazy(() => new Singleton()); + + // 외부에서 해당 인스턴스에 접근하려면 이 속성을 통해서만 접근이 가능 + public static Singleton Instance => _instance.Value; + + private int value = 0; + + // 싱글턴이니까 외부에서 인스턴스 생성 불가하게 + private Singleton() + { + } + + public int DoSomething(int value) + { + Console.WriteLine("DO SINGLETON"); + this.value++; + return this.value + value; + } + + public void checkValue() + { + Console.WriteLine($"CHECK VALUE:{this.value}"); + } +} \ No newline at end of file diff --git a/BlazorApp/Components/Pages/BlogPage/Blog.razor.css b/BlazorApp/Components/Pages/BlogPage/Blog.razor.css new file mode 100644 index 0000000..e0c5d1e --- /dev/null +++ b/BlazorApp/Components/Pages/BlogPage/Blog.razor.css @@ -0,0 +1,14 @@ +.form-group { + display: flex; + flex-direction: row; + align-items: center; + margin-bottom: 10px; +} + + +.form-group label { + width: 100px; + text-align: right; + margin-right: 10px; + padding-right: 4px; +} \ No newline at end of file diff --git a/BlazorApp/Components/Pages/BlogPage/BlogService.cs b/BlazorApp/Components/Pages/BlogPage/BlogService.cs new file mode 100644 index 0000000..35b9c58 --- /dev/null +++ b/BlazorApp/Components/Pages/BlogPage/BlogService.cs @@ -0,0 +1,54 @@ +namespace BlazorApp.Components.Pages.BlogPage; + +public class BlogService +{ + private static Lazy _instance = new Lazy(() => new BlogService()); + + public static BlogService Instance = _instance.Value; + + private List posts = new List(); + private BlogService() { + + } + + public void CreatePost(PostItem post) + { + if(posts.Count > 0) + post.Id = posts[^1].Id + 1; + else + post.Id = 1; + + posts.Add(post); + } + + public List ReadPosts() + { + foreach (var post in posts) + Console.WriteLine($"READ {post.Id} : {post.Title} = {post.Content}"); + return this.posts; + } + + public PostItem ReadPost(int id) + { + foreach (var post in posts) + { + if (post.Id == id) + return post; + } + return null; + } + + public void UpdatePost(PostItem post,string title, string content) + { + post.Title = title; + post.Content = content; + } + + + public void DeletePost(int id) + { + PostItem post = ReadPost(id); + posts.Remove(post); + } + +} \ No newline at end of file diff --git a/BlazorApp/Components/Pages/TodoPage/Todo.razor.cs b/BlazorApp/Components/Pages/TodoPage/Todo.razor.cs index fe476e9..c267c93 100644 --- a/BlazorApp/Components/Pages/TodoPage/Todo.razor.cs +++ b/BlazorApp/Components/Pages/TodoPage/Todo.razor.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components; namespace BlazorApp.Components.Pages.TodoPage { + public partial class Todo : ComponentBase { private List todos = new(); @@ -37,6 +38,7 @@ namespace BlazorApp.Components.Pages.TodoPage private void ToggleTodo(TodoItem todo, int index) { + if (todo.IsDone) { todo.SuccessDate = string.Empty; diff --git a/BlazorApp/Components/Pages/ValidationPage/Validation.razor.css b/BlazorApp/Components/Pages/ValidationPage/Validation.razor.css index 139b01e..65b08d7 100644 --- a/BlazorApp/Components/Pages/ValidationPage/Validation.razor.css +++ b/BlazorApp/Components/Pages/ValidationPage/Validation.razor.css @@ -6,7 +6,6 @@ margin-bottom: 10px; } - .form-group label { width: 100px; text-align: right; @@ -21,8 +20,10 @@ .form-actions { display: flex; - justify-content: center; + flex-grow: 1; + /*justify-content: flex-end;*/ margin-top: 10px; + width: 100%; } .success-message { diff --git a/BlazorApp/bin/Debug/net8.0/BlazorApp.dll b/BlazorApp/bin/Debug/net8.0/BlazorApp.dll index 7510867..f6ac795 100644 Binary files a/BlazorApp/bin/Debug/net8.0/BlazorApp.dll and b/BlazorApp/bin/Debug/net8.0/BlazorApp.dll differ diff --git a/BlazorApp/bin/Debug/net8.0/BlazorApp.pdb b/BlazorApp/bin/Debug/net8.0/BlazorApp.pdb index 9529a3a..d6f4ce5 100644 Binary files a/BlazorApp/bin/Debug/net8.0/BlazorApp.pdb and b/BlazorApp/bin/Debug/net8.0/BlazorApp.pdb differ diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfo.cs b/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfo.cs index 98e8eac..fbde20a 100644 --- a/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfo.cs +++ b/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("BlazorApp")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] -[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4781a17b00b8e21414f02e947102acf72c5c6178")] +[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+22a9ed453132bd81e3b047b70a2c7c8fecf41cf5")] [assembly: System.Reflection.AssemblyProductAttribute("BlazorApp")] [assembly: System.Reflection.AssemblyTitleAttribute("BlazorApp")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfoInputs.cache b/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfoInputs.cache index f3f48c1..823660a 100644 --- a/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfoInputs.cache +++ b/BlazorApp/obj/Debug/net8.0/BlazorApp.AssemblyInfoInputs.cache @@ -1 +1 @@ -d6262563cb144b1b24689bcb852f66331e78b742afea34cc7526f77806d8fefb +0de9e1463f1e76d9de8902e22e93a1f278b71d472a502e3acfe4ed33a0f2e63e diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.GeneratedMSBuildEditorConfig.editorconfig b/BlazorApp/obj/Debug/net8.0/BlazorApp.GeneratedMSBuildEditorConfig.editorconfig index c779905..58e82fa 100644 --- a/BlazorApp/obj/Debug/net8.0/BlazorApp.GeneratedMSBuildEditorConfig.editorconfig +++ b/BlazorApp/obj/Debug/net8.0/BlazorApp.GeneratedMSBuildEditorConfig.editorconfig @@ -54,6 +54,10 @@ build_metadata.AdditionalFiles.CssScope = b-fekawvbbds build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50cy9MYXlvdXQvTmF2TWVudS5yYXpvcg== build_metadata.AdditionalFiles.CssScope = b-zswk0q6kaa +[/Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/Components/Pages/BlogPage/Blog.razor] +build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50cy9QYWdlcy9CbG9nUGFnZS9CbG9nLnJhem9y +build_metadata.AdditionalFiles.CssScope = b-yzv2llfl6l + [/Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/Components/Pages/TodoPage/Todo.razor] build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50cy9QYWdlcy9Ub2RvUGFnZS9Ub2RvLnJhem9y build_metadata.AdditionalFiles.CssScope = b-n64y2ur3f8 diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.CoreCompileInputs.cache b/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.CoreCompileInputs.cache index eec5051..2dc8279 100644 --- a/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.CoreCompileInputs.cache +++ b/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -50edfd86d0e1da31fa31883ae82e1f3520bbce7f4aabb52fd74698655be3fe0f +ef301e20d32eb1a6f2108ec68675e157089016ae7ab00873f44aef4cee05680c diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.FileListAbsolute.txt b/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.FileListAbsolute.txt index b488e7c..eeed635 100644 --- a/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.FileListAbsolute.txt +++ b/BlazorApp/obj/Debug/net8.0/BlazorApp.csproj.FileListAbsolute.txt @@ -33,3 +33,4 @@ /Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/obj/Debug/net8.0/ref/BlazorApp.dll /Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/TodoPage/Todo.razor.rz.scp.css /Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/ValidationPage/Validation.razor.rz.scp.css +/Users/seankim/1.Program/Project(ASP)/BlazorApp/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/BlogPage/Blog.razor.rz.scp.css diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.dll b/BlazorApp/obj/Debug/net8.0/BlazorApp.dll index 7510867..f6ac795 100644 Binary files a/BlazorApp/obj/Debug/net8.0/BlazorApp.dll and b/BlazorApp/obj/Debug/net8.0/BlazorApp.dll differ diff --git a/BlazorApp/obj/Debug/net8.0/BlazorApp.pdb b/BlazorApp/obj/Debug/net8.0/BlazorApp.pdb index 9529a3a..d6f4ce5 100644 Binary files a/BlazorApp/obj/Debug/net8.0/BlazorApp.pdb and b/BlazorApp/obj/Debug/net8.0/BlazorApp.pdb differ diff --git a/BlazorApp/obj/Debug/net8.0/ref/BlazorApp.dll b/BlazorApp/obj/Debug/net8.0/ref/BlazorApp.dll index ba887f5..48d8abd 100644 Binary files a/BlazorApp/obj/Debug/net8.0/ref/BlazorApp.dll and b/BlazorApp/obj/Debug/net8.0/ref/BlazorApp.dll differ diff --git a/BlazorApp/obj/Debug/net8.0/refint/BlazorApp.dll b/BlazorApp/obj/Debug/net8.0/refint/BlazorApp.dll index ba887f5..48d8abd 100644 Binary files a/BlazorApp/obj/Debug/net8.0/refint/BlazorApp.dll and b/BlazorApp/obj/Debug/net8.0/refint/BlazorApp.dll differ diff --git a/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/BlogPage/Blog.razor.rz.scp.css b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/BlogPage/Blog.razor.rz.scp.css new file mode 100644 index 0000000..048cb55 --- /dev/null +++ b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/BlogPage/Blog.razor.rz.scp.css @@ -0,0 +1,14 @@ +.form-group[b-yzv2llfl6l] { + display: flex; + flex-direction: row; + align-items: center; + margin-bottom: 10px; +} + + +.form-group label[b-yzv2llfl6l] { + width: 100px; + text-align: right; + margin-right: 10px; + padding-right: 4px; +} \ No newline at end of file diff --git a/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/TodoPage/Todo.razor.rz.scp.css b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/TodoPage/Todo.razor.rz.scp.css index 73344f8..d593765 100644 --- a/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/TodoPage/Todo.razor.rz.scp.css +++ b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/TodoPage/Todo.razor.rz.scp.css @@ -1,5 +1,4 @@ -/* todo 용 CSS */ .list-item[b-n64y2ur3f8] { display: flex; align-items: center; diff --git a/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/ValidationPage/Validation.razor.rz.scp.css b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/ValidationPage/Validation.razor.rz.scp.css index 307d81a..94b5f88 100644 --- a/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/ValidationPage/Validation.razor.rz.scp.css +++ b/BlazorApp/obj/Debug/net8.0/scopedcss/Components/Pages/ValidationPage/Validation.razor.rz.scp.css @@ -6,7 +6,6 @@ margin-bottom: 10px; } - .form-group label[b-z8q1q1jnjv] { width: 100px; text-align: right; @@ -21,8 +20,10 @@ .form-actions[b-z8q1q1jnjv] { display: flex; - justify-content: center; + flex-grow: 1; + /*justify-content: flex-end;*/ margin-top: 10px; + width: 100%; } .success-message[b-z8q1q1jnjv] { diff --git a/BlazorApp/obj/Debug/net8.0/scopedcss/bundle/BlazorApp.styles.css b/BlazorApp/obj/Debug/net8.0/scopedcss/bundle/BlazorApp.styles.css index 1538da2..f2ecf36 100644 --- a/BlazorApp/obj/Debug/net8.0/scopedcss/bundle/BlazorApp.styles.css +++ b/BlazorApp/obj/Debug/net8.0/scopedcss/bundle/BlazorApp.styles.css @@ -206,6 +206,21 @@ main[b-fekawvbbds] { overflow-y: auto; } } +/* _content/BlazorApp/Components/Pages/BlogPage/Blog.razor.rz.scp.css */ +.form-group[b-yzv2llfl6l] { + display: flex; + flex-direction: row; + align-items: center; + margin-bottom: 10px; +} + + +.form-group label[b-yzv2llfl6l] { + width: 100px; + text-align: right; + margin-right: 10px; + padding-right: 4px; +} /* _content/BlazorApp/Components/Pages/TodoPage/Todo.razor.rz.scp.css */ .list-item[b-n64y2ur3f8] { @@ -266,7 +281,6 @@ main[b-fekawvbbds] { margin-bottom: 10px; } - .form-group label[b-z8q1q1jnjv] { width: 100px; text-align: right; @@ -281,8 +295,10 @@ main[b-fekawvbbds] { .form-actions[b-z8q1q1jnjv] { display: flex; - justify-content: center; + flex-grow: 1; + /*justify-content: flex-end;*/ margin-top: 10px; + width: 100%; } .success-message[b-z8q1q1jnjv] { diff --git a/BlazorApp/obj/Debug/net8.0/scopedcss/projectbundle/BlazorApp.bundle.scp.css b/BlazorApp/obj/Debug/net8.0/scopedcss/projectbundle/BlazorApp.bundle.scp.css index 1538da2..f2ecf36 100644 --- a/BlazorApp/obj/Debug/net8.0/scopedcss/projectbundle/BlazorApp.bundle.scp.css +++ b/BlazorApp/obj/Debug/net8.0/scopedcss/projectbundle/BlazorApp.bundle.scp.css @@ -206,6 +206,21 @@ main[b-fekawvbbds] { overflow-y: auto; } } +/* _content/BlazorApp/Components/Pages/BlogPage/Blog.razor.rz.scp.css */ +.form-group[b-yzv2llfl6l] { + display: flex; + flex-direction: row; + align-items: center; + margin-bottom: 10px; +} + + +.form-group label[b-yzv2llfl6l] { + width: 100px; + text-align: right; + margin-right: 10px; + padding-right: 4px; +} /* _content/BlazorApp/Components/Pages/TodoPage/Todo.razor.rz.scp.css */ .list-item[b-n64y2ur3f8] { @@ -266,7 +281,6 @@ main[b-fekawvbbds] { margin-bottom: 10px; } - .form-group label[b-z8q1q1jnjv] { width: 100px; text-align: right; @@ -281,8 +295,10 @@ main[b-fekawvbbds] { .form-actions[b-z8q1q1jnjv] { display: flex; - justify-content: center; + flex-grow: 1; + /*justify-content: flex-end;*/ margin-top: 10px; + width: 100%; } .success-message[b-z8q1q1jnjv] { diff --git a/BlazorApp/ㅎNETCORE 학습/공부 방향.md b/BlazorApp/ㅎNETCORE 학습/공부 방향.md index 1edda02..a2df9bc 100644 --- a/BlazorApp/ㅎNETCORE 학습/공부 방향.md +++ b/BlazorApp/ㅎNETCORE 학습/공부 방향.md @@ -190,4 +190,8 @@ WebAssembly의 개념을 이해하고, Blazor를 사용하여 클라이언트 ## **결론** -이미 다양한 개발 경험과 기본기를 갖추고 계신 만큼, .NET Core와 Blazor, EF6를 체계적으로 학습하신다면 빠르게 실력을 향상시킬 수 있을 것입니다. 제안드린 단계별 학습 방법을 따라가며, 실습과 프로젝트를 통해 이론을 실제로 적용해보세요. 학습 과정에서 궁금한 점이나 어려운 부분이 생기면 언제든지 질문해 주세요. 성공적인 학습과 개발 여정을 응원합니다! 화이팅하세요! \ No newline at end of file +이미 다양한 개발 경험과 기본기를 갖추고 계신 만큼, .NET Core와 Blazor, EF6를 체계적으로 학습하신다면 빠르게 실력을 향상시킬 수 있을 것입니다. 제안드린 단계별 학습 방법을 따라가며, 실습과 프로젝트를 통해 이론을 실제로 적용해보세요. 학습 과정에서 궁금한 점이나 어려운 부분이 생기면 언제든지 질문해 주세요. 성공적인 학습과 개발 여정을 응원합니다! 화이팅하세요! + + + +ex, 경로탐색 알고리즘, 충돌회피 알고리즘, 교통 트래픽 분산 알고리즘 트래픽 잼 탈출 알고리즘 등 \ No newline at end of file diff --git a/BlazorApp/ㅎNETCORE 학습/추가적으로 알게 되는 내용.md b/BlazorApp/ㅎNETCORE 학습/추가적으로 알게 되는 내용.md index 06b4668..23f9cac 100644 --- a/BlazorApp/ㅎNETCORE 학습/추가적으로 알게 되는 내용.md +++ b/BlazorApp/ㅎNETCORE 학습/추가적으로 알게 되는 내용.md @@ -23,4 +23,56 @@ - 변수나 클래스, 메서드 위에 [ ]를 사용해 속성을 추가 - 코드에서 어떤 동작이나 정보를 제공하거나, 특정 조건에 따라 처리되는 방식을 지정할 수 있다. - 이미 존재하는(Required, Range, Obsolte, 등) 속성들도 있지만, 사용자가 직접 정의해서 사용할 수 있다. -- 2의 EditForm과 엮으면 좋음 \ No newline at end of file +- 2의 EditForm과 엮으면 좋음 + +## 4. Dependency Injection (DI) +- 의존성 관리를 쉽게 하기 위한 설계 패턴으로 객체가 필요로 하는 의존성을 직접 생성하지 않고,
+ 외부에서 주입해주는 방식으로 객체 간 결합도를 줄이고 테스트 가능성과 유연성을 높이는 역할을 한다. +- 의존성(Dependency)란? + - 간단하게 한 객체가 다른 객체의 기능, 데이터를 필요로 할 때 이를 의존성이라고 한다. + - A 클래스가 B 클래스의 기능을 사용하면 A는 B에 의존한다고 보면 된다. +- 핵심 개념 + - 제어 역전(inversion of Control) : DI도 결국 이 개념의 한 형태로서
+ 전통적으로 객체는 스스로 필요한 의존성을 직접 생성하지만 DI에서는 외부에서 의존성 주입해 객체간 결합도 줄임 + - 느슨한 결합 : DI는 클래스 간 결합도 낮추는 방법으로 객체가 다른 객체의 구체적 구현에 의존하지 않고 인터페이스나 추상 클래스에 의존하게 한다.
+ 그로 인해 시스템의 유지보수성과 확장성을 높인다. + +## 5. 싱글턴 패턴 +- 가장 유명하고 자주 쓰이면서 남발하면 절대 안되는 패턴 +```csharp +public class Singleton +{ + // Lazy 선언으로 사용시에만 객체를 생성하게 하면서 외부에서 접근할 수 없는 단일 인스턴스를 저장한다. + private static Lazy _instance = new Lazy(() => new Singleton()); + + // 외부에서 해당 인스턴스에 접근하려면 이 속성을 통해서만 접근이 가능 + public static Singleton Instance => _instance.Value; + + private int value = 0; + + // 싱글턴이니까 외부에서 인스턴스 생성 불가하게 + private Singleton() + { + // 초기화 + } + + public int DoSomething(int value) + { + // 그냥 아무 동작이나 + Console.WriteLine("DO SINGLETON"); + this.value++; + return this.value + value; + } +} + +public class Base +{ + private void CheckSingle() + { + Singleton ton1 = Singleton.Instance; + Singleton ton2 = Singleton.Instance; + if (ton1 == ton2) + Console.WriteLine("Same"); + } +} +``` \ No newline at end of file