Tag:. NET
Pattern Factory (Factory)
by Jose Jorge Marquez Gomez to ago.27, 2010, under DoNet
A factory pattern serves to channel all creations of objects in a single point. This is useful for robust design of our system and responsive to change.
The factory pattern can be combined with the singleton pattern as it is more comfortable to use and useful form to use. In this example we will put a factory without singleton.
To say the factory pattern is of the creational patterns, this type of pattern that abstracts the way to create concrete objects, thus giving support to create objects in the abstract.
To start a factory pattern is to perform:
- A function or method which is responsible for creating objects of a given type.
- It is not mandatory but recommended that the function or method returns an abstraction (interface or abstract class).
Imagine a pizza shop requires a software you need to have a number of products (pizzas varied). As designers we are good, the pizza object creation can be done from anywhere in the project, but the problem is that there are many types of pizzas. To create each pizza for each type in different parts of the program would be the first thing you would think, but ... if instead we use common sense and apply the factory?, This would allow us as a new product or change in any of them that only change in one place, taking more controlled this code. Even if you realize the speed of change is much more to go changing all possible appearances of our pizzas by the code.
Well, the factory would be responsible for creating all kinds of pizzas and offer all possible points of the program.
Our example would be:
- The interface IPizza:
IPizza interface
{
WhoAmI void ();
}
- The individual pizzas:
public class PizzaBarbacoa: IPizza
{
# Region Members IPizza
public void WhoAmI ()
{
Console.WriteLine ("I am a BBQ pizza");
}
# Endregion
}
public class PizzaMargarita: IPizza
{
# Region Members IPizza
public void WhoAmI ()
{
Console.WriteLine ("I am a pizza Margarita");
}
# Endregion
}
public class PizzaPepperoni: IPizza
{
# Region Members IPizza
public void WhoAmI ()
{
Console.WriteLine ("I am a pizza Pepperoni");
}
# Endregion
}
- The plant in question:
public class PizzaFactory
{
public enum tipoPizza
{
Pepperoni, Margherita, Barbeque
}
IPizza public getPizza (tipoPizza type)
{
switch (type)
{
tipoPizza.Barbacoa case:
return new PizzaBarbacoa ();
break;
tipoPizza.Margarita case:
return new PizzaMargarita ();
break;
tipoPizza.Pepperoni case:
return new PizzaPepperoni ();
break;
default:
return new PizzaBarbacoa ();
}
}
}
- A little test:
public class Test
{
public void testPizza ()
{
/ / Example not using factory
IPizza pizza1 = new PizzaBarbacoa ();
pizza1.quienSoy ();
IPizza pizza2 = new PizzaMargarita ();
pizza2.quienSoy ();
IPizza pizza3 = new PizzaPepperoni ();
pizza3.quienSoy ();
IPizza pizza4 = new PizzaBarbacoa ();
pizza4.quienSoy ();
IPizza pizza5 = new PizzaMargarita ();
pizza5.quienSoy ();
/ / Example using factory
PizzaFactory PizzaFactory factory = new ();
IPizza pizza6 = factory.getPizza (PizzaFactory.tipoPizza.Barbacoa);
pizza6.quienSoy ();
IPizza pizza7 = factory.getPizza (PizzaFactory.tipoPizza.Margarita);
pizza7.quienSoy ();
IPizza pizza8 = factory.getPizza (PizzaFactory.tipoPizza.Pepperoni);
pizza8.quienSoy ();
}
}
As you can see how to use and its advantages are better. I hope you like!. See you later!
Log4Net for C #
by Jose Jorge Marquez Gomez to ago.25, 2010, under DoNet
Well today I'll tell you a little how to configure log4net for c #. But before I tell you that is log4net.
Log4Net is a framework log4java ported the library, which provides all the tools possible to make a logging, error handling, monitoring all your applications for possible applications. In my view, the log is as good as I've tried so far and is quite easy to configure. Offers several monitoring modes and even create you your own. That bookstore you can get http://logging.apache.org/log4net/index.html .
Explain some concepts related to log4net.
Log4Net offers multiple levels of monitoring predefined as:
1. Debug: This is usually used for debug lines.
2. Info: It is often used to give information relating to your application.
3. Warn: breakpoints are possible of your software, I mean, any important alerts.
4. Error: Used to detect errors in your software.
5. Fatal is used to detect significant errors in software, ie to prevent the operation thereof for example.
These levels are predefined brings log4net, but you can create custom levels.
Another highlight is the appenders, this is just how different levels of log4net will be monitored / logged in log4net there these include:
1. FileAppender: logs on the level defined in the configuration file.
2. RollingFileAppender: logs on the level defined in the configuration in a file that rotates depending on their weight.
3. SmtpAppender: logs on the level defined in the configuration and is sent to a mail recipient.
Later on you will see an example of setting these levels configured. For details of all properties you can see the web appenders you have below.
All you can see detailed on this web http://logging.apache.org/log4net/release/config-examples.html .
Begin.
The first step is descargaros library the previous website, once downloaded, you must add this library to your project references. Then we'll set the log4net in the configuration file of your application, if a Web application is the web.config, if the app.config console application, etc ...
In the tag "configuration" -> "ConfigSection" -> agregad the following line:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821"/> Then a new tag that we called "<log4net>" the appenders are configured there. Each has its own settings in the previous website you can see all. Deputy log4net Example
Once we have this, you already use our log4net in your application. A requirement to be met is that each class must initialize log4net in its constructor so that it is as illustrated in the following code:
class test { Ilog private log; public test () { log4net.Config.XmlConfigurator.Configure (); log = LogManager.GetLogger (this.GetType ()) / / here we proceed to initialize the object log. }
To make use of our log is simple, we just call the different levels of alert that has through the object log.
I hope you serve as a seed with log4net.
Greetings!






