Vixen
dd8b0a81
csharp
public sealed class ServiceRegistry

A flat, typed lookup from a service's compile-time type to its single instance. The bootstrapper fills it in by hand and subsystems read from it; that is the whole model.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

This is not a DI container. There is no constructor injection, no lifetime scope, no auto-wiring, and no reflection — a subsystem takes what it needs as constructor parameters, and whoever constructs it supplies them. The registry exists for the genuinely global handful (graphics device, content manager, input, time) that would otherwise be threaded through every signature. Under NativeAOT the alternative does not work at all, and on a frame budget it costs more than it is worth.

The service type is the *static* type argument, not the instance's type, so registering an implementation against its interface is the ordinary case: registry.Add<IGraphicsDevice>(vulkanDevice).

⚠ A value can be registered too, through AddValue``1. The table has always held object, so the where T : class on Add``1 was never about what a dependency may be — it is what lets Get``1 and TryGet``1 answer "not registered" with . A value needs a different pair of methods for that reason and for no other, and it is keyed on its static type exactly as a service is. See AddValue``1 for what that costs a project that needs two of one type.

Reads are lock-free and safe against concurrent writes: a write clones the table and swaps it in, so a reader either sees the old table or the new one, never a torn one. Writes are expected at boot, and are not cheap enough to do per frame.

Fields and properties (2)

  • public int Count

    How many services are registered.

  • public IReadOnlyCollection<Type> ServiceTypes

    The registered service types, in no particular order.

Methods (11)

  • public void Add<T>(T service) where T : class

    Registers under .

  • public void AddValue<T>(T value) where T : struct

    Registers a value under , boxed once.

  • public bool TryAdd<T>(T service) where T : class

    Registers unless is taken.

  • public T? AddOrReplace<T>(T service) where T : class

    Registers , displacing any current registration.

  • public T Get<T>() where T : class

    Looks up the service registered under .

  • public bool TryGet<T>(out T? service) where T : class

    Looks up the service registered under , if there is one.

  • public bool TryGetValue<T>(out T value) where T : struct

    Looks up the value registered under , if there is one.

  • public T? GetOrDefault<T>() where T : class

    Looks up the service registered under , or null.

  • public bool Contains<T>() where T : class

    Whether anything is registered under .

  • public bool Remove<T>() where T : class

    Unregisters . Does not dispose the instance.

  • public void Clear()

    Unregisters everything. Does not dispose the instances.

Used by (10)

  • VillageAiVillage
  • VillageRunAiVillage.Agent.Tests
  • VillageTestsAiVillage.Agent.Tests
  • AppServicesVixen.App.Hosting
  • GameSystemTestsVixen.Engine.Tests
  • ServiceRegistryTestsVixen.Core.Tests
  • HostedEngineTestsVixen.App.Tests
  • HostedRendererTestsVixen.App.Tests
  • SuppliesTheDepotVixen.App.Tests
  • VixenApplicationTestsVixen.App.Tests