New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Add DI between services #19
Comments
This is possible and would be a very helpful feature actually. I will work on this soon. |
@xamantra Since we're on the topic, any chance of also allowing callbacks to setup a service inside the momentum using existing services eg.: Momentum(
services: [
Service(MyService, () {
var apiService = getService<ApiService>();
return MyService(apiService);
}),
FactoryService(MyService, () { ... }), // same concept except it creates a new instance every time it's called
]
); |
@exts, can you explain more about this? like, why would you want to have this and some use cases? |
For the factory service, sometimes you don't want to keep an instance of the same object (I'm assuming it's keeping a copy of the instance when its first called, I haven't tested or checked the source). For the other example, it just respects DIP, plus sometimes you might have a service that can be used on its own and another service that may rely on it. This would just allow you to pass it as a dependency when setting up the application. This would also solve ops issue as well which is cleaner and would make the services easier to test as well w/out needing to setup an momentum object. |
I'm already done writing the function for var apiService = getService<ApiService>();
return MyService(apiService);
I see. An example of this would be like an services: [
ApiService(), // false by default.
ApiService(enableLogs: true),
], The question is, how can we grab the API service that has logs enabled and the one with logs disabled based on your // get the API service with logs enabled.
var apiServiceA = getService<ApiService>(where: (apiService) => apiService.logsEnabled);
// get the API service with logs disabled.
var apiServiceB = getService<ApiService>(where: (apiService) => !apiService.logsEnabled);
// if "where" parameter isn't provided, it returns the first instance from the "services" list.
// And it doesn't break existing projects too.
var apiService = getService<ApiService>(); So I think we don't need the FactoryService anymore. |
On one end I like it, but I feel like wouldn't it be quicker to have some form of alias. What happens if your where clause gets unnecessarily lengthy? var apiServiceExample = getService<ApiService>(alias: "logs");
///....
Momentum(
services: [
Service(ApiService, alias: "logs", () => ApiService(enableLogs: true)),
]
); This is just me throwing out different ideas before you commit to one solution. e: This also made me think about the many services and analytics tools out there that some apps use w/ similar interfaces which map to different apis. |
First, I would like to cleanup this code: Service(ApiService, alias: "logs", () => ApiService(enableLogs: true)),
// You don't need the first runtime type parameter "ApiService".
Service(alias: "logs", () => ApiService(enableLogs: true)),
// You don't need a lambda to generate the service.
Service(alias: "logs", ApiService(enableLogs: true)), I would also want to make the Momentum(
services: [
DatabaseService(),
DownloadService(),
InjectService(alias: ApiAlias.withoutLogs, ApiService()),
InjectService(alias: ApiAlias.withLogs, ApiService(enableLogs: true)),
],
);
// accessing down the tree
var apiService = getService<ApiService>(alias: ApiAlias.withoutLogs);
// using context (non-breaking change).
var apiService = getService<ApiService>(context, alias: ApiAlias.withoutLogs);
// will return this service: "InjectService(alias: ApiAlias.withoutLogs, ApiService())," (the first one from the list)
var apiService = getService<ApiService>(); The code is a bit longer but it's much safer than using strings. I'll forget about the |
yeah I like that much better. |
I published a new version on pub.dev: v1.2.8 Docs References:
|
I'll test out the changes tonight to make sure everything works. |
@exts and @d3fkon . If you guys found an issue relating to this new feature. File a separate issue. I will be closing this one. |
In my current project, I have a
StorageService
,DownloadService
and anAPIService
.More often than not, whatever is downloaded needs to be stored in the database and the API needs to be called to get the bucket URL for downloading the file.
Would it make sense to be able to access services using
getService
inside aMomentumService
?Or should I stick to injecting them through the
bootStrap
of a non-lazy controller?The text was updated successfully, but these errors were encountered: