c# - How to inject dependencies into an asmx webservice webmethod? -


i have asmx webserice method

[webmethod] double getbookprice(int bookid) {     //instantiates discountservice,deliveryservice , couple of other services      //uses various methods of these services calculate price     //e.g. discountservice.calculatediscount(book)  } 

there 4 services dependencies of method.

how test method? need inject these dependencies? or should ? client sending int check price.

thanks

if getbookprice method doing taking int , passing along method, returning results, i'd testing service method of dubious value. doesn't hurt test it, , have value if want expand functionality in getbookprice method down line.

in general, though, if wanted test service, you'd standard ioc via constructor injection , constructor chaining:

public class foowebservice {     private readonly isomedependency dependency;     public foowebservice(isomedependency dependency)     {         //this call during testing         this.dependency = dependency;     }     public foowebservice() : this(new concretedependencyimplementation())     {     } } 

when you're testing, pass in instance of dependency (or dependencies!). when you're not testing, dependency automatically created , provided way of calling parameterless constructor.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -