Skip to content

Commit efa5fe8

Browse files
committed
builder
1 parent dc9143d commit efa5fe8

7 files changed

Lines changed: 63 additions & 77 deletions

File tree

src/Disc.NET/App.cs

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,22 @@
11
using Disc.NET.Configuration;
22
using Disc.NET.Gateway;
3-
using Microsoft.Extensions.Logging;
43

54
namespace Disc.NET
65
{
76
public class App
87
{
9-
private ILogger<GatewayConnection> _logger;
10-
public App()
11-
{
12-
_logger = CreateLogger(LogLevel.Information);
13-
}
14-
15-
public async Task RunAsync(AppConfiguration configuration)
16-
{
17-
if (configuration.UseContainer)
18-
{
19-
DiscNetContainer.GetInstance().RegisterDependencies();
20-
}
21-
var gateway = new GatewayConnection(configuration,_logger);
22-
await gateway.ConnectAsync();
23-
}
8+
private readonly AppConfiguration _appConfiguration;
249

25-
public App WithDebugLogger()
10+
public App(AppConfiguration appConfiguration)
2611
{
27-
_logger = CreateLogger(LogLevel.Debug);
28-
return this;
12+
_appConfiguration = appConfiguration;
2913
}
3014

31-
private ILogger<GatewayConnection> CreateLogger(LogLevel level)
15+
public async Task RunAsync()
3216
{
33-
var loggerFactory = LoggerFactory.Create(builder =>
34-
{
35-
builder.AddConsole();
36-
builder.SetMinimumLevel(level);
37-
});
38-
39-
return loggerFactory.CreateLogger<GatewayConnection>();
40-
}
41-
/// <summary>
42-
/// Configures the container usage strategy for the application.
43-
///
44-
/// By default, object creation is performed using the standard C# <see cref="Activator"/>,
45-
/// which does not provide support for dependency injection.
46-
///
47-
/// When this method is called, the application switches to using the Autofac container,
48-
/// enabling full dependency injection support for registered components.
49-
///
50-
/// In summary:
51-
/// - Without calling this method → uses default Activator (no DI)
52-
/// - Calling this method → enables Autofac and dependency injection
53-
/// </summary>
54-
/// <param name="appConfiguration">
55-
/// Application configuration instance where the container selection flag is stored.
56-
/// </param>
57-
/// <returns>
58-
/// Returns the singleton instance of <see cref="DiscNetContainer"/> configured to use Autofac.
59-
/// </returns>
60-
public DiscNetContainer UseDependencyInjection(AppConfiguration appConfiguration)
61-
{
62-
appConfiguration.UseContainer = true;
63-
return DiscNetContainer.GetInstance();
17+
var gateway = new GatewayConnection(_appConfiguration);
18+
await gateway.ConnectAsync();
6419
}
65-
6620
}
6721

6822
}

src/Disc.NET/AppBuilder.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Disc.NET.Configuration;
2+
3+
namespace Disc.NET
4+
{
5+
public class AppBuilder
6+
{
7+
public DiscNetContainer Services { get; set; }
8+
public AppConfiguration? Configuration { get; set; }
9+
10+
public AppBuilder()
11+
{
12+
Services = DiscNetContainer.GetInstance();
13+
}
14+
15+
public AppBuilder AddConfiguration(AppConfiguration configuration)
16+
{
17+
Configuration = configuration;
18+
return this;
19+
}
20+
21+
public App Build()
22+
{
23+
Services.RegisterDependencies();
24+
if (Configuration == null)
25+
{
26+
var token = Environment.GetEnvironmentVariable("GENERIC_BOT_TOKEN");
27+
var applicationId = Environment.GetEnvironmentVariable("GENERIC_BOT_APPLICATION_ID");
28+
return new App(new AppConfiguration(token)
29+
{
30+
ApplicationId = long.Parse(applicationId)
31+
});
32+
}
33+
return new App(Configuration!);
34+
}
35+
}
36+
}

src/Disc.NET/Configuration/AppConfiguration.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public sealed class AppConfiguration
77
public string Token { get; }
88
public char BotPrefix { get; init; }
99
public required long ApplicationId { get; init; }
10-
public bool UseContainer { get; set; }
1110
public List<GatewayIntent> Intents { get; init; } = new()
1211
{
1312
GatewayIntent.GUILD_MESSAGES,
@@ -18,6 +17,5 @@ public AppConfiguration(string token, char botPrefix = '!')
1817
BotPrefix = botPrefix;
1918
Token = token;
2019
}
21-
2220
}
2321
}

