29.6.10

XML Serialization objects generically

Below is an example to serialize and deserialize an object using generics in C#:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

public class MyClass
{
public static void RunSnippet()
{
string test = "abc";
int number = 123;
WL(XMLSerializationUtility.SerializeObject(Encoding.UTF8, test));
WL(XMLSerializationUtility.SerializeObject(Encoding.UTF8, abc));

}

#region Helper methods

public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}



public class XMLSerializationUtility
{
public static T DeserializeObject<T>( Encoding encoding, string xml )
{
try
{
using (MemoryStream memoryStream = new MemoryStream( StringToByteArray( encoding, xml ) ) )
{
using ( XmlTextWriter xmlTextWriter = new XmlTextWriter( memoryStream, encoding ) )
{
XmlSerializer xmlSerializer = new XmlSerializer( typeof( T ) );

return (T)xmlSerializer.Deserialize( memoryStream );
}
}
}
catch
{
return default( T );
}
}

public static string SerializeObject<T>( Encoding encoding, T obj )
{
try
{
using(MemoryStream memoryStream = new MemoryStream())
{
using ( XmlTextWriter xmlTextWriter = new XmlTextWriter( memoryStream, encoding ) )
{
XmlSerializer xmlSerializer = new XmlSerializer( typeof( T ) );
xmlSerializer.Serialize( xmlTextWriter, obj );

memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
}

return ByteArrayToString( encoding, memoryStream.ToArray() );
}
}
catch
{
return string.Empty;
}
}

private static Byte[] StringToByteArray( Encoding encoding, string xml )
{
return encoding.GetBytes( xml );
}

private static string ByteArrayToString( Encoding encoding, byte[] byteArray )
{
return encoding.GetString( byteArray );
}
}






Source: http://www.dotnet-snippets.de/dns/objekt-serialisierung-SID1407.aspx

25.6.10

Early warning signs that an SW-Architecture is in trouble

  • The architecture is forced to match the current organization.
  • Top-level architecture components number more than 25.
  • One requirement drives the rest of the design.
  • The architecture depends upon alternatives in the system software.
  • Proprietary components are being used when standard components would do.
  • The component definition comes from the hardware division.
  • There is redundancy not needed for reliability (or load balancing).
  • The design is exception driven (emphasis on the extensibility, not on core commonalities).
  • The architect or project manager has difficulty identifying the stakeholders.
  • The project team has difficulty identifying the architect(s) of the system.
  • Developers have a plethora of choices in how they design and code.
  • The architect, when asked for architecture documentation, produces class diagrams and nothing else.
  • The architect, when asked for architecture documentation, provides a large stack of automatically generated documents which no humans has ever seen.
  • Documents provided are old and apparently not kept up to date.
  • A developer, when asked to describe the architecture, is either unable to or describes a much different architecture than the architect presented.

14.6.10

Checklist: questions the architect should plan to answer

  1. What are the driving architectural constraints, and where are they documented? Are they requirements or goals? Are they measurably quantitative or qualitative? In particular, what are the system's real-time constraints?
  2. What component types are defined? For each, what are its:
    • Responsibilities
    • Methods, data members
    • Limitations
    • Composition rules
    • Other characteristics
  3. What component instances are defined by the architecture?
  4. How do components communicate and synchronize? In particular:
    • Mechanisms used
    • Restrictions on use
  5. What are the system partitions?
    • Composition
    • Restrictions on use and visibility
    • Functional allocations
  6. What are the styles or architectural approaches used?
  7. What constitutes the system infrastructure?
    • Supplied functionality
    • Resource management
    • APIs
    • Restrictions
  8. What are the system interfaces? (this includes HMI, devices, external systems)
    • Participants
    • Mechanisms, formats, protocols, modes
    • Identification/typing/versioning What are the strategies and tools used for persistent storage?
  9. What are the strategies and tools for enforcing security requirements ?
    • Trust boundaries
    • Threat model
  10. What are the strategies and tools for handlings faults and failures ?
  11. What is the process/thread model of the architecture?
  12. What is the deployment model of the system?
    • Server and datastore partitioning/instances
    • Topology
    • Capacity and sizing
  13. What are the system states and modes?
    • Control
    • Responsibilities
    • State knowledge dispersal
  14. What COTS (Commercial off-the-shelf) are used? How are they chosen and integrated?
  15. What variability (in terms of implementation changes and not data or scenario changes) mechanisms and variation points are included in the architecture ?
  16. How far along is the development? Were the block delivery dates met? Did the blocks meet their functionality requirements?
  17. What documentation tree and human help do new employees get?
  18. What is the skill level and experience of the development team members?

11.6.10

