luni, 21 decembrie 2009

Bianca - decembrie 2009 (aproape 10 luni)

Sunt un vierme. Asa imi zice mama. Cum imi da pampersul jos, cum o iau de-a sanatoasa, de-a busilea, prin pat. Atunci, trebuie, neaparat sa ma agat de orice, sa trag de toate lucrurile care imi ies in cale. Distractia se termina, cand mama imi pune pampersul cel nou, curatel.
Si tot viermisor sunt cand trag de toate firele din casa. Bag in gura si rup orice este facut din hartie.

Sunt o caragioasa. Pentru ca ingan. Bat din palme. Suflu pe nas odata cu tata. Plescai cand tata imi explica cum se mesteca. Ma dau pe parchet cu premergatorul. Vai peretilor din casa. Mananc singura biscutii, fructe, rod morcovi sau carnita.

Sunt o mofturoasa pentru ca fac scandal daca nu mi se face pe plac. Daca nu sunt lasata sa bag balute in telefoane sau in ceasuri, sa rod cablurile de la calculator sau sa stau pe masa de la calculator. Si de o bucate de vreme, adorm doar daca am suzeta. Problema mare.

Sunt un arici. Pentru ca ma imi plac reclamele asa de mult incat nimeni nu mai poate discuta cu mine in timp ce le vizionez.

Si musc cu putere. In primul rand pe Tigrisor. Apoi, paiul de la canita, degetele celor din jur si multe altele. Am, de toti, opt dintisori.

Si mai am opt, opt kilograme. Si aproape saptezeci de centimetri.

Mancarea preferata: branza cu smantana si cu mamaliguta. Yami, yam. Mananc si dovlegel, spanac, ficatei, carnita de pui, etc.

Beau cei si suculet singurica. Cu paiul din canita mea.
Am mingiute multe, de toate culorile. Le bag in gura si scot diferite zgomote. Le mai si bat cap in cap.

Cu baita, ne-am mutat in cada. Acolo bat apa cu palmutele, ma joc cu dusul si cu jucariile plutitoare.

A venit si iarna. A nins frumos afara. Mama ma imbraca gros cand iesim la aer. Nu-mi prea convine. Cizmuletele sau bocanceii sunt cam grei. Nu prea pot sa fug asa de repede cat asa dori eu. Si cam imi curge nasucul uneori. Dar m-am inzdravenit pana acuma destul de repede. Doar cu homeopate. Deci, sunt rezistenta si luptatoare.

Mami si tati au impodobit bradul. Multe luminite, globulete, beteala... Numai bune de tras de ele si, evident, de bagat in gura.

Dau cu piciorul in mingiute si fug dupa ele. Bineinteles, tinuta de manute, fie de tata, fie de mama. Joc prinselea cu tata: eu in premergator, tata pe jos. Si sunt mandra cand prind lucruri care cred ca nu am voie sa le iau. Dar, totusi le iau. Si-mi mai place sa ma care tata in spate. Daca mai si topaie, distractia e garantata.

Sunt destul de vorbareata. Stiu sa zic ma-ma, ta-ta-ta, ca, na-na-na si multe alte zgomote. Ma fac inteleasa, de obicei.

Ne vedem si luna viitoare sa va spun cum mai este.

luni, 14 decembrie 2009

[Design Pattern] Bridge Pattern

Bridge Pattern:

The contract can be used as a bridge within other classes, and it allows other classes to consume the concrete implementer without knowing anything about the details.

- separates the contract and implementation in two independent areas.
- use interface in C# to build contracts.
- contract will only provide the functionality.
- contract maintains a reference to an object of type Implementor
- the implementor defines higher-level operations. The implementor's interface doesn't have to correspond exactly to contract's interface

C# Sample:

















/// <summary>
/// ICommunicator is an interface for classes which want to be able to communicate to an collection of <see cref="IImplemetorContract"/>.
/// It has a list of methods that acts on the list of IImplemetorContract objects.
/// </summary>
public interface ICommunicator
{
List<IImplemetorContract> ImplementorsContract { get; }

string GetValues(DateTime i_date);
string GetFullName(int i_index);
void StartGetValues(int i_interval);
void StopGetValues();
}

/// <summary>
/// IImplemetorContract objects act as a bridge to let ICommnucator object consume it.
/// ICommnucator has alist of IImplemetorContract objects with which communicates.
/// It has a list of method which each IImplemetorContract class should implemnt and which should be used by ICommnucator methods
/// </summary>
public interface IImplemetorContract
{
object GetValue(DateTime i_dateTime);
}

