Bannerlord-Coop-Team / BannerlordCoop

MIT License
261 stars 51 forks source link

Use dependency injection framework #119

Closed ignaciofernandezsoto closed 2 years ago

ignaciofernandezsoto commented 3 years ago

It'd make our classes easier to test, as we wouldn't have static methods nor static classes.

Mocking classes/methods would be so much easier.

If we'd like to test a class, say class A, that calls static method from Bannerlord, we then can wrap that Bannerlord's class in one of our own to test said class A.

There's investigation to be done regarding which DI framework it's best for our needs

araex commented 3 years ago

I do not have any experience with C# DI frameworks. Do i understand correctly that this could rewrite CIL at runtime in order to be able to mock classes without an interface?

While that would be really useful, i wonder if that wouldn't conflict with our patches. There would have to be a well defined order of patching. It might be doable trough harmony, that is what the library is for. But that would require the DI framework to use harmony as well which likely isn't the case.

Do you think this is doable?

ignaciofernandezsoto commented 3 years ago

I may've expressed my thoughts wrongly, I'll put it in another way.

We have a lot of static classes and static methods, which can't be tested on because mocking them is from quite painful to not doable. Hence why I didn't test functionality added in #120.

With a DI framework we'd be able to have singletons (not to confuse with _singleton pattern_), which they are testable.

This framework should not collide with harmony lib for its purposes differ.

I can do a spike for this technical feature and do a PR if it's ok, and then we can take it from there 😄. What do you think?

I'm sorry for the confusion!

araex commented 3 years ago

Ah ok, i misunderstood. Sure, if you want to you can go ahead and give it a shot.

thomasDelaporte commented 2 years ago

I would like to raise this issue because it is interesting, an IoC framework could allow us to follow a better structure and force us to respect it (which would benefit the conventions and the cleanliness of the code globally).

I've been reading up on some existing .NET frameworks, for example these ones:

More research would probably be necessary to see if there is a real use of this kind of pattern in our case and if it will not cause us more problems.

If someone has more knowledge on the subject it would be great to share it here.

garrettluskey commented 2 years ago

We are now using autofac