3 arten der Serialisierung bzw Deserialisierung in C#

Man versteht darunter eine Abbildung von Objekten auf eine externe sequenzielle Darstellungsform. Serialisierung kann für das Erreichen von Persistenz für ein Objekt verwendet werden, aber auch in verteilten Softwaresystemen spielt Serialisierung eine bedeutende Rolle.[Quelle Wikipedia: http://de.wikipedia.org/wiki/Serialisierung]

Es gibt unter C# 3 Arten der Serialisierung:
* BinaryFormatter (Objekte werden binär abgespeichert)
* SoapFormatter (Objekte können gelesen werden)
* XMLSerializer (Objekte werden in eine XML Datei gespeichert)

using System;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;
using System.IO;
using System.Collections;


namespace SerializationSample
{
public class Tester
{

private readonly string serFolder;
private static ArrayList arrList = new ArrayList();

public Tester()
{
serFolder = Path.GetTempPath();
}

public static void Main()
{
Tester abc = new Tester();
arrList.Add((Person) abc.AddPerson("Hans", "Müller", "CH", 39));
arrList.Add((Person) abc.AddPerson("Luft", "Hansa", "D", 82));
arrList.Add((Person) abc.AddPerson("Soto", "Mayer", "ZSQ", 55));

abc.serializeXml();
abc.serializeSoap();
abc.serializeBinary();
Console.WriteLine("Press enter to begin deserialization...");
Console.ReadLine();
abc.deserializeXml();
abc.deserializeSoap();
abc.deserializeBinary();
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}

//....(ArrayList füllen .....)
private object AddPerson(string Vorname, string Name, string Land, int Alter)
{
Person p = new Person();
p.Vorname = Vorname;
p.Name = Name;
p.Land = Land;
p.Alter = Alter;
return p;
}

/*****************
* XmlSerializer *
*****************/
private void serializeXml() {
Console.WriteLine("Serializing in XML...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "Personen.xml"), FileMode.Create)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
xmlSer.Serialize(fs, arrList);
}
}

private void deserializeXml() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "Personen.xml"), FileMode.Open)) {
XmlSerializer xmlSer = new XmlSerializer(typeof(ArrayList), new Type[] { typeof(Person) });
arrList = (ArrayList) xmlSer.Deserialize(fs);
}
Console.WriteLine("Deserializing XML...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}

/******************
* Soap Formatter *
******************/
private void serializeSoap() {
Console.WriteLine("Serializing in SOAP...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "soap.dat"), FileMode.Create)) {
SoapFormatter soaFormatter = new SoapFormatter();
soaFormatter.Serialize(fs, arrList);
}
}

private void deserializeSoap() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "soap.dat"), FileMode.Open)) {
SoapFormatter soaFormatter = new SoapFormatter();
arrList = (ArrayList)soaFormatter.Deserialize(fs);
}
Console.WriteLine("Deserializing SOAP...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}

/********************
* Binary Formatter *
********************/
private void serializeBinary() {
Console.WriteLine("Serializing in Binary...\n");
using (FileStream fs = new FileStream(Path.Combine(serFolder, "binary.dat"), FileMode.Create)) {
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(fs, arrList);
}
}

private void deserializeBinary() {
try {
using (FileStream fs = new FileStream(Path.Combine(serFolder, "binary.dat"), FileMode.Open)) {
BinaryFormatter binFormatter = new BinaryFormatter();
arrList = (ArrayList)binFormatter.Deserialize(fs);
}
Console.WriteLine("Deserializing Binary...\n");
foreach (Person p in arrList)
Console.WriteLine(p.ToString());
}
catch (IOException ex) {
Console.WriteLine(ex.Message);
}
}
}

//Die zu serialisierende Klasse
//(Die XML-Attribute werden nur für die Xml-serialisierung gebraucht)
[Serializable()]
public class Person {
[XmlElement("Vorname",DataType="string")]
private string vorname;
[XmlElement("Nachname", DataType = "string")]
private string name;
[XmlElement("Alter", DataType = "int")]
private int alter;
[XmlAttribute("Land", DataType = "string")]
private string land;

public Person() { }

public string Vorname {
get { return vorname; }
set { vorname = value; }
}

public string Name {
get { return name; }
set { name = value; }
}

public int Alter {
get { return alter; }
set { alter = value; }
}

public string Land {
get { return land; }
set { land = value; }
}

public override String ToString() {
return vorname + "\t" + name + "\t" + alter + "\t" + land;
}
}
}





Nvidia's GauGan App

NVIDIA's GauGAN AI Machine Learning Tool creates photorealistic images from Simple Hand Doodling http://nvidia-research-mingyuliu.com/...