Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ha s a real worl case where i needed dependency injection :</p> <p>2 assembly : 1 responsible for computing a price when you do a search (lets call it Search), and the second responsible for computing a booking price with all the options (let's call it Booking).</p> <p>The Booking assembly is already referencing Search(because it needs to know the initial price for computing the full price).</p> <p>But here is the requirement : we need the price from Search to include all mandatories options (yes in the tourism industry you have mandatories options) like "full room cleaning".</p> <p>So I couldn't had a ref to Booking in Search (because of circular reference). So I decided use dependency injection.</p> <p>My Search assembly defined an interface</p> <pre><code>public interface IAddMandatoryOptionService{ void ChangeResultsWithMandatoryOptions(SearchResult[] results); } </code></pre> <p>And then my Booking Assembly could implement this interface.</p> <pre><code>public class AddMandatoryOptionService : IAddMandatoryOptionService{ public void ChangeResultsWithMandatoryOptions(SearchResult[] results){ ... } } </code></pre> <p>My SearchService class now would look like</p> <pre><code>public class SearchService{ public SearchService(IAddMandatoryOptionService optionService){ this.OptionService = optionService; } public SearchResult[] Search(Filter filter){ ... this.OptionService.ChangeResultsWithMandatoryOptions(results); ... return results; } } </code></pre> <p>So my Search Service has no dependency to the AddMandatoryOptionService class (and the Booking assembly), but it's using its functionality.The good IAddMandatoryOptionService will be injected when I create my service (in my Application_Start or with a DI Framework)</p> <p>The advantages are :</p> <ul> <li>Resolving the circular reference problem.</li> <li>If I want to unit test my SearchService I'll juste have to mock/fake IAddMandatoryOptionService.</li> </ul> <p>Here the need for injection was more technical than logical, but I think this kind of real world scenario could help you to get the point.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload