I am using an Azure Function written in Java to retrieve the data from REST API and insert it into the mongo database. I'm trying to separate the app into different layers like I usually do for web applications - for now I've only extracted the repository that stores the data in mongo into a separate class, so my function class looks like this (I've omitted triggers, error handlings etc)
public class SensorFunctions {
@FunctionName("saveSensors")
public void saveSensors(
final ExecutionContext context) {
SensorRepository sensorRepository = new SensorRepository();
new SensorAPI().retrieveSensors()
.forEach(sensorRepository::saveSensor);
}
}
I'd prefer to use some king of IoC mechanism, so I don't have to instantiate repostiory and other classes by myself but I can do something like
public class SensorFunctions {
@Inject
SensorRepository sensorRepository;
@Inject
SensorAPI sensorAPI;
@FunctionName("saveSensors")
public void saveSensors(
final ExecutionContext context) {
sensorAPI.retrieveSensors()
.forEach(sensorRepository::saveSensor);
}
}
Is it possible with Azure functions? Is so, is it possible to create an automatic configuration or do I need to trigger the configuration of the IoC container manually at the beginning of each function (I will have multiple functions in a single project). As the cost is dependent on the function's computation time, I'd prefer as lightweight solution as possible