Dependency injection in Xunit project

You can implement your own service provider to resolve DbContext. public class DbFixture { public DbFixture() { var serviceCollection = new ServiceCollection(); serviceCollection .AddDbContext<SomeContext>(options => options.UseSqlServer(“connection string”), ServiceLifetime.Transient); ServiceProvider = serviceCollection.BuildServiceProvider(); } public ServiceProvider ServiceProvider { get; private set; } } public class UnitTest1 : IClassFixture<DbFixture> { private ServiceProvider _serviceProvider; public UnitTest1(DbFixture fixture) { _serviceProvider … Read more

DbContext for background tasks via Dependency Injection

You should pass IServiceScopeFactory instance (it’s singleton) into your task. Inside task, when data arrives, you should create new CreateScope() and request services from that scope. When data process finishes – dispose this scope (but hold reference to IServiceScopeFactory for next run). See this for example. I run small and fast tasks with this library. … Read more

Web Api Start up Exceptions with IDependencyResolver implementation

I was running in to the same issue using Unity with WebApi and OWIN/Katana. The solution for me was to use the UnityDependencyResolver defined in the Unity.WebApi Nuget package instead of my own custom implementation (like @Omar Alani above) Install-Package Unity.WebAPI Note that the package will try and add a file named UnityConfig.cs in App_Start … Read more

Unity: Replace registered type with another type at runtime

The method RegisterInstance of the UnityContainer will always override the last registration entry if you do not distinguish them by name. So if you call container.RegisterInstance<IHardware>(new HardwareB()); you will override the registration for the interface IHardware and will retreive HardwareB on the next resolving attempt

Meaning of bean discovery mode annotated in CDI 1.1

When using bean-discovery-mode=”annotated” only classes with a bean defining annotation are discovered. All other classes are ignored. Any scope type is a bean defining annotation. If a scope type is declared on a bean class, then the bean class is said to have a bean defining annotation [spec]. The 1.1 spec is not completely clear … Read more

Using Dependency Injection frameworks for classes with many dependencies

Well, while you can do this as described in other answers I believe there is more important thing to be answered regarding your example and that is that you are probably violating SRP principle with class having many dependencies. What I would consider in your example is breaking up the class in couple of more … Read more

Life-cycle methods for services in angular2 [duplicate]

Injectables are just normal classes (normal objects) and as such, they have no special lifecycle. When an object of your class is created, the class’s constructor is called, so that’s what your “OnInit” would be. As for the destruction, a service does not really get destroyed. The only thing that might happen is that it … 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

Using Dependency Injection with .NET Core Class Library (.NET Standard)

You don’t have to do anything in your class library. Only the main application has a composition root (earliest point in an application lifecycle you can set up your object graph). This happens in Startup.cs in your ASP.NET Core application. There you also register your dependencies: services.AddScoped<IUserManager,UserManager>(); That’s it. Class libraries don’t have a composition … Read more

Where exactly is the difference between IoC and DI [duplicate]

In common usage, the terms have become somewhat synonymous. The original idea of IoC — Inversion of Control — was very much related to the “Hollywood Principle:” Don’t Call Us, We’ll Call You. In traditional applications, developers would write business code and framework code. The business code would then call the framework code to accomplish … Read more

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