I'd like to have this code looked over because I feel my approach might be very novice and there has to be a more elegant or at least less "grunt-work" way of doing it.
I have to write a small TCP client in my application which can send an object to a server for it to be stored in a Database. Before I send the object I remember that I was once told sending custom objects is generally bad practice unless you have a custom Stream Class (Which I don't have). So I am dissecting my objects before I send them into their respective Strings/ints/whatever else objects. I just feel my approach might not be very efficient.
The end result will be that the server can put together a Query to be executed in the database for storage.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Smart_Journal.Familie;
using System.Net.Sockets;
using System.IO;
namespace Smart_Journal
{
/// <summary>
/// DBM is our Database Manager. It will handle sending and receiving data from the server.
/// You should NOT call these functions directly. Instead, call the functions in the Utility Class.
/// </summary>
public static class DBM
{
private static TcpClient _client;
private static StreamReader _sReader;
private static StreamWriter _sWriter;
private static bool _isConnected;
private static int portNum;
private static String ipAddress;
private static void InitConnection()
{
_client = new TcpClient();
_client.Connect(ipAddress, portNum);
_sReader = new StreamReader(_client.GetStream(), Encoding.ASCII);
_sWriter = new StreamWriter(_client.GetStream(), Encoding.ASCII);
_isConnected = true;
}
private static void EndConnection()
{
_sWriter.WriteLine("END");
_sWriter.Flush();
_sReader.Close();
_sWriter.Close();
_client.Close();
_isConnected = false;
}
// Basically means: SendSupportFamilyObject
public static void SendPlejeFamilieObjekt(PlejeFamilie pfamilie)
{
InitConnection();
if (_isConnected)
{
_sWriter.WriteLine("SEND_PLEJEFAMILIE");
_sWriter.Flush();
if (_sReader.ReadLine().Equals("AUTH_GIVEN"))
{
#region Send Plejefamilie Objekt
_sWriter.WriteLine(pfamilie.CPRKvinde);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.CPRMand);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.NavnKvinde);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.NavnMand);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.Addresse);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.MobilKvinde);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.MobilMand);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.HjemmeTelefon);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.StillingKvinde);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.StillingMand);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.EmailKvinde);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.EmailMand);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.EmailFaelles);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.GodkendtDato);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.PlejeForaeldreUdd);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.AntalKurserIAar);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.AntalBoernGodkendt);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.Vilkaar);
_sWriter.Flush();
_sWriter.WriteLine(pfamilie.Status);
_sWriter.Flush();
#endregion Send Plejefamilie Objekt
}
}
EndConnection();
}
// Basically means: SendSpecialNeedsChildObject
public static void SendPlejeBarnObjekt(PlejeBarn pbarn)
{
InitConnection();
if (_isConnected)
{
_sWriter.WriteLine("SEND_PLEJEBARN");
_sWriter.Flush();
if (_sReader.ReadLine().Equals("AUTH_GIVEN"))
{
#region Send Plejebarn Objekt
_sWriter.WriteLine(pbarn.CPR);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Navn);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.FolkeregisterAdresse);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Email);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Telefon);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Sagsbehandler);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Konsulent);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.Aflastning);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.NuvaerendeForanstaltning);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.AnbringelsesDato);
_sWriter.Flush();
_sWriter.WriteLine(pbarn.UdskrivningsDato);
_sWriter.Flush();
_sWriter.WriteLine("PLEJEBARN_B");
_sWriter.Flush();
foreach(Note n in pbarn.Bemaerkninger)
{
_sWriter.WriteLine(n.Dato);
_sWriter.Flush();
_sWriter.WriteLine(n.Navn);
_sWriter.Flush();
_sWriter.WriteLine(n.Tekst);
_sWriter.Flush();
}
_sWriter.WriteLine("END_PLEJEBARN_B");
_sWriter.Flush();
_sWriter.WriteLine("PLEJEBARN_TF");
_sWriter.Flush();
foreach (Note n in pbarn.TideligereForanstaltninger)
{
_sWriter.WriteLine(n.Dato);
_sWriter.Flush();
_sWriter.WriteLine(n.Navn);
_sWriter.Flush();
_sWriter.WriteLine(n.Tekst);
_sWriter.Flush();
}
_sWriter.WriteLine("END_PLEJEBARN_TF");
#endregion Send Plejebarn Objekt
}
}
EndConnection();
}
// Basically means: SendBiologicalFamilyObject
public static void SendBiologiskFamilieObjekt(BiologiskFamilie bfamilie)
{
InitConnection();
if (_isConnected)
{
if (_sReader.ReadLine().Equals("AUTH_GIVEN"))
{
#region Send Biologiskfamilie Objekt
_sWriter.WriteLine(bfamilie.CPRMor);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.CPRFar);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.NavnMor);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.NavnFar);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.Addresse);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.MobilMor);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.MobilFar);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.HjemmeTelefon);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.EmailMor);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.EmailFar);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.EmailFaelles);
_sWriter.Flush();
_sWriter.WriteLine(bfamilie.ForaeldreMyndighed);
_sWriter.Flush();
#endregion Send Biologiskfamilie Objekt
}
}
EndConnection();
}
public static PlejeFamilie HentPlejeFamilieObjekt(String s)
{
return null;
}
public static PlejeBarn HentPlejeBarnObjekt(String s)
{
return null;
}
public static BiologiskFamilie HentBiologiskFamilie(String s)
{
return null;
}
}
}
_sWriter.WriteLine(pfamilie.CPRKvinde);
can you translate everything to English? if not that is cool. it just helps to understand what is really going where. \$\endgroup\$