dependency-injection

How do I test Guice injections?

ぃ、小莉子 提交于 2019-12-17 21:56:32
问题 I gave to Google Guice the responsibility of wiring my objects. But, how can I test if the bindings are working well? For example, suppose we have a class A which has a dependence B . How can I test that B is injected correctly? class A { private B b; public A() {} @Inject public void setB(B b) { this.b = b } } Notice that A hasn't got a getB() method and I want to assert that A.b isn't null . 回答1: For any complex Guice project, you should add tests to make sure that the modules can be used

IoC in class library. Where to bootstrap

試著忘記壹切 提交于 2019-12-17 21:53:19
问题 I'm using a class library that can be reused by other components. In this class library I'm using unity for dependency injection. For this class library I create a test project. The caller also gets a test project. One thing I'm uncertain about is the location of the bindings. Should I incorporate this in the class library or should I do this from the calling application? 回答1: This is an interesting problem. How can you dependency inject re-usable assemblies that do not have an entry point. I

Autofac lazy property injection

心不动则不痛 提交于 2019-12-17 21:23:33
问题 I'm trying to inject business logic implementations into web API base controller. Somehow property in base controller is always null . Also how can I do lazy injection? Startups.cs public IServiceProvider ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType<ViewBusinessLogic>().As<IViewBusinessLogic>(). PropertiesAutowired(); containerBuilder.Populate(services); var container

C# - Copy dlls to the exe output directory when using dependency injection with no references?

笑着哭i 提交于 2019-12-17 20:28:33
问题 I have a C# solution that I am using dependency injection to resolve references between dlls. I have an exe project and some other dll projects that are not referenced by the exe (It uses the dlls through the IoC container). The project settings are the default, visual studio settings where it builds each dll in it's own folder. Since the exe doesn't reference the dlls, they never get copied to the output directory of the exe and don't get found by the IoC framework. How do you handle this?

'Autofac Circular Component Dependency Detected' Error

北慕城南 提交于 2019-12-17 20:24:08
问题 I am new to IoC and am using Autofac in my current project. I have the following 2 classes: public class UserService : IUserService { private readonly IUserRepository _repo; private readonly IMailService _mailService; public UserService(IUserRepository repo, IMailService mailService) { _repo = repo; _mailService = mailService; } } public class MailService : IMailService { private readonly IMailRepository _repo; private readonly IUserService _userService; public MailService(IMailRepository

How to make Jersey work with Dagger dependency injection?

流过昼夜 提交于 2019-12-17 20:18:05
问题 Jersey normally uses HK2 dependency injection, but I would like to use Jersey with Dagger 2. Both Dagger and HK2 implement JSR 330, which I have taken as evidence that this should be possible without too much effort. I found ways to make Jersey work with CDI (e.g. Weld), Spring DI and Guice, but I can't find anything on Dagger. To provide some context: I'm running a Grizzly–Jersey server in an SE environment, not in an EE container. My Maven project has com.google.dagger:dagger and org

Pass runtime value to constructor using Simple Injector abd WebFormsMVP

删除回忆录丶 提交于 2019-12-17 20:13:18
问题 I'm trying to combine SimpleInjector with WebFormsMvp . To facilitate DI WebFormsMvp provides the IPresenterFactory interface. It contains the Create method which provides the presenter type to resolve and the view instance . I need to inject the view instance into the constructor of the presenter . The presenter also has other dependencies that need creating by the container . This is what I got so far, but it is not ideal. What's the correct solution for the problem? Presenter constructor:

Dependency Injection - use with Data Transfer Objects (DTOs)?

寵の児 提交于 2019-12-17 20:11:06
问题 Consider the code below (which has been simplified). I have a service class that returns a list of specific DTO objects that each implement their own specific interface. In the actual code these are getting populated by iterating thru a Dataset as I'm working with legacy code. Questions: How do we create/use a DTO without newing them up or using the Service Locator anti-pattern? It doesn't make much sense to compose an empty DTO object in the Composition Root and inject it into the Service

What are the best practices for class libraries using dependency injection for internal operations? [duplicate]

戏子无情 提交于 2019-12-17 20:10:03
问题 This question already has answers here : Dependency Inject (DI) “friendly” library (4 answers) Closed 3 years ago . What should I be careful about when building a class library complex enough to use internal dependency injection? Assuming that it will use Castle Windsor (as an example), what would be the best place/method to configure the container, given that the library will be used by simple console application (with no DI), web forms using the same container (Castle Windsor), and web apps

Bootstrapping Unity - Composition Root location

我只是一个虾纸丫 提交于 2019-12-17 19:58:09
问题 I have a simple .NET project and I am wondering what's the best approach for bootstrapping Unity. I started with a WebApp with a bunch of controllers. Each of these controllers has its own Handler class to which the controller delegates the implementation. Something in the lines of: public class UsersHandler : IUsers { IAuthenticationClient authenticationClient; ILogger logger; public UsersHandler(IAuthenticationClient authClient, ILogger logger) { ... } } In the Application_Start method of