/// <summary>
/// A more specific implementator for a Stat
/// </summary>
public interface IStat : IImplemetorContract
{
string Name { get; }
string Caption { get; }
Type Type { get; }
}

/// <summary>
/// Common implementation for all Communicators
/// </summary>
public abstract class AbstractCommunicator
{
private List<IImplemetorContract> m_listImplementorsContract = new List<IImplemetorContract>();
protected Timer _timer = null;
public virtual string GetValues(DateTime i_date)
{
Dictionary<IImplemetorContract, object> _setStatValues = new Dictionary<IImplemetorContract, object>();
StringBuilder _sb = new StringBuilder();
_sb.AppendLine("Communicator: " + GetType().Name + ". Values at time: " + i_date.ToLongTimeString());
foreach (IImplemetorContract _implementor in ImplementorsContract)
{
object _value = _implementor.GetValue(i_date);
_sb.AppendLine(GetFullName(ImplementorsContract.IndexOf(_implementor)) + "\t\t" + _value);
}
return _sb.ToString();
}

public abstract string GetFullName(int i_index);

public List<IImplemetorContract> ImplementorsContract
{
get { return m_listImplementorsContract; }
}

public virtual void StartGetValues(int i_interval)
{
_timer = new Timer(GetValuesNow, null, 1000 , i_interval);
}

public virtual void StopGetValues()
{
if (_timer == null) return;
_timer.Change(Timeout.Infinite, Timeout.Infinite);
_timer.Dispose();
}

protected virtual void GetValuesNow(Object statusInfo)
{
Console.WriteLine(GetValues(DateTime.Now));
}

}

public class CSVCommunicator : AbstractCommunicator, ICommunicator
{
#region ICommunicator Members

public override string GetFullName(int i_index)
{
if (i_index < 0 || i_index > ImplementorsContract.Count - 1)
{
throw new IndexOutOfRangeException();
}
if (ImplementorsContract[i_index] is IStat)
{
return ((IStat)ImplementorsContract[i_index]).Name + "/" + ((IStat)ImplementorsContract[i_index]).Caption;
}
else
{
return ImplementorsContract[i_index].ToString();
}
}

public override void StartGetValues(int i_interval)
{
Console.WriteLine("CSV is writing values");
base.StartGetValues(i_interval);
}

public override void StopGetValues()
{
Console.WriteLine("CSV is stopping writing values");
base.StopGetValues();
}
#endregion
}

public class GuiCommunicator : AbstractCommunicator, ICommunicator
{
#region ICommunicator Members

public override string GetFullName(int i_index)
{
if (i_index < 0 || i_index > ImplementorsContract.Count - 1)
{
throw new IndexOutOfRangeException();
}
if (ImplementorsContract[i_index] is IStat)
{
return ((IStat)ImplementorsContract[i_index]).Caption;
}
else
{
return ImplementorsContract[i_index].ToString();
}
}

public override void StartGetValues(int i_interval)
{
Console.WriteLine("GUI is displaying values");
base.StartGetValues(i_interval);
}

public override void StopGetValues()
{
Console.WriteLine("GUI is stopping displaying values");
base.StopGetValues();
}
#endregion
}
public class DiskFreeSpaceStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskFreeSpace"; }
}

public string Caption
{
get { return "C Drive Disk Free Space"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["FreeSpace"];
}

#endregion
}

public class DiskLabelStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskLabel"; }
}

public string Caption
{
get { return "C: Drive Disk Label"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["Name"];

}

#endregion

}

public class DiskSizeStat : IStat
{
#region IStat Members

public string Name
{
get { return "DiskSpace"; }
}

public string Caption
{
get { return "C: Drive Disk Space"; }
}

public Type Type
{
get { return typeof(int); }
}

#endregion

#region IImplemetorContract Members

public object GetValue(DateTime i_dateTime)
{
ManagementObject _disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
_disk.Get();
return _disk["Size"];

}

#endregion
}

class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
//create communicators
CSVCommunicator CSV = new CSVCommunicator();
GuiCommunicator GUI = new GuiCommunicator();

//create implementors contract
DiskFreeSpaceStat _statFreeSpace = new DiskFreeSpaceStat();
DiskLabelStat _statLabel = new DiskLabelStat();
DiskSizeStat _statDiskSize = new DiskSizeStat();

