[{"data":1,"prerenderedAt":736},["ShallowReactive",2],{"$fikVW39sBv9xKgjHzNDmUVF7JB0XAdL2tX9L8BnPUDkw":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":719,"editUrl":730,"prev":731,"next":734},"overview\u002Fproviders","Providers","Services registered with the container and injected with","---\ntitle: Providers\ndescription: Services registered with the container and injected with #[injectable] and #[inject].\n---\n\n# Providers\n\nProviders are the building blocks of Ferrite. Anything you want to *share* or *resolve* through dependency injection — services, repositories, infrastructure clients — is a provider. Each provider is registered with the DI container and resolved by type.\n\n## `#[injectable]`\n\nMark a struct as a provider:\n\n```rust\nuse ferrite_framework::{injectable, inject};\n\n#[injectable]\npub struct UsersService {\n    db: Repository\u003CUser>,\n}\n\nimpl UsersService {\n    #[inject]\n    pub fn new(db: Repository\u003CUser>) -> Self {\n        Self { db }\n    }\n}\n```\n\n### Rules\n\n- Applies to a **struct** (no generics).\n- Fields are automatically rewritten to `Arc\u003CT>`, so you can clone handles cheaply.\n- You must supply a constructor with `#[inject]` on `pub fn new(...)`.\n- Parameter types of `new` become the provider's dependencies.\n\n### Scopes\n\n```rust\n#[injectable(scope = \"singleton\")]  \u002F\u002F default — one instance for the app lifetime\n#[injectable(scope = \"request\")]    \u002F\u002F one instance per request\n#[injectable(scope = \"transient\")]  \u002F\u002F a fresh instance every resolution\n```\n\nAnything that isn't `request` or `transient` registers as a singleton. `Scope { Singleton, Request, Transient }` mirrors this at the runtime level.\n\n## `#[inject]`\n\n`#[inject]` annotates the constructor. It rewrites each parameter to `Arc\u003CT>` and generates two functions:\n\n- `__ferrite_deps()` → the ordered `Vec\u003CTypeId>` of dependencies\n- `__ferrite_inject(&Container)` → resolves each dependency from the container and calls the constructor\n\nZero-dependency constructors are supported too:\n\n```rust\n#[injectable]\npub struct AppService;\n\nimpl AppService {\n    #[inject]\n    pub fn new() -> Self {\n        Self\n    }\n}\n```\n\n## Resolution & injection\n\nHandlers and controllers access providers through constructor injection:\n\n```rust\nuse ferrite_framework::{injectable, inject};\n\n#[injectable]\npub struct HealthController {\n    service: AppService,\n}\n\nimpl HealthController {\n    #[inject]\n    pub fn new(service: AppService) -> Self {\n        Self { service }\n    }\n}\n```\n\n## The container\n\n`#[injectable]` registers a `ProviderEntry` into an `inventory` registry at compile time. At bootstrap, Ferrite assembles a `fr_core::Container`:\n\n- `Container::get\u003CT>() -> Arc\u003CT>` — panics on resolution failure\n- `Container::try_get\u003CT>() -> Option\u003CArc\u003CT>>`\n- `Container::seed_singleton\u003CT>(value: Arc\u003CT>)` — pre-populate a provider before the graph compiles\n\nThe running app exposes its container, which is how subsystems mount themselves:\n\n```rust\nlet app = Ferrite::create::\u003CAppModule>().await;\nlet container = &app.container;\nlet service: Arc\u003CAppService> = container.get::\u003CAppService>();\n```\n\n## Global providers\n\nSome providers are registered **globally** — resolvable without being imported by a module. Examples:\n\n- `ConfigService`\n- `InMemoryPick` (ORM)\n- `WsServer` (WebSockets)\n- `GrpcServer` (gRPC)\n\nThese register directly into inventory from their crates, so any module can inject them.\n\n## Next steps\n\n- [Modules](\u002Fdocs\u002Foverview\u002Fmodules) — group providers and controllers.\n- [Dependency injection](\u002Fdocs\u002Ffundamentals\u002Fdependency-injection) — the full DI story.\n- [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration) — `ConfigService` in depth.",{"frontmatter":9,"meta":10,"nodes":41},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,26,29,32,35,38],{"id":17,"depth":13,"text":18,"children":19},"injectable","#[injectable]",[20,23],{"id":21,"depth":14,"text":22},"injectable-rules","Rules",{"id":24,"depth":14,"text":25},"injectable-scopes","Scopes",{"id":27,"depth":13,"text":28},"inject","#[inject]",{"id":30,"depth":13,"text":31},"resolution-injection","Resolution & injection",{"id":33,"depth":13,"text":34},"the-container","The container",{"id":36,"depth":13,"text":37},"global-providers","Global providers",{"id":39,"depth":13,"text":40},"next-steps","Next steps",[42,46,59,65,68,209,212,248,250,302,316,320,328,346,349,412,414,417,517,519,535,554,557,653,655,662,687,690,692],[43,44,5],"h1",{"id":45},"providers",[47,48,49,50,54,55,58],"p",{},"Providers are the building blocks of Ferrite. Anything you want to ",[51,52,53],"em",{},"share"," or ",[51,56,57],{},"resolve"," through dependency injection — services, repositories, infrastructure clients — is a provider. Each provider is registered with the DI container and resolved by type.",[60,61,62],"h2",{"id":17},[63,64,18],"code",{},[47,66,67],{},"Mark a struct as a provider:",[69,70,73],"pre",{"language":71,"class":72},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[63,74,76,96,97,96,99,96,103,96,117,96,137,96,142,96,144,96,153,96,158,96,192,96,200,96,205],{"class":75},"language-rust",[77,78,81,85,89,92],"span",{"class":79,"style":80},"line","display: inline",[77,82,84],{"style":83},"color:#D73A49;--shiki-dark:#F97583","use",[77,86,88],{"style":87},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[77,90,91],{"style":83},"::",[77,93,95],{"style":94},"color:#24292E;--shiki-dark:#E1E4E8","{injectable, inject};","\n",[77,98],{"class":79,"style":80},[77,100,101],{"class":79,"style":80},[77,102,18],{"style":94},[77,104,105,108,111,114],{"class":79,"style":80},[77,106,107],{"style":83},"pub",[77,109,110],{"style":83}," struct",[77,112,113],{"style":87}," UsersService",[77,115,116],{"style":94}," {",[77,118,119,122,125,128,131,134],{"class":79,"style":80},[77,120,121],{"style":94},"    db",[77,123,124],{"style":83},":",[77,126,127],{"style":87}," Repository",[77,129,130],{"style":94},"\u003C",[77,132,133],{"style":87},"User",[77,135,136],{"style":94},">,",[77,138,139],{"class":79,"style":80},[77,140,141],{"style":94},"}",[77,143],{"class":79,"style":80},[77,145,146,149,151],{"class":79,"style":80},[77,147,148],{"style":83},"impl",[77,150,113],{"style":87},[77,152,116],{"style":94},[77,154,155],{"class":79,"style":80},[77,156,157],{"style":94},"    #[inject]",[77,159,160,163,166,169,172,174,176,178,180,183,186,190],{"class":79,"style":80},[77,161,162],{"style":83},"    pub",[77,164,165],{"style":83}," fn",[77,167,168],{"style":87}," new",[77,170,171],{"style":94},"(db",[77,173,124],{"style":83},[77,175,127],{"style":87},[77,177,130],{"style":94},[77,179,133],{"style":87},[77,181,182],{"style":94},">) ",[77,184,185],{"style":83},"->",[77,187,189],{"style":188},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[77,191,116],{"style":94},[77,193,194,197],{"class":79,"style":80},[77,195,196],{"style":188},"        Self",[77,198,199],{"style":94}," { db }",[77,201,202],{"class":79,"style":80},[77,203,204],{"style":94},"    }",[77,206,207],{"class":79,"style":80},[77,208,141],{"style":94},[210,211,22],"h3",{"id":21},[213,214,215,224,231,241],"ul",{},[216,217,218,219,223],"li",{},"Applies to a ",[220,221,222],"strong",{},"struct"," (no generics).",[216,225,226,227,230],{},"Fields are automatically rewritten to ",[63,228,229],{},"Arc\u003CT>",", so you can clone handles cheaply.",[216,232,233,234,236,237,240],{},"You must supply a constructor with ",[63,235,28],{}," on ",[63,238,239],{},"pub fn new(...)",".",[216,242,243,244,247],{},"Parameter types of ",[63,245,246],{},"new"," become the provider's dependencies.",[210,249,25],{"id":24},[69,251,252],{"language":71,"class":72},[63,253,254,96,273,96,288],{"class":75},[77,255,256,259,262,266,269],{"class":79,"style":80},[77,257,258],{"style":94},"#[injectable(scope ",[77,260,261],{"style":83},"=",[77,263,265],{"style":264},"color:#032F62;--shiki-dark:#9ECBFF"," \"singleton\"",[77,267,268],{"style":94},")]  ",[77,270,272],{"style":271},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F default — one instance for the app lifetime",[77,274,275,277,279,282,285],{"class":79,"style":80},[77,276,258],{"style":94},[77,278,261],{"style":83},[77,280,281],{"style":264}," \"request\"",[77,283,284],{"style":94},")]    ",[77,286,287],{"style":271},"\u002F\u002F one instance per request",[77,289,290,292,294,297,299],{"class":79,"style":80},[77,291,258],{"style":94},[77,293,261],{"style":83},[77,295,296],{"style":264}," \"transient\"",[77,298,268],{"style":94},[77,300,301],{"style":271},"\u002F\u002F a fresh instance every resolution",[47,303,304,305,54,308,311,312,315],{},"Anything that isn't ",[63,306,307],{},"request",[63,309,310],{},"transient"," registers as a singleton. ",[63,313,314],{},"Scope { Singleton, Request, Transient }"," mirrors this at the runtime level.",[60,317,318],{"id":27},[63,319,28],{},[47,321,322,324,325,327],{},[63,323,28],{}," annotates the constructor. It rewrites each parameter to ",[63,326,229],{}," and generates two functions:",[213,329,330,340],{},[216,331,332,335,336,339],{},[63,333,334],{},"__ferrite_deps()"," → the ordered ",[63,337,338],{},"Vec\u003CTypeId>"," of dependencies",[216,341,342,345],{},[63,343,344],{},"__ferrite_inject(&Container)"," → resolves each dependency from the container and calls the constructor",[47,347,348],{},"Zero-dependency constructors are supported too:",[69,350,351],{"language":71,"class":72},[63,352,353,96,357,96,369,96,371,96,379,96,383,96,400,96,404,96,408],{"class":75},[77,354,355],{"class":79,"style":80},[77,356,18],{"style":94},[77,358,359,361,363,366],{"class":79,"style":80},[77,360,107],{"style":83},[77,362,110],{"style":83},[77,364,365],{"style":87}," AppService",[77,367,368],{"style":94},";",[77,370],{"class":79,"style":80},[77,372,373,375,377],{"class":79,"style":80},[77,374,148],{"style":83},[77,376,365],{"style":87},[77,378,116],{"style":94},[77,380,381],{"class":79,"style":80},[77,382,157],{"style":94},[77,384,385,387,389,391,394,396,398],{"class":79,"style":80},[77,386,162],{"style":83},[77,388,165],{"style":83},[77,390,168],{"style":87},[77,392,393],{"style":94},"() ",[77,395,185],{"style":83},[77,397,189],{"style":188},[77,399,116],{"style":94},[77,401,402],{"class":79,"style":80},[77,403,196],{"style":188},[77,405,406],{"class":79,"style":80},[77,407,204],{"style":94},[77,409,410],{"class":79,"style":80},[77,411,141],{"style":94},[60,413,31],{"id":30},[47,415,416],{},"Handlers and controllers access providers through constructor injection:",[69,418,419],{"language":71,"class":72},[63,420,421,96,431,96,433,96,437,96,448,96,460,96,464,96,466,96,474,96,478,96,502,96,509,96,513],{"class":75},[77,422,423,425,427,429],{"class":79,"style":80},[77,424,84],{"style":83},[77,426,88],{"style":87},[77,428,91],{"style":83},[77,430,95],{"style":94},[77,432],{"class":79,"style":80},[77,434,435],{"class":79,"style":80},[77,436,18],{"style":94},[77,438,439,441,443,446],{"class":79,"style":80},[77,440,107],{"style":83},[77,442,110],{"style":83},[77,444,445],{"style":87}," HealthController",[77,447,116],{"style":94},[77,449,450,453,455,457],{"class":79,"style":80},[77,451,452],{"style":94},"    service",[77,454,124],{"style":83},[77,456,365],{"style":87},[77,458,459],{"style":94},",",[77,461,462],{"class":79,"style":80},[77,463,141],{"style":94},[77,465],{"class":79,"style":80},[77,467,468,470,472],{"class":79,"style":80},[77,469,148],{"style":83},[77,471,445],{"style":87},[77,473,116],{"style":94},[77,475,476],{"class":79,"style":80},[77,477,157],{"style":94},[77,479,480,482,484,486,489,491,493,496,498,500],{"class":79,"style":80},[77,481,162],{"style":83},[77,483,165],{"style":83},[77,485,168],{"style":87},[77,487,488],{"style":94},"(service",[77,490,124],{"style":83},[77,492,365],{"style":87},[77,494,495],{"style":94},") ",[77,497,185],{"style":83},[77,499,189],{"style":188},[77,501,116],{"style":94},[77,503,504,506],{"class":79,"style":80},[77,505,196],{"style":188},[77,507,508],{"style":94}," { service }",[77,510,511],{"class":79,"style":80},[77,512,204],{"style":94},[77,514,515],{"class":79,"style":80},[77,516,141],{"style":94},[60,518,34],{"id":33},[47,520,521,523,524,527,528,531,532,124],{},[63,522,18],{}," registers a ",[63,525,526],{},"ProviderEntry"," into an ",[63,529,530],{},"inventory"," registry at compile time. At bootstrap, Ferrite assembles a ",[63,533,534],{},"fr_core::Container",[213,536,537,543,548],{},[216,538,539,542],{},[63,540,541],{},"Container::get\u003CT>() -> Arc\u003CT>"," — panics on resolution failure",[216,544,545],{},[63,546,547],{},"Container::try_get\u003CT>() -> Option\u003CArc\u003CT>>",[216,549,550,553],{},[63,551,552],{},"Container::seed_singleton\u003CT>(value: Arc\u003CT>)"," — pre-populate a provider before the graph compiles",[47,555,556],{},"The running app exposes its container, which is how subsystems mount themselves:",[69,558,559],{"language":71,"class":72},[63,560,561,96,594,96,614],{"class":75},[77,562,563,566,569,571,574,576,579,581,583,586,589,592],{"class":79,"style":80},[77,564,565],{"style":83},"let",[77,567,568],{"style":94}," app ",[77,570,261],{"style":83},[77,572,573],{"style":87}," Ferrite",[77,575,91],{"style":83},[77,577,578],{"style":87},"create",[77,580,91],{"style":83},[77,582,130],{"style":94},[77,584,585],{"style":87},"AppModule",[77,587,588],{"style":94},">()",[77,590,591],{"style":83},".await",[77,593,368],{"style":94},[77,595,596,598,601,603,606,609,611],{"class":79,"style":80},[77,597,565],{"style":83},[77,599,600],{"style":94}," container ",[77,602,261],{"style":83},[77,604,605],{"style":83}," &",[77,607,608],{"style":94},"app",[77,610,240],{"style":83},[77,612,613],{"style":94},"container;",[77,615,616,618,621,623,626,628,631,634,636,639,641,644,646,648,650],{"class":79,"style":80},[77,617,565],{"style":83},[77,619,620],{"style":94}," service",[77,622,124],{"style":83},[77,624,625],{"style":87}," Arc",[77,627,130],{"style":94},[77,629,630],{"style":87},"AppService",[77,632,633],{"style":94},"> ",[77,635,261],{"style":83},[77,637,638],{"style":94}," container",[77,640,240],{"style":83},[77,642,643],{"style":87},"get",[77,645,91],{"style":83},[77,647,130],{"style":94},[77,649,630],{"style":87},[77,651,652],{"style":94},">();",[60,654,37],{"id":36},[47,656,657,658,661],{},"Some providers are registered ",[220,659,660],{},"globally"," — resolvable without being imported by a module. Examples:",[213,663,664,669,675,681],{},[216,665,666],{},[63,667,668],{},"ConfigService",[216,670,671,674],{},[63,672,673],{},"InMemoryPick"," (ORM)",[216,676,677,680],{},[63,678,679],{},"WsServer"," (WebSockets)",[216,682,683,686],{},[63,684,685],{},"GrpcServer"," (gRPC)",[47,688,689],{},"These register directly into inventory from their crates, so any module can inject them.",[60,691,40],{"id":39},[213,693,694,702,709],{},[216,695,696,701],{},[697,698,700],"a",{"href":699},"\u002Fdocs\u002Foverview\u002Fmodules","Modules"," — group providers and controllers.",[216,703,704,708],{},[697,705,707],{"href":706},"\u002Fdocs\u002Ffundamentals\u002Fdependency-injection","Dependency injection"," — the full DI story.",[216,710,711,715,716,718],{},[697,712,714],{"href":713},"\u002Fdocs\u002Foverview\u002Fconfiguration","Configuration"," — ",[63,717,668],{}," in depth.",{"title":12,"searchDepth":13,"depth":14,"links":720},[721,725,726,727,728,729],{"id":17,"depth":13,"text":18,"children":722},[723,724],{"id":21,"depth":14,"text":22},{"id":24,"depth":14,"text":25},{"id":27,"depth":13,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},{"id":39,"depth":13,"text":40},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fproviders.md",{"title":732,"slug":733},"Controllers","overview\u002Fcontrollers",{"title":700,"slug":735},"overview\u002Fmodules",1789565440248]