-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
43 lines (35 loc) · 2.01 KB
/
Program.cs
File metadata and controls
43 lines (35 loc) · 2.01 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
using Health;
using Microsoft.Extensions.DependencyInjection;
// Programa principal para testar a calculadora com injeção de dependência via contêiner
class Program
{
static void Main()
{
// Configura o contêiner de injeção de dependência
var serviceCollection = new ServiceCollection();
// Registra IHealthRules com sua implementação concreta HealthRules
// AddScoped: uma instância por escopo (neste caso, por execução no console)
serviceCollection.AddScoped<IHealthRules, HealthRules>();
// Registra a PersonFactory, que será resolvida pelo contêiner
serviceCollection.AddScoped<IPersonFactory, PersonFactory>();
// Constrói o provedor de serviços (contêiner)
var serviceProvider = serviceCollection.BuildServiceProvider();
// Cria um escopo para resolver as dependências
using (var scope = serviceProvider.CreateScope())
{
// Obtém a factory como IPersonFactory do contêiner
var factory = scope.ServiceProvider.GetRequiredService<IPersonFactory>();
// Usa a factory para criar instâncias de Person e Athlete
Person regular = factory.Create(false, 70, 1.75); // Pessoa normal
Person athlete = factory.Create(true, 80, 1.80); // Atleta
// Exibe os resultados
Console.WriteLine($"Pessoa normal, peso {regular.Weight}kg, altura {regular.Height}m: IMC = {regular.IMC:F2}");
Console.WriteLine($"Atleta, peso {athlete.Weight}kg, altura {athlete.Height}m: IMC = {athlete.IMC:F2}");
}
// Comentário: O contêiner gerencia a criação de IHealthRules e PersonFactory.
// Person e Athlete não são registrados diretamente no contêiner porque têm parâmetros
// (weight e height) que variam em tempo de execução, então usamos a factory.
// SOLID - L (Liskov Substitution): Tanto regular quanto athlete são tratados como Person,
// mostrando que a substituição funciona.
}
}