Overview
The Beehive feature implements Clean Architecture use cases for all beehive-related operations. Each use case encapsulates a single business operation and follows the UseCase interface pattern, returning Either<Failure, Result> for functional error handling.
CreateBeehiveUseCase
Creates a new beehive within a specified apiary.
Class Definition
class CreateBeehiveUseCase implements UseCase < Beehive , CreateBeehiveParams >
Parameters
params
CreateBeehiveParams
required
Parameters object containing beehive creation data
CreateBeehiveParams
ID of the apiary where the beehive will be created
Sequential number for the new beehive
Initial activity level: “Alta”, “Media”, or “Baja”
Initial population classification: “Alta”, “Media”, or “Baja”
Initial hive configuration status
Whether the hive has a production chamber: “Si” or “No”
Additional notes or observations
Return Type
Returns either a Failure on error or the newly created Beehive entity on success
Example Usage
import 'package:Softbee/feature/beehive/domain/usecases/create_beehive_usecase.dart' ;
import 'package:Softbee/feature/beehive/domain/repositories/beehive_repository.dart' ;
// Inject repository (typically done via dependency injection)
final createBeehiveUseCase = CreateBeehiveUseCase (beehiveRepository);
// Create parameters
final params = CreateBeehiveParams (
apiaryId : 'apiary_123' ,
beehiveNumber : 15 ,
activityLevel : 'Alta' ,
beePopulation : 'Media' ,
foodFrames : 3 ,
broodFrames : 7 ,
hiveStatus : 'Cámara de cría y producción' ,
healthStatus : 'Ninguno' ,
hasProductionChamber : 'Si' ,
observations : 'New colony from split' ,
);
// Execute use case
final result = await createBeehiveUseCase (params);
result. fold (
(failure) => print ( 'Error creating beehive: ${ failure . message } ' ),
(beehive) => print ( 'Created beehive: ${ beehive . id } ' ),
);
GetBeehivesByApiaryUseCase
Retrieves all beehives belonging to a specific apiary.
Class Definition
class GetBeehivesByApiaryUseCase implements UseCase < List < Beehive >, String >
Parameters
The apiary ID to retrieve beehives for
Return Type
Either<Failure, List<Beehive>>
Returns either a Failure on error or a list of Beehive entities on success
Example Usage
import 'package:Softbee/feature/beehive/domain/usecases/get_beehives_by_apiary_usecase.dart' ;
// Inject repository
final getBeehivesUseCase = GetBeehivesByApiaryUseCase (beehiveRepository);
// Execute use case with apiary ID
final result = await getBeehivesUseCase ( 'apiary_123' );
result. fold (
(failure) => print ( 'Error fetching beehives: ${ failure . message } ' ),
(beehives) {
print ( 'Found ${ beehives . length } beehives' );
for ( final beehive in beehives) {
print ( 'Beehive # ${ beehive . beehiveNumber } : ${ beehive . healthStatus } ' );
}
},
);
UpdateBeehiveUseCase
Updates an existing beehive’s information.
Class Definition
class UpdateBeehiveUseCase implements UseCase < Beehive , UpdateBeehiveParams >
Parameters
params
UpdateBeehiveParams
required
Parameters object containing update data
UpdateBeehiveParams
ID of the beehive to update
ID of the parent apiary (for authorization context)
Updated population classification
Updated number of food frames
Updated number of brood frames
Updated production chamber status
Return Type
Returns either a Failure on error or the updated Beehive entity on success
Example Usage
import 'package:Softbee/feature/beehive/domain/usecases/update_beehive_usecase.dart' ;
// Inject repository
final updateBeehiveUseCase = UpdateBeehiveUseCase (beehiveRepository);
// Create update parameters
final params = UpdateBeehiveParams (
beehiveId : 'beehive_789' ,
apiaryId : 'apiary_123' ,
activityLevel : 'Media' , // Changed from 'Alta'
foodFrames : 5 , // Added more food frames
healthStatus : 'Presencia barroa' , // Detected health issue
observations : 'Varroa treatment started on 2024-03-05' ,
);
// Execute use case
final result = await updateBeehiveUseCase (params);
result. fold (
(failure) => print ( 'Error updating beehive: ${ failure . message } ' ),
(beehive) => print ( 'Updated beehive: ${ beehive . id } ' ),
);
DeleteBeehiveUseCase
Deletes a beehive from an apiary.
Class Definition
class DeleteBeehiveUseCase implements UseCase < void , DeleteBeehiveParams >
Parameters
params
DeleteBeehiveParams
required
Parameters object containing deletion identifiers
DeleteBeehiveParams
ID of the beehive to delete
ID of the parent apiary (required for backend authorization)
Return Type
Returns either a Failure on error or void on successful deletion
Example Usage
import 'package:Softbee/feature/beehive/domain/usecases/delete_beehive_usecase.dart' ;
// Inject repository
final deleteBeehiveUseCase = DeleteBeehiveUseCase (beehiveRepository);
// Create deletion parameters
final params = DeleteBeehiveParams (
beehiveId : 'beehive_789' ,
apiaryId : 'apiary_123' ,
);
// Execute use case
final result = await deleteBeehiveUseCase (params);
result. fold (
(failure) => print ( 'Error deleting beehive: ${ failure . message } ' ),
(_) => print ( 'Beehive successfully deleted' ),
);
UseCase Interface
All beehive use cases implement the generic UseCase interface:
abstract class UseCase < Type , Params > {
Future < Either < Failure , Type >> call ( Params params);
}
This interface ensures:
Consistent error handling with Either<Failure, Type>
Single responsibility (one operation per use case)
Easy testing and mocking
Clear separation of business logic from presentation
Error Handling
All use cases return Either<Failure, Result> from the either_dart package:
Show Common Failure Types
AuthFailure Returned when authentication token is missing or invalid. AuthFailure ( 'No se encontró el token de autenticación.' )
ServerFailure Returned when server operations fail. ServerFailure (errorMessage)
Handling Results
final result = await useCase (params);
// Pattern 1: fold
result. fold (
(failure) => handleError (failure),
(success) => handleSuccess (success),
);
// Pattern 2: isLeft/isRight
if (result.isLeft) {
final failure = result.left;
// Handle error
} else {
final data = result.right;
// Handle success
}
Dependency Injection
Use cases require a BeehiveRepository instance. In production, inject via your DI container:
// Example with Riverpod
final createBeehiveUseCaseProvider = Provider < CreateBeehiveUseCase >(
(ref) => CreateBeehiveUseCase (
ref. watch (beehiveRepositoryProvider),
),
);
Location
Source files:
/lib/feature/beehive/domain/usecases/create_beehive_usecase.dart
/lib/feature/beehive/domain/usecases/get_beehives_by_apiary_usecase.dart
/lib/feature/beehive/domain/usecases/update_beehive_usecase.dart
/lib/feature/beehive/domain/usecases/delete_beehive_usecase.dart