-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
102 lines (90 loc) · 2.31 KB
/
Program.cs
File metadata and controls
102 lines (90 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Author: Nikolay Dvurechensky
* Site: https://dvurechensky.pro/
* Gmail: dvurechenskysoft@gmail.com
* Last Updated: 28 апреля 2026 14:25:05
* Version: 1.0.255
*/
using Microsoft.EntityFrameworkCore;
/* Заместитель
Предоставляет объект-заместитель другого объекта
для контроля доступа к нему
*/
class Program
{
static void Main()
{
#region Пример №1 - базовое
using (IBook book = new BookStoreProxy())
{
//читаем первую страницу
Page page1 = book.GetPage(1);
Console.WriteLine(page1.Text);
//читаем первую страницу
Page page2 = book.GetPage(2);
Console.WriteLine(page1.Text);
//возвращаемся на первую страницу
page1 = book.GetPage(1);
Console.WriteLine(page1.Text);
}
Console.ReadKey();
#endregion
}
}
/// <summary>
/// отдельная страница книги
/// </summary>
/// <param name="Id">Идентификатор</param>
/// <param name="Number">Номер</param>
/// <param name="Text">Содержимое</param>
record Page(int Id, int Number, string Text);
class PageContext : DbContext
{
public DbSet<Page> Pages { get; set; }
}
interface IBook : IDisposable
{
Page GetPage(int number);
}
class BookStore : IBook
{
PageContext db;
public BookStore()
{
db = new PageContext();
}
public void Dispose()
{
db.Dispose();
}
public Page GetPage(int number)
{
return db.Pages.FirstOrDefault(p => p.Number == number);
}
}
class BookStoreProxy : IBook
{
List<Page> Pages;
BookStore bookStore;
public BookStoreProxy()
{
Pages = new List<Page>();
}
public void Dispose()
{
if (bookStore != null)
bookStore.Dispose();
}
public Page GetPage(int number)
{
Page page = Pages.FirstOrDefault(p => p.Number == number);
if(page == null)
{
if(bookStore == null)
bookStore = new BookStore();
page = bookStore.GetPage(number);
Pages.Add(page);
}
return page;
}
}