Thursday, November 20, 2014

... Public static void Main (string [] args) {var logger = new FileLogger mol container tracking (


Introduction. In this article we will see how the changes affect the code Windsor Container settings mol container tracking and see how it would affect the case of not using toilet. Interfaces. The first changes will be to create an interface for the summer class.
public interface ISumador {decimal sum (decimal num1, num2 decimal); } Public class Adder: ISumador {public decimal Sum (num1 decimal, decimal num2) {return num1 + num2; }} ... Public static WindsorContainer miContenedor; public static void Main (string [] args) {// Create the container = new WindsorContainer miContenedor (new XmlInterpreter ("MiContenedor.xml")); mol container tracking // We call an object of type adder to adder var container = miContenedor.Resolve <ISumador> (); sumador.Suma var result = (1,1); } ... When generating the interface instead request the container us back an object of type adder, we ask an object that implements the interface adder. For this we modify the configuration file WC, adding the attribute service to adder component.
<Configuration> <components> <component id = "Adder" type = "IoC.Tutoriales.Part2.Sumador, IoC.Tutoriales.Part2" service = "IoC.Tutoriales.Part2.ISumador, IoC.Tutoriales.Part2" /> < / components> </ configuration> With this by asking mol container tracking an object mol container tracking that implements the interface ISumador, the container will look for the first component mol container tracking that implements this interface and return an object of class for that component. In our case it will seek a component that implements the interface ISumador, the component with the id "Adder" and generate an object of type "IoC.Tutoriales.Part2.Sumador". By making mol container tracking these changes now if we ask the container object Adder will fail because the rates prevailing on the service type attribute types. Dependencies among components. Supogamos that every time we perform a sum a log is saved in a file. First we write our class that records the log:
public interface ILogger {void Debug (string mol container tracking text); } Public class FileLogger: mol container tracking ILogger {private string fileName; public FileLogger (string fileName) {fileName = this.fileName; } Public void Debug (string text) {// Save the text in the file fileName ....}} The changes in the Adder class would type:
public class Adder {private ILogger mol container tracking logger; public Adder (ILogger logger) {this.logger = logger; } Public decimal Amount (decimal num1, num2 decimal) {logger.debug (string.Format ("Adding {0} + {1}", num1, num2)); return mol container tracking num1 + num2; }} For an architecture of this type without using toilet we might as pirmero FileLogger create an object of type and pass it to the constructor of the Adder object.
... Public static void Main (string [] args) {var logger = new FileLogger mol container tracking ("log.txt"); var adder = new Adder (logger); sumador.Suma var result = (1,1); } Using Windsor Container all you have to change is the configuration file as follows:
<Configuration> <components> <component id = "Adder" type = "IoC.Tutoriales.Part2.Sumador, IoC.Tutoriales.Part2" service = "IoC.Tutoriales.Part2.ISumador, IoC.Tutoriales.Part2" /> < component id = "Logger" type = "IoC.Tutoriales.Part2.FileLogger, IoC.Tutoriales.Part2" service = "IoC.Tutoriales.Part2.ILogger, IoC.Tutoriales.Part2"> <parameters> <fileName> C: \ log.txt </ fileName> </ parameters> </ component> </ components> </ configuration> The Main method does not have to modify it and stay as we had:
... Public static void Main (string [] args) {// Create the container = new WindsorContainer miContenedor (new XmlInterpreter mol container tracking ("MiContenedor.xml")); // We call an object of type adder to adder var container = miContenedor.Resolve mol container tracking <ISumador> (); sumador.Suma var result = (1,1); } When making a request, looking at the Windsor Container container component that fulfills that request, once found look for that component dependencies in the container, and so on, if any of the dependencies can not be satisfied Windsor Container throws an exception . In our case encontarĂ­a the component with id "Adder" mol container tracking which is of type "IoC.Tutoriales.Part2.Sumador" this class has a dependecia with "IoC.Tutoriales.Part2.ILogger" class, so if you look inside the container there is a component that satisfies this dependecia, and find the comoponente with id "Logger". So that to create an object of type "IoC.Tutoriales.Part2.Sumador" first create an object mol container tracking of type "IoC.Tutoriales.Part2.FileLogger". A small refactoring. Suppose now that instead of saving the log to a file so we must keep in database. The first would be to generate the classes needed to store log in database:
public class DBLogger: ILogger {private stri

No comments:

Post a Comment