# Services and dependency injection

Give a window the services it needs through its constructor. This keeps business behavior separate from drawing and makes the same service usable from several pages.

The example supplies a greeting service to a window. It uses `Xui.Core.DI` and `Microsoft.Extensions.DependencyInjection`, as the starter host does.

```csharp
using System;
using Microsoft.Extensions.DependencyInjection;
using Xui.Core.Abstract;
using Xui.Core.DI;
using Xui.Core.UI;

public sealed class GreetingService
{
    public string For(string name) => $"Welcome, {name}.";
}

public sealed class GreetingWindow : Window
{
    public GreetingWindow(IServiceProvider context, GreetingService greetings) : base(context)
    {
        Title = "Shared services";
        Content = new Label
        {
            Text = greetings.For("Gigi"),
            FontSize = 24,
            Margin = 24
        };
    }
}

public sealed class GreetingApplication(IServiceProvider context) : Application(context)
{
    public override void Start() => this.CreateAndShowMainWindowOnce<GreetingWindow>();
}

public static class ServiceComposition
{
    // Call this from the starter's existing ConfigureServices callback.
    public static void Register(IServiceCollection services)
    {
        services.AddSingleton<GreetingService>();
        services.AddScoped<GreetingWindow>();
        services.AddScoped<GreetingApplication>();
    }
}
```

## Connect it to the starter host

Keep the starter's platform setup and `UseRuntime()` call. In its `ConfigureServices` callback, register these classes with `ServiceComposition.Register(services)`. Use `Run<GreetingApplication>()` for this alternative application. Replace the starter's application/window registrations when switching to this example; running the original application still creates the original window.

The important boundary is the constructor. `GreetingWindow` receives a `GreetingService`, then creates ordinary view content. The service does not depend on Canvas, a native window, or a particular device. Test its `For` method without launching a UI.

## Choose a lifetime deliberately

`AddSingleton` gives the host one service instance. Use that for shared application state or a cache that is safe for its callers. It does not make the instance thread-safe.

`AddScoped` gives each dependency-injection scope its own instance. `CreateAndShowMainWindowOnce<TWindow>()` creates a scope, resolves the window, and adds that scope to the window's disposal queue. Scoped dependencies therefore belong to that window. Two windows can receive separate drafts while sharing a singleton repository.

Do not inject a window-scoped service into a singleton and retain it after the window closes. Pass short-lived data to the singleton, or create a deliberate scope for the operation.

## Resolve view services only when available

A view also implements `IServiceProvider`. Its default lookup walks through its parent; the root connects to the owning window's platform and application service chain. This supplies context-sensitive services such as `IFocus` and `IDeviceInfo`.

A detached view has no such parent chain. Resolve platform-dependent services after attachment or activation, handle an optional service being absent, and avoid resolving required app dependencies implicitly inside rendering. Constructor injection makes those dependencies clear.

The application's runtime exposes `MainDispatcher`; use it for [async UI updates](../async-work/index.md). A dispatcher is a scheduling service, not permission to keep a disposed window alive.
