c# - Dependency Injection resolve and unit testing -
I am trying to learn dependency injection and came into a problem when the unit tests the test.
I am writing a console application and have been created and started in the container main (), it is available as a get-property program in . Container anywhere, my application can call the program. Container. Resolve & lt; .. & gt; () .
I have a ServiceValidator class like:
Public Class Service Provinces (Private Readonly IC configuration _configuration; Private Readonly IService _service; Public ServiceWidder (IConfiguration Configuration, IService Service) { _configuration = Configuration; _service = service;} In the second class I
ServiceValidator serviceValidator = Program.Container.Resolve ( ); ServiceValidator.VerifyVersion (); This is a call to program.Container.Resolve , which gives me a unit test Problems arise because it has not been setup.
Is there a bad habit to solve the container? I can create a service example in main and the object I can pass from around, but it seems stupid as it causes a lot of parameters for the object which just gets around the next method.
So I think it Sweep to solve within a class Ary, but then the container must be configured for the unit test. How should I do this, should I take the container from programmable to second place? What would you recommend?
If it matters, then I am using Unity and C #.
Thanks: -)
Is this a bad practice, to fix the container? I can make serviscript examples in main () and pass the object all around, but it seems stupid because it causes a lot of parameters for those objects which are just around the next method.
When you use dependency injection all the way, you will not need to pass a lot of parameters for the object. The constructor of each object should only be in the form of dependencies, which use it directly - it does not know about the gross dependence of its direct dependencies.
So if you have a class X for which a service applicant, then the type of Constructor parameter near Class X will be ServiceValidator. So if some square uses Y square X, then there will be a constructor parameter of Type X in class Y. Notice that Y do not know anything about ServiceValidator, so you do not need to pass the service provider to one class second - the only place where it is used, when creating X , And it is often done only in one place by the De Framework or in a hand-written factory.
Some links for more information:
- - Your question about passing the object begins at 19:20 "Myth About D "
Comments
Post a Comment