IoC Factory: Pros and contras for Interface versus Delegates

Any place where you need a run-time value to construct a particular dependency, Abstract Factory is the solution. I would argue against this. Dependencies should not be constructed using runtime data, as explained here. In summary the article states: Don’t inject runtime data into application components during construction; it causes ambiguity, complicates the composition root … Read more

How to create a child scope from the parent with default dependency injection in .NET Core?

Use IServiceProvider.CreateScope() method to create a local scope: var services = new ServiceCollection(); services.AddScoped<ExampleService>(); var globalProvider = services.BuildServiceProvider(); using (var scope = globalProvider.CreateScope()) { var localScoped = scope.ServiceProvider.GetService<ExampleService>(); var globalScoped = globalProvider.GetService<ExampleService>(); } It can be easily tested: using (var scope = globalProvider.CreateScope()) { var localScopedV1 = scope.ServiceProvider.GetService<ExampleService>(); var localScopedV2 = scope.ServiceProvider.GetService<ExampleService>(); Assert.Equal(localScopedV1, localScopedV2); var … Read more

Validation: How to inject A Model State wrapper with Ninject?

The solution given by that article mixes validation logic with the service logic. These are two concerns and they should be separated. When your application grows you will quickly find out that validation logic gets complicated and gets duplicated throughout the service layer. I, therefore, like to suggest a different approach. First of all, it … Read more

Arguments against Inversion of Control containers

There are certainly people who think that DI Containers add no benefit, and the question is valid. If you look at it purely from an object composition angle, the benefit of a container may seem negligible. Any third party can connect loosely coupled components. However, once you move beyond toy scenarios you should realize that … Read more

How does Unity.Resolve know which constructor to use?

When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the … Read more

How to registerType with a PARAMETER constructor?

It’s simple. When you register the constructor, you just pass the value you want injected for the parameter. The container matches up your constructor based on the type of value (API) or name of parameter (XML). In the API, you’d do: container.RegisterType<MyType>(new InjectionConstructor(“My string here”)); That will select a constructor that takes a single string, … Read more

Microsoft Unity. How to specify a certain parameter in constructor?

Your best answer is to actually use the container. What you’re doing is saying “when building this type, use this specific instance of the object.” This doesn’t take advantage of the ability of the container to build up instance for you. Instead, you should register IService1 and IService2 in the container. Then, tell the container … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)