Files
cherrypick/cherrypick_annotations/lib/src/instance.dart

51 lines
1.6 KiB
Dart
Raw Normal View History

//
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2025-08-08 23:24:05 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import 'package:meta/meta.dart';
/// Marks a provider method or class to always create a new instance (factory) in CherryPick DI.
2025-05-22 13:52:56 +03:00
///
/// Use `@instance()` to annotate methods or classes in your DI module/class
/// when you want a new object to be created on every injection (no singleton caching).
/// The default DI lifecycle is instance/factory unless otherwise specified.
2025-05-22 13:52:56 +03:00
///
/// ### Example (in a module method)
2025-05-22 13:52:56 +03:00
/// ```dart
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
///
2025-05-22 13:52:56 +03:00
/// @module()
/// abstract class FeatureModule {
2025-05-22 13:52:56 +03:00
/// @instance()
/// MyService provideService() => MyService();
2025-05-22 13:52:56 +03:00
///
/// @singleton()
/// Logger provideLogger() => Logger();
2025-05-22 13:52:56 +03:00
/// }
/// ```
2025-05-23 08:21:11 +03:00
///
/// ### Example (on a class, with @injectable)
2025-05-23 08:21:11 +03:00
/// ```dart
/// @injectable()
/// @instance()
/// class MyFactoryClass {
/// // ...
2025-05-23 08:21:11 +03:00
/// }
/// ```
///
/// See also: [@singleton]
@experimental
final class instance {
/// Creates an [instance] annotation for classes or providers.
const instance();
}