src/Disc.NET/Configuration/DiscNetContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void RegisterDependencies()
5050
commandType.ForEach(x => _containerBuilder!.RegisterType(x));
5151
}
5252

53-
public DiscNetContainer WithHttpClient()
53+
public DiscNetContainer AddHttpClient()
5454
{
5555
_containerBuilder!.Register(c => new HttpClient())
5656
.As<HttpClient>();

src/Disc.NET/Gateway/GatewayConnection.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Disc.NET.Shared.Serializer;
66
using Microsoft.Extensions.Logging;
77
using System.Net.WebSockets;
8+
using System.Reflection.Emit;
89
using System.Text.Json;
910
using System.Threading.Channels;
1011

@@ -36,10 +37,15 @@ internal class GatewayConnection
3637
/// Initializes a new instance of the <see cref="GatewayConnection"/> class.
3738
/// </summary>
3839
/// <param name="logger">Logger instance used for structured logging and diagnostics.</param>
39-
public GatewayConnection(AppConfiguration appConfiguration,ILogger<GatewayConnection> logger)
40+
public GatewayConnection(AppConfiguration appConfiguration)
4041
{
4142
_appConfiguration = appConfiguration;
42-
_logger = logger;
43+
var loggerFactory = LoggerFactory.Create(builder =>
44+
{
45+
builder.AddConsole();
46+
builder.SetMinimumLevel(LogLevel.Debug);
47+
});
48+
_logger = loggerFactory.CreateLogger<GatewayConnection>();
4349
_eventDispatcher = new EventDispatcher(_appConfiguration);
4450
}
4551

src/Disc.NET/Handlers/HandlerCommandBase.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Disc.NET.Commands.Responses;
66
using Disc.NET.Configuration;
77
using Disc.NET.Dispatcher;
8-
using Disc.NET.Enums;
98
using Disc.NET.Shared.Constraints;
109
using Disc.NET.Shared.Extensions;
1110
using System.Reflection;
@@ -30,11 +29,8 @@ public HandlerCommandBase(AppConfiguration appConfiguration) : base(appConfigura
3029
var commandType = GetCommandTypeByName<TAttribute, TKContext>(commandName);
3130

3231
if (commandType == null) return null;
33-
if (_appConfiguration.UseContainer)
34-
{
35-
return DiscNetContainer.GetInstance().Resolve(commandType) as ICommand<TKContext>;
36-
}
37-
return Activator.CreateInstance(commandType) as ICommand<TKContext>;
32+
return DiscNetContainer.GetInstance().Resolve(commandType) as ICommand<TKContext>;
33+
//return Activator.CreateInstance(commandType) as ICommand<TKContext>;
3834
}
3935

4036
protected List<T> GetCommandAttributes<T>() where T : Attribute

tests/GenericBot/Program.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@
66
using Disc.NET.Enums;
77

88

9+
var builder = new AppBuilder();
10+
builder.Services.AddHttpClient();
11+
builder.AddConfiguration(new AppConfiguration(Environment.GetEnvironmentVariable("GENERIC_BOT_TOKEN")!)
12+
{
13+
Intents = [GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MESSAGES],
14+
ApplicationId = long.Parse(Environment.GetEnvironmentVariable("GENERIC_BOT_APPLICATION_ID")!),
15+
BotPrefix = '?'
16+
});
917

10-
App app = new App().WithDebugLogger();
11-
var token = Environment.GetEnvironmentVariable("GENERIC_BOT_TOKEN")!;
12-
var applicationId = Environment.GetEnvironmentVariable("GENERIC_BOT_APPLICATION_ID")!;
13-
var appConfiguration =
14-
new AppConfiguration(token)
15-
{
16-
Intents = [GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MESSAGES],
17-
ApplicationId = long.Parse(applicationId),
18-
BotPrefix = '?'
19-
};
20-
21-
// Tratar os lifetimes, por padrão é InstancePerDependency (uma nova toda vez que é resolvida)
22-
app.UseDependencyInjection(appConfiguration).WithHttpClient();
23-
await app.RunAsync(appConfiguration);
18+
var app = builder.Build();
19+
await app.RunAsync();

0 commit comments

Comments
 (0)