I recently joined a company that is a C#/.NET shop. I have been working primarilyc in the JVM ecosystem for backend through most of my professional work, so this is quite a big of change.
The language different is minor, but re-learning the ecosystem is quite another story.
Let us start with Dependency Injection / Inversion of Control Container. There are quite a bit few to choose from:
From my few conversation with developers coming from the .NET ecosystem, the default answer is always to go with the "official" Microsoft provided solution. So that is what I did.
Perhaps the documentation is written for people completely new to programming. For those with a Java background and are familiar with the concept of DI and IoC, here is my take of a quick start guide.
Use Host from the Microsoft.Extensions.Hosting namespace and build your IHost
IHost host = Host.CreativeDefaultBuilder()
.ConfigureServices(
( _, services ) =>
services.AddSingleton<MyInterface, MyImplementationClass>()
.AddSingleton<NextInterface, NextImplementationClass>()
// ... ...
);
For thosse coming from the Spring world, this is approximately equivalent to:
@Configuration
public class MyConfig {
@Bean
public MyInterface myClass() {
return new MyImplementationClass();
}
}
Or Guice if you come from Guice:
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MyInterface.class).toInstance(new MyImplementationClass());
}
});
Spring Boot take this a step further with its @ComponentScan and @Component magic. Does .NET have something similar? I do not know, if you do, please drop me a comment!
No comments:
Post a Comment