Skip to content

Commit e83f25d

Browse files
Copilotianfnelson
andauthored
Remove Autofac dependency — use reflection for IDay discovery (#53)
* Initial plan * Replace Autofac with reflection-based assembly scanning Remove Autofac NuGet dependency and replace the DI container usage in Program.cs with simple reflection to discover and instantiate IDay implementations. Since none of the Day classes have constructor dependencies, a DI container is unnecessary. Co-authored-by: ianfnelson <205606+ianfnelson@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ianfnelson <205606+ianfnelson@users.noreply.github.com>
1 parent 989e297 commit e83f25d

2 files changed

Lines changed: 4 additions & 14 deletions

File tree

AdventOfCode/AdventOfCode.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
<Folder Include="Events\2024\InputFiles\" />
1717
</ItemGroup>
1818

19-
<ItemGroup>
20-
<PackageReference Include="Autofac" Version="9.0.0" />
21-
</ItemGroup>
2219

2320
<ItemGroup>
2421
<None Remove="Events\2024\InputFiles\1.txt" />

AdventOfCode/Program.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
using System.Diagnostics;
22
using System.Reflection;
33
using AdventOfCode.Framework;
4-
using Autofac;
54

6-
var container = BuildContainer();
7-
8-
var days = container.Resolve<IEnumerable<IDay>>();
5+
var days = Assembly.GetExecutingAssembly()
6+
.GetTypes()
7+
.Where(t => t is { IsClass: true, IsAbstract: false } && typeof(IDay).IsAssignableFrom(t))
8+
.Select(t => (IDay)Activator.CreateInstance(t)!);
99
var day = GetDay();
1010
Console.WriteLine("Year " + day.Year + " Day " + day.Day);
1111
var inputPath = $"Events/{day.Year}/InputFiles/{day.Day}.txt";
1212
DoPart(1, () => day.Part1(inputPath));
1313
DoPart(2, () => day.Part2(inputPath));
1414

15-
IContainer BuildContainer()
16-
{
17-
var builder = new ContainerBuilder();
18-
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();
19-
return builder.Build();
20-
}
21-
2215
IDay GetDay()
2316
{
2417
if (args.Length == 0)

0 commit comments

Comments
 (0)