Skip to content
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

Adding service dependency injection feature for Services #111

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

SimonDarksideJ
Copy link
Contributor

@SimonDarksideJ SimonDarksideJ commented Oct 22, 2024

Reality Collective - Service Framework Pull Request

Overview

Adding the much anticipated Dependency Injection feature, which allows constructors like the following

public DependencyTestService2(string name, uint priority, BaseProfile profile, IParentService parentService)

So long as the dependent service is registered in the Service Configuration PRIOR to the above service being registered, it will automatically inject the reference to the existing registered service.
Removes the need for unnecessary service wind up functionality within a service.

Changes

  • Adds a new TryGetService call for simplification
  • Adds dependency check if there are additional service interfaces included in the primary constructor

Note

Only the primary constructor is inspected and ONLY parameters deriving from IService will be included. Any other types of parameters are likely to introduce errors.

Breaking Changes

  • None

Testing status

  • Includes unit tests.

Notes

This is designed for Services only at present. Modules will be evaluated in future updates

@SimonDarksideJ SimonDarksideJ self-assigned this Oct 22, 2024
@SimonDarksideJ SimonDarksideJ requested a review from FejZa as a code owner October 22, 2024 14:03
@FejZa
Copy link
Contributor

FejZa commented Oct 22, 2024

Will this always assume the first service is the parent service and following are dependencies?

@SimonDarksideJ
Copy link
Contributor Author

Will this always assume the first service is the parent service and following are dependencies?

The first 3 parameters are mandatory for the service registration, any ADDITIONAL parameters need to be based upon IService and you can add as many as you like (so long as their services are registered before this service in the Service COnfiguration, e.g:
A basic service

public DependencyTestService2(string name, uint priority, BaseProfile profile)

A service with one dependency

public DependencyTestService2(string name, uint priority, BaseProfile profile, IParentService parentService)

A service with multiple dependencies

public DependencyTestService2(string name, uint priority, BaseProfile profile, IServiceDependency1 dependentService1, IServiceDependency2 dependentService2, IServiceDependency3 dependentService3)

Only caveat, is that all parameters AFTER BaseProfile (or CustomProfile for services with a custom profile) MUST be inheritable from IService, else it will throw an error when it is registered.

@SimonDarksideJ
Copy link
Contributor Author

Will this always assume the first service is the parent service and following are dependencies?

Actually, I've not tested with with ServiceModules only base services.

Do we also need the injection to work on modules as well? Or should we complete this first and then look at modules, which "technically" already accept/expect a service as a parent as the next parameter.

@FejZa
Copy link
Contributor

FejZa commented Oct 22, 2024

I fear people will assume it works for modules as well. But I think for a first version we can omit it and keep it in the preview release until we have added modules as well

@SimonDarksideJ SimonDarksideJ changed the title Adding service dependency injection feature Adding service dependency injection feature for Services Oct 22, 2024
@SimonDarksideJ SimonDarksideJ merged commit 170fc5e into development Oct 28, 2024
27 checks passed
@SimonDarksideJ SimonDarksideJ deleted the feature/dependency-injection branch October 28, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants