MadBoy
MadBoy

Reputation: 11114

Creating a simple class in C# to gather data from SQL

I'm trying to learn proper way of defining classes. I have two classes where Person is definition for every possible information about person, while Client is using information from Person and is highely connected with Person.

public class Person {
    public virtual int ID { get; set; }
    public virtual string FirmaNazwa { get; set; }
    public virtual string Imie { get; set; }
    public virtual string Nazwisko { get; set; }
    public virtual Adres Adres { get; set; }
    public virtual Adres AdresKorespondencyjny { get; set; }
    public virtual UrzadSkarbowy UrzadSkarbowy { get; set; }
    public virtual string Pesel { get; set; }
    public virtual string Nip { get; set; }
    public virtual string Regon { get; set; }
    public virtual string Krs { get; set; }
    public virtual DateTime KrsData { get; set; }
    public virtual string KrsOznaczenie { get; set; }
    public virtual string Rodzaj { get; set; } // Typ klienta (Firma / Osoba)
    public virtual string Narodowosc { get; set; }
    public virtual string ZgodaNaPrzetwarzanieDanychOsobowych { get; set; }
    public virtual string ZgodaNaPrzetwarzanieDanychOsobowychMarketing { get; set; }
    public virtual Pracownik Uzytkownik { get; set; }
    public virtual DateTime DataZmiany { get; set; } 
}
public sealed class Client {
    public int ID { get; set; }
    public Person Person { get; set; }
    public Pracownik Uzytkownik { get; set; }
    public DateTime DataZmiany { get; set; }
    public string Haslo { get; set; }
    public string Typ { get; set; }
    public void GetClient() {

    }
}

I've got 2 tables in SQL which are basically Person and Client tables with fields that are similar to those defined in c#.

Now I would like to add some methods (GetClient() and similar) to actually get Client information but since to get all Client information I need to get all Person information first. In SQL I would do query with JOIN's and I kinda get Client info connected with Person info straight away. So do I basically do it like:

    public void GetClient() {
        //SQLQUERYHERE
        Person podmiot = new Person();
        podmiot.FirmaNazwa = ...
        podmiot.Imie = ...
        Haslo = "test";
    }

Or should I do it differently?

Upvotes: 0

Views: 542

Answers (1)

CodeCaster
CodeCaster

Reputation: 151730

There are various options here:

O/R mapping

Use a framework to generate your classes from the database layout. Extend the classes to add your own functionality and extra properties. Some frameworks to do this are:

Code your own

You could also write your own queries, and while for the learning purposes (and sometimes for the sake of simplicity and less overhead and dependencies) it's the less recommended approach. You could use a DataReader that you fill with the results of a query, and assign the properties manually:

foo.Bar = dataReader["Bar"].ToString()

The Enterprise Library can make things a bit easier here.

Upvotes: 2

Related Questions