c# - Using constructor injection when caller expects a specific constructor signature -
i'm new di in .net c# & autofac , having problems understand how use di when can't control caller side.
there 2 scenarios have problems understand.
scenario 1: caller expects default constructor (without parameters)
how handle scenario when still want inject service interfaces when class constructed? thinking of constructor chaining, mean have know concrete type , works around idea of di. (at least think).
public class serviceworker { iservice _service; public serviceworker(iservice service) { _service = service } } public class caller { // no way change this. var serviceworker = new serviceworker(); }
scneario 2: caller expects specific constructor signature (e.g.
same question here. how can inject additional dependencies when caller expects exact match constructor signature?
i think main issue in understanding concept is, don't see how di partially when not constructed di (caller)
public class serviceworker { iservice _service; public serviceworker(string name, string id, iservice service) { _service = service } } public class caller { // no way change this. var serviceworker = new serviceworker(name, id); }
i know, pretty basic, believe need understand first before moving on. there alternatives?
Comments
Post a Comment