Skip to main content

ServiceContainer

Struct ServiceContainer 

Source
pub struct ServiceContainer { /* private fields */ }
Expand description

Service container that holds all repository instances and provides factory methods for use cases.

The container uses Arc for thread-safe sharing of repository instances across use cases. All repositories are stored as trait objects, allowing for easy substitution with mock implementations during testing.

§Design Principles

  1. Singleton Repositories: Repository instances are created once and shared across all use cases
  2. Factory Methods: Use cases are created on-demand with their dependencies injected
  3. Thread Safety: All repositories are wrapped in Arc for concurrent access
  4. Testability: Repositories can be swapped with mocks via the builder

§Stateless Use Cases

Some use cases are stateless (unit structs with static methods). These are NOT included in the container and should be used directly:

  • PublishArticleUseCase::execute(article)
  • ArchiveArticleUseCase::execute(article)
  • DeleteArticleUseCase::execute(article)
  • RestoreArticleUseCase::execute(article)
  • RecordArticlePurchaseUseCase::execute(article, amount)
  • ExtendAccessUseCase::execute(token, days)
  • And others…

§Example

let container = ContainerBuilder::new()
    .with_in_memory_repositories()
    .build();

// Get use cases with dependencies injected
let register_creator = container.register_creator_use_case();
let initiate_payment = container.initiate_payment_use_case();

Implementations§

Source§

impl ServiceContainer

Source

pub fn new( creator_repository: Arc<dyn CreatorRepository>, article_repository: Arc<dyn ArticleRepository>, transaction_repository: Arc<dyn TransactionRepository>, access_token_repository: Arc<dyn AccessTokenRepository>, fork_repository: Arc<dyn ForkRepository>, event_stream_repository: Arc<dyn EventStreamRepository>, ) -> Self

Creates a new service container with the provided repositories.

Prefer using ContainerBuilder for a more fluent API.

Source

pub fn creator_repository(&self) -> Arc<dyn CreatorRepository>

Returns the creator repository instance.

Source

pub fn article_repository(&self) -> Arc<dyn ArticleRepository>

Returns the article repository instance.

Source

pub fn transaction_repository(&self) -> Arc<dyn TransactionRepository>

Returns the transaction repository instance.

Source

pub fn access_token_repository(&self) -> Arc<dyn AccessTokenRepository>

Returns the access token repository instance.

Source

pub fn fork_repository(&self) -> Arc<dyn ForkRepository>

Returns the fork repository instance.

Source

pub fn event_stream_repository(&self) -> Arc<dyn EventStreamRepository>

Returns the event stream repository instance.

Source

pub fn register_creator_use_case(&self) -> RegisterCreatorUseCase

Creates a new RegisterCreatorUseCase with injected dependencies.

Source

pub fn update_creator_use_case(&self) -> UpdateCreatorUseCase

Creates a new UpdateCreatorUseCase with injected dependencies.

Source

pub fn create_article_use_case(&self) -> CreateArticleUseCase

Creates a new CreateArticleUseCase with injected dependencies.

Source

pub fn update_article_use_case(&self) -> UpdateArticleUseCase

Creates a new UpdateArticleUseCase with injected dependencies.

Source

pub fn initiate_payment_use_case(&self) -> InitiatePaymentUseCase

Creates a new InitiatePaymentUseCase with injected dependencies.

Source

pub fn confirm_transaction_use_case(&self) -> ConfirmTransactionUseCase

Creates a new ConfirmTransactionUseCase with injected dependencies.

Source

pub fn refund_transaction_use_case(&self) -> RefundTransactionUseCase

Creates a new RefundTransactionUseCase with injected dependencies.

Source

pub fn grant_free_access_use_case(&self) -> GrantFreeAccessUseCase

Creates a new GrantFreeAccessUseCase with injected dependencies.

Source

pub fn validate_token_use_case(&self) -> ValidateTokenUseCase

Creates a new ValidateTokenUseCase with injected dependencies.

Source

pub fn revoke_access_use_case(&self) -> RevokeAccessUseCase

Creates a new RevokeAccessUseCase with injected dependencies.

Source

pub fn check_access_use_case(&self) -> CheckAccessUseCase

Creates a new CheckAccessUseCase with injected dependencies.

Source

pub fn cleanup_expired_tokens_use_case(&self) -> CleanupExpiredTokensUseCase

Creates a new CleanupExpiredTokensUseCase with injected dependencies.

Source

pub fn create_fork_use_case(&self) -> CreateForkUseCase

Creates a new CreateForkUseCase with injected dependencies.

Source

pub fn update_fork_use_case(&self) -> UpdateForkUseCase

Creates a new UpdateForkUseCase with injected dependencies.

Source

pub fn merge_fork_use_case(&self) -> MergeForkUseCase

Creates a new MergeForkUseCase with injected dependencies.

Source

pub fn discard_fork_use_case(&self) -> DiscardForkUseCase

Creates a new DiscardForkUseCase with injected dependencies.

Source

pub fn get_fork_use_case(&self) -> GetForkUseCase

Creates a new GetForkUseCase with injected dependencies.

Source

pub fn append_fork_event_use_case(&self) -> AppendForkEventUseCase

Creates a new AppendForkEventUseCase with injected dependencies.

Source

pub fn query_fork_events_use_case(&self) -> QueryForkEventsUseCase

Creates a new QueryForkEventsUseCase with injected dependencies.

Source

pub fn branch_fork_use_case(&self) -> BranchForkUseCase

Creates a new BranchForkUseCase with injected dependencies.

Source

pub fn cleanup_expired_forks_use_case(&self) -> CleanupExpiredForksUseCase

Creates a new CleanupExpiredForksUseCase with injected dependencies.

Trait Implementations§

Source§

impl Clone for ServiceContainer

Source§

fn clone(&self) -> ServiceContainer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ServiceContainer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more