Let’s explain with some code:
I set the implementation of the interface I want in the web.config:
<appSettings> <addkey="WatzdPriceDALCType" value="com.bekijkhet.WatzdPrice.DALC.PGsql.WatzdPriceDALC, WatzdPriceDALCPGsql" /> </appSettings>
Then I use the following code to inject the Data Access Layer I specified in the web.config:
public class WatzdPriceBLL : IWatzdPriceBLL { private IWatzdPriceDALC _DALC; public WatzdPriceBLL() { Type typeWatzdPrice = Type.GetType(ConfigurationManager.AppSettings["WatzdPriceDALCType"].ToString()); _DALC = (IWatzdPriceDALC)Activator.CreateInstance(typeWatzdPrice); } public WatzdPriceBLL(IWatzdPriceDALC watzdpriceDALC) { _DALC = watzdpriceDALC; } ... }
This will inject the class from the web.config when the Data Access Layer is created without parameters. For unit testing we can call the constructor with the paramter to inject our mock object.