//CSV communicator is talking with _statDiskSize and _statFreeSpace
Console.WriteLine("\t\tCSV");
CSV.ImplementorsContract.Add(_statDiskSize);
CSV.ImplementorsContract.Add(_statFreeSpace);
Console.WriteLine("First set of values for CSV: \n" + CSV.GetValues(DateTime.Now));
Console.WriteLine();

//GUI communicator is talking with _statDiskSize, _statFreeSpace and _statFreeSpace
Console.WriteLine("\t\tGUI");
GUI.ImplementorsContract.Add(_statDiskSize);
GUI.ImplementorsContract.Add(_statDiskSize);
GUI.ImplementorsContract.Add(_statFreeSpace);
Console.WriteLine("First set of values for GUI: \n" + GUI.GetValues(DateTime.Now));

Console.WriteLine();
Console.WriteLine();

Console.WriteLine("GUI present values at 2 s / CSV is writing value to .csv file at 4 s for 20 s");
GUI.StartGetValues(2000);
CSV.StartGetValues(4000);

AutoResetEvent _autoEvent = new AutoResetEvent(false);

Timer _timer = new Timer(delegate(Object _state)
{
GUI.StopGetValues();
CSV.StopGetValues();
Console.WriteLine("Finishing getting values ");
_autoEvent.Set();
}, null, 20000, 1000);


_autoEvent.WaitOne();
_timer.Dispose();

Console.WriteLine("Bye, bye now. Press ENTER to Finish.");
Console.ReadLine();

}

static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine(e.ToString());
}
}

vineri, 11 decembrie 2009

[C#] [4.0] Custom Dynamic Object - Code Sample




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.Dynamic;

namespace ConsoleApplication1
{
class MyDynamicClass : DynamicObject
{
private Dictionary<string, object> _properties = new Dictionary<string, object>();
//TryGetMember and TrySetMember methods implement the logic to read or write a member of the dynamic object.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _properties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_properties[binder.Name] = value;
return true;
}

}

class Program
{

public static void UseDynamicObject(dynamic i_dynamic)
{
i_dynamic.Message = "Ala bala portocala!";

Console.WriteLine(i_dynamic.ToString());

i_dynamic.SayMessage1 = new Action(delegate {
Console.WriteLine(i_dynamic.Message);
Console.WriteLine("Say Message 1");
i_dynamic.ToString(); });
i_dynamic.SayMessage2 = new Action(
() =>
{
Console.WriteLine("SayMessage 2, the call SayMesage1");
i_dynamic.SayMessage1();
});
i_dynamic.SayMessage1();
i_dynamic.SayMessage2();
}

static void Main(string[] args)
{
dynamic _annonimus = new MyDynamicClass();
UseDynamicObject(_annonimus);
_annonimus.Message2 = "Hello from George";

Console.WriteLine(_annonimus.Message2 + ": " + _annonimus.Message);

Console.ReadLine();
}
}
}

joi, 10 decembrie 2009

