Overview
The Application layer orchestrates business workflows by coordinating domain entities and infrastructure services. It contains use cases, application services, and defines interfaces for external dependencies following the Dependency Inversion Principle.Core Interfaces
IAppDbContext
Defines the database abstraction for the application.IPubSub
Abstraction for pub/sub messaging (implemented by NATS).INotificationChannel
Defines notification delivery strategy (Telegram, email, etc.).Rule Engine
RuleEngine
Core service that evaluates price rules using NRules (business rules engine).- Add incoming price to history cache
- Insert price and matching rules into NRules session
- Fire rules engine to evaluate conditions
- Clean up session state
TechnicalAnalysisRule
NRules rule that evaluates price conditions and technical indicators.PriceRuleNotificationRule
NRules rule that handles notifications when price rules are triggered.Caching Services
PriceHistoryCache
In-memory cache for recent price data, required for technical analysis.- Thread-safe concurrent access
- Fixed size queue (default 500 prices)
- Per-symbol history tracking
- Used by technical indicators (RSI, SMA, EMA)
RuleCache
Caches active price rules to avoid database queries on every price update.Notification Services
NotificationService
Routes notifications to the appropriate channel based on user preferences.TelegramNotificationChannel
Implements Telegram bot notifications.Data Structures
FixedSizeQueue<T>
Circular buffer for efficient price history storage.Dependency Injection
Application Services Registration
- Singleton: Caches, NRules session, notification channels
- Scoped: RuleEngine (per-request)
- MediatR: Domain event handling
External Service Integrations
Binance API
Interface for Binance exchange data.Alpaca News API
Interface for crypto news data.Architecture Benefits
Clean Architecture Principles
- Dependency Inversion: Application depends on abstractions (interfaces), not implementations
- Separation of Concerns: Business logic isolated from infrastructure
- Testability: Easy to mock dependencies for unit testing
- Flexibility: Swap implementations without changing application code
Performance Optimizations
- In-memory caching reduces database load
- NRules provides fast rule evaluation
- Concurrent collections for thread-safety
- Fixed-size buffers prevent memory growth
Next Steps
- See Domain Layer for entity definitions
- Learn about Infrastructure Layer implementations
- Explore GraphQL Layer for API exposure