Skip to content

Commit 3efe70d

Browse files
committed
up
1 parent 1fd7ce4 commit 3efe70d

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/Disc.NET/App.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,25 @@ private ILogger<GatewayConnection> CreateLogger(LogLevel level)
3939

4040
return loggerFactory.CreateLogger<GatewayConnection>();
4141
}
42-
42+
/// <summary>
43+
/// Configures the container usage strategy for the application.
44+
///
45+
/// By default, object creation is performed using the standard C# <see cref="Activator"/>,
46+
/// which does not provide support for dependency injection.
47+
///
48+
/// When this method is called, the application switches to using the Autofac container,
49+
/// enabling full dependency injection support for registered components.
50+
///
51+
/// In summary:
52+
/// - Without calling this method → uses default Activator (no DI)
53+
/// - Calling this method → enables Autofac and dependency injection
54+
/// </summary>
55+
/// <param name="appConfiguration">
56+
/// Application configuration instance where the container selection flag is stored.
57+
/// </param>
58+
/// <returns>
59+
/// Returns the singleton instance of <see cref="DiscNetContainer"/> configured to use Autofac.
60+
/// </returns>
4361
public DiscNetContainer UseDependencyInjection(AppConfiguration appConfiguration)
4462
{
4563
appConfiguration.UseContainer = true;

src/Disc.NET/Configuration/DiscNetContainer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,11 @@ public IContainer Build()
6767
_container = _containerBuilder.Build();
6868
return _container;
6969
}
70+
71+
public object Resolve(Type type)
72+
{
73+
var scope = Build().BeginLifetimeScope();
74+
return scope.Resolve(type);
75+
}
7076
}
7177
}

src/Disc.NET/Handlers/HandlerCommandBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ public HandlerCommandBase(AppConfiguration appConfiguration) : base(appConfigura
2828
var commandType = GetCommandTypeByName<TAttribute, TKContext>(commandName);
2929

3030
if (commandType == null) return null;
31-
var container = DiscNetContainer.GetInstance();
3231
if (_appConfiguration.UseContainer)
3332
{
34-
var scope = container.Build().BeginLifetimeScope();
35-
return scope.Resolve(commandType)
36-
as ICommand<TKContext>;
33+
return DiscNetContainer.GetInstance().Resolve(commandType) as ICommand<TKContext>;
3734
}
3835
return Activator.CreateInstance(commandType) as ICommand<TKContext>;
3936
}

0 commit comments

Comments
 (0)