Closed kondratov-saritasa closed 1 year ago
I need to think about your suggestion,because changing it may break existing code.
The features of my service container are simple, if you need a powerful container, you can integrate the third-party Ioc/DI container.In the question you mentioned, you can inherit the IServiceContainer interface and customize your own ServiceContainer,or you can integrate Zenject into your project.
There is another way, that is, you can customize the name for your service, use the following method:
ServiceContainer.Register<T>(string name, T target);
container.Register("serviceName1",service1);
container.Register("serviceName2",service2);
@vovgou That is what I did already. But I decided to report in hope it might be resolved sometime.
Regarding breaking something - I think this unlikely will happen unless you save registered services somewhere (like serialize to file) or may be lookup for services manually.
Also as an option we could have different strategy for generic classes and keep current code for usual classes.
I will modify it in the next version.
Steps to reproduce:
Service<T>
Service<Type1>
Service<Type2>
Result:
DuplicateRegisterServiceException
is thrown.Expected: All works fine.
Here is a source of the problem: https://github.com/vovgou/loxodon-framework/blob/master/Loxodon.Framework/Assets/LoxodonFramework/Runtime/Framework/Services/ServiceContainer.cs#L72
typeof(T).Name
forService<Type1>
Service<Type2>
will return same string:Service'1
.My suggestion is to use
typeof(T).ToString()
then we get something like:Service'1[Type1]
andService'1[Type2]
. If you ok with that - I can prepare a PR.