0

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?

2
  • Have you looked at ORMs like LinqToSql, Entity Framework and NHibernate? They take care of all this for you. Commented Nov 24, 2011 at 10:57
  • I tried NHibernate just now just I couldn't get around it. Somehow writing a simple query to get all that seems simpler (as I've been doing this for 2+ years like that). Commented Nov 24, 2011 at 11:01

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

For the last 2 years i've been writting queries by hand and assigning results to strings etc. I tried to use NHibernate now but I have some problems with it to actually catch how to properly use it.
@MadBoy There is a learning curve to using ORMs - but it is worth the effort in most cases. If NHibernate seems hard, possibly check out Dapper - samsaffron.com/archive/2011/03/30/… .
I started using Entity Framework now. I'm heading somewhere just it seems to have problem with two databases that have a lot in common database names, same columns and it's finding in c# that the names are already in use (360 errors).
@MadBoy Create the 2 different database models in separate name spaces then you won't get a clash
Am I understanding it right that with Entity Framework I don't have to define class Person and class Client myself but I can relay on whatever is provided by Entity?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.