tirsdag den 13. november 2012

Hello layered programming

Groft sagt gør alle applikationer det samme

  • Modtager bruger data
  • Behandler data/Gemmer data
  • Returnerer behandlede data
Så endnu simplere har vi en brugergrænseflade og en behandlingsflade. 
Brugergænsefladen kan være forskellige flader:
  • Windows form applikation
  • Web applikation
  • Mobil applikation
  • Powershell script
  • Kommando prompt
Behandlingsfladen kan ligeledes deles op i forskellige flader:
  • Funktionsflade
  • Filflade
  • Databaseflade
  • Webserviceflade 
og for at man kan genbruge fladerne bedst muligt, deler man det op i mindre dele/byggeklodser, som kan bruges i de forskellige sammenhænge. Det svarer til at man har stikkontakter i huset, som kan bruges til forskellige elektriske enheder. 

I udvikling (i windows) kan man gøre det med klassebiblioteker. 

Lav et klassebibliotek

Lad os starte med at lave et simpelt beregningsbibliotek. (Nyt projekt, Windows C#, Class Library)
Det generer et projekt med en Class.cs fil. Start med at ændre filnavnet til Calculator.cs, hvilket også ændrer navnet på klassen. 

Vi vil gerne have de 4 grundlæggende regneformer. 
  • Addition +
  • Subtraktion -
  • Division /
  • Multiplikation *

så vi starter med at tilføje en funktion, der kan addere 2 decimaltal
        public decimal Add(decimal value1, decimal value2)
        {
            return value1 + value2;
        }

Vi kan jo ikke se hvordan det virker, så vi starter med at lave et ekstra projekt - en Console Application. (Højreklik på solution og vælg Add > New Project)
Her skal vi have tilføjet en reference til Calculator projektet, så højreklik på References og vælg Add Reference... og under fanen Projects vælges Calculator projektet. 
Derefter højreklikkes på ConsoleCalc projektet og Select as Startup Project, da man ikke kan køre et klasse bibliotek. 

I program.cs skal vi lægge kode ind til at kalde vores calculator funktioner, en meget simpel udgave er her. 

            Calculator.Calculator calc = new Calculator.Calculator();

            decimal value1 = decimal.Parse(Console.ReadLine());

            decimal value2 = decimal.Parse(Console.ReadLine());

            Console.WriteLine(calc.Add(value1, value2));

            Console.ReadKey();

Bemærk: Der er INGEN fejlhåndtering i koden. 
Tryk F5 for at køre programmet og prøv at indtaste nogle værdier. 
Nu kan du tilføje de resterende funktioner til koden for at kunne bruge regneformerne. 

tirsdag den 21. februar 2012

Hello Powershell xml

I remember when I started looking at xml and I thought that I had to write a parser myself. Then I started looking at mssql which made it a lot easier. But it still had some bugs. Then I tried powershell and xml. This is quite smart.

Lets see. To read a text file, we need to call the Get-Content. This returns an object and by putting the [XML] in front of the returned object, we can use it as a structure of objects. Lets see, first we'll make an xmlfile with a couple of services in them:



wininit explorer



Let's just read the information on the process, so we read the xml file and gets the process info for each service in the xml-file


[xml]$xmlFile=Get-Content "D:\temp\test.xml"
$xmlFile.Services.Service | ForEach-Object {     Get-Process $_ | Format-List }


Voila, we now have read an xmlfile and tested the processes. But what if we want to write the Ids back to an xmlfile.. 

Lets see - first we make an xml template of what we want. 







and lets use the xml in another way.. just for fun, you can still use [XML]


cls
[xml]$xmlFile=Get-Content "D:\temp\test.xml"
$processFile=New-Object xml
$processFile.Load("D:\temp\processes.xml")
$newProcess=(@($processFile.processes.process)[0]).Clone()
Write-Host $newProcess.pId
$xmlFile.Services.Service | ForEach-Object {
    Get-Process $_ | ForEach-Object {
        $newProcess = $newProcess.Clone()
        $newProcess.Id = $_.Id.ToString()
        write-host $newProcess.Id
        $processFile.processes.AppendChild($newProcess)
    }
}
$processFile.processes.RemoveChild($processFile.processes.process[0])
$processFile.Save("D:\temp\proc.xml")

This reads from the first xmlfile, opens the next as a template. Makes a cloneable object, clones it and inserts values. It appends the new object to the xml and at last it removes the first child as it was the template and voila, we have ourselves a written xml



 
    844   
    4056 

lørdag den 21. januar 2012

Hello Powershell

For a long time I have thought about writing a hello world  on Powershell.. I have done a lot of scripting in my career and Powershell is a great step in the right direction.

So lets get it fired up.
But.. it just looks like a command prompt.. whats so special about that.. Well wait and see. First of all lets do the general Hello world.  In the prompt write:

Write-host "Hello world"

And it returns Hello world as you might imagine.. WOW?? not yet.. Lets try another little thing. Try to write get-pr and press the Tab key. It now fills out the rest and writes Get-Process. Thats great, it has autocompletion .. Press enter, and see the running processes.. Impressed??

Now for something even better. The pipelining. (|) when you write a command it returns an object. This object can be further processed by another command after the pipecharacter through the variable $_. So when you write Get-Process you'll receive a collection of processes, and if you see at the top of the list, the propertynames are shown.
This'll lead us to the first wow.. Try to write :

Get_Process |  Where-Object {$_.ProcessName -like "wi*" }

Now you should see only the processes where the processname starts with wi. So to make a final Hello world in this blog. Try to write


Get-Process | Where-Object {$_.ProcessName -like "wi*" } | ForEach-Object {Write-Host "Hello" $_.ProcessName }

Now you should have a hello for the processes.