[C#] Dynamically Generating an Assembly in Memory and using it

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.CSharp;

namespace GeorgeNamespace
{
public class Program
{
static void Main()
{

#region Dynamically Generating an Assembly in Memory
CodeCompileUnit _myProgram = new CodeCompileUnit();//create a program
CodeNamespace _ns = new CodeNamespace("MyNameSpace");//create a namespace
_ns.Imports.Add(new CodeNamespaceImport("System"));//<=> using System;
_myProgram.Namespaces.Add(_ns);//add namespace to program

CodeTypeDeclaration _myClass = new CodeTypeDeclaration("MyClass");//create class
_ns.Types.Add(_myClass);//add class to namespace

CodeEntryPointMethod _entryPointMethod = new CodeEntryPointMethod();//create an entry point method

_entryPointMethod.ReturnType = new CodeTypeReference(typeof(void));//set returned type

//Add Code to method
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "WriteLine",
new CodePrimitiveExpression("Hello, World!"));

CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "WriteLine",
new CodePrimitiveExpression("Press ENTER to finish!"));

CodeMethodInvokeExpression cs3 = new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("System.Console"), "ReadLine");

_entryPointMethod.Statements.Add(cs1);
_entryPointMethod.Statements.Add(cs2);
_entryPointMethod.Statements.Add(cs3);

_myClass.Members.Add(_entryPointMethod);//add entry method to class

CSharpCodeProvider _codeProvider = new CSharpCodeProvider();//create an instance of code generator/compiler
CompilerParameters _params = new CompilerParameters(null, "MyDynamicProgram", true);//set assemply name
_params.GenerateExecutable = true;//create an .exe

CompilerResults _results = _codeProvider.CompileAssemblyFromDom(_params, _myProgram); //compile the assembly

if (_results.Errors.Count > 0)//check for errors
{
foreach (CompilerError _ce in _results.Errors)
{
Console.WriteLine(string.Format("{0} : {1}", _ce.ErrorNumber, _ce.ErrorText));
}
}


//First sample of usage: generate a type on the fly/create a method and invoke it
if (_results.Errors.Count == 0 && _results.CompiledAssembly != null)
{
Type _objType = _results.CompiledAssembly.GetType( "MyNameSpace.MyClass" );
try
{
if (_objType != null)
{
MethodInfo _mi = _objType.GetMethod("Main");
_mi.Invoke(_objType, new object[] { });
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

//Second Sample: use AppDomain to load the assembly and to load it.
//AppDomain _appDomain = AppDomain.CreateDomain("MyNewAppDomain");
//_appDomain.ExecuteAssembly(_results.PathToAssembly);
#endregion

#region Dynamic method
ClassSample _sampleDynamicMethod = new ClassSample();
_sampleDynamicMethod.InvokeDynamicMethod();
#endregion

Console.ReadLine();
}
}

public class ClassSample
{
public delegate void MyDelegate();
public void InvokeDynamicMethod()
{
DynamicMethod _dynamicMethod = new DynamicMethod("TempMethod" + Guid.NewGuid(), null, null);
ILGenerator _il = _dynamicMethod.GetILGenerator();

_il.Emit(OpCodes.Ldstr, "Hello, World! From George!");
MethodInfo _methodWriteLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
_il.Emit(OpCodes.Call, _methodWriteLine);
_il.Emit(OpCodes.Ret);

//First way of calling the method
_dynamicMethod.Invoke(null, null);

//Second way of calling the method
MyDelegate _myDelegate = (MyDelegate)_dynamicMethod.CreateDelegate(typeof(MyDelegate));
_myDelegate();
}
}
}

vineri, 4 decembrie 2009

NUnit - basic

[TestFixture] attribute - applied to a class (has to be public and to have a default ctor)
[Test] attribute
- applied to methods indicating that it is a test method
- method returns void and has no parameter.
Assert class defines a collection of methods used to check the post-conditions
.AreEquals(object, object) -- the first parameter is an expected value and the second parameter is the actual value
[ExpectedException] attribute - this is the way to indicate that the test code is expecting an exception of a certain type; if such an exception is not thrown during the execution – the test will fail.
[Ignore] attribute - temporarily ignore a test method
[SetUp] attribute
- applied to methods
- it has void return type, no parameters
- put a common set of test objects to all test methods
- has the common initialization code

Steps to test with NUnit:
- Start the NUnit Gui
- Select the File->Open menu item, navigate to the location of your dll
- Click the Run button
- See the “Errors and Failures” panel
- GUI watches for the changes made to the test assemblies and updates itself when the structure of the test tree has changed – e.g. new test is added

miercuri, 2 decembrie 2009

[.NET] [4.0] Generic tuples classes

-> Microsoft .NET Framework introduces a new type called System.Tuple

Tuples are a group of properties that provide a means for you to easily group pieces of data together without having to write your own classes.
Or:
Tuple is a fixed-size collection of heterogeneously typed data.

-> You'll find tuples in languages such as Python and F#


Sample:
class Tuple
{
T1 Item1 { get; }
T2 Item2 { get; }
}

Tuple person = new Tuple ( "George", 28);
Console.WriteLine(person.Item1 + " " + person.Item2);

var _tuplePerson = Tuple.Create("George", 28);

-> This class allows you to group two different pieces of data of any type.

-> Tuples in .NET are considered immutable.
-> Like an array, a tuple has a fixed size that can't be changed once it has been created. Unlike an array, each element in a tuple may be a different type, and a tuple is able to guarantee strong typing for each element.
-> KeyValuePair is a sample of Tuple
-> All tuples are reference types (this may hit the performance).
-> Can have till 8 generic parameters
-> The last element of an eight-element tuple would be called "Rest" and it is required to be a tuple.
Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8));

-> Both Tuple and Array now implement these interfaces explicitly: IStructualEquatable and IStructuralComparable

One problem with tuples is your code becomes less descriptive, because tuples employ non-descript property names such a person.Item1 and person.Item2.