-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
88 lines (73 loc) · 2.33 KB
/
Program.cs
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
using System.Collections.Concurrent;
namespace DesignPatterns.VIPER;
using System;
using System.Collections.Generic;
// Entity (Model)
public record User(int Id, string Name, string Email)
{
public override string ToString() => $"ID: {Id}, Name: {Name}, Email: {Email}";
}
// Interactor (Business Logic)
public interface IUserInteractor
{
void FetchUsers();
User GetUserById(int userId);
}
public class UserInteractor(IUserPresenter presenter, IUserRouter router) : IUserInteractor
{
// Using ConcurrentBag for thread-safe operations
ConcurrentBag<User> _users = new();
public void FetchUsers()
{
// Simulating fetching data from a real data source (DB, API, etc.)
_users.Add(new(1,"Ibrahim", "[email protected]"));
_users.Add(new(2,"Betul", "[email protected]"));
// Pass the fetched data to the presenter
presenter.OnUsersFetched(_users.ToList());
}
public User GetUserById(int userId) => _users.FirstOrDefault(u => u.Id == userId);
}
// Presenter (Bridge between View and Interactor)
public interface IUserPresenter
{
void OnUsersFetched(List<User> users);
}
public class UserPresenter(IUserView view) : IUserPresenter
{
// Send the retrieved data to the view
public void OnUsersFetched(List<User> users) => view.DisplayUsers(users);
}
// View (User Interface)
public interface IUserView
{
void DisplayUsers(List<User> users);
}
public class ConsoleUserView : IUserView
{
public void DisplayUsers(List<User> users) => users.ForEach(Console.WriteLine);
}
// Router (Navigation)
public interface IUserRouter
{
void NavigateToUserDetails(int userId);
}
public class UserRouter : IUserRouter
{
public void NavigateToUserDetails(int userId)
=> Console.WriteLine($"Navigating to User Details for User ID: {userId}");
}
// Main Program to Initialize VIPER Components
class Program
{
static void Main(string[] args)
{
// Creating VIPER components
IUserView view = new ConsoleUserView();
IUserPresenter presenter = new UserPresenter(view);
IUserRouter router = new UserRouter();
IUserInteractor interactor = new UserInteractor(presenter, router);
// Start the process of fetching user data
interactor.FetchUsers();
Console.WriteLine( interactor.GetUserById(2));
}
}