How Do I Create an Object?¶
Without any configuration¶
Composer.Create() needs no configuration for plain types — Compono's source generator produces the construction plan for Customer from its constructor/required members directly.
Several independent objects at once¶
See Collections for how this differs from a collection-typed member of a composed object.
With one-off configuration¶
var composer = Composer.Create(builder => builder
.For<string>().Use("acme-corp")
.Register<IClock>(_ => new FakeClock()));
var customer = composer.Create<Customer>();
Reach for Register a Type or Customize a Member once you need to control a specific dependency rather than every composed value of a type.
From an xUnit v3 theory instead¶
If you're inside a [Theory] method, you don't call Composer.Create yourself at all — Compono.XunitV3's [Compose] attribute does it for you, per parameter:
See Write a Composed Theory for the full picture, including mixing composed and inline values.
Common mistakes¶
- Calling
Composer.Createinside the test method itself, per-assertion — aComposeris meant to be created once (per test, or once and reused via a fixture/profile), not rebuilt for everyCreate<T>()call. - Expecting two separately-composed parameters of the same type to be the same instance — they aren't, by default. See Shared Values if you need that.
Next¶
- The mental model behind what just happened → The Composition Model.
- A composed xUnit theory instead of programmatic creation → Write a Composed Theory.
- Narrow recipes → Cookbook.