Good class design by example

前端 未结 4 1259
忘了有多久
忘了有多久 2021-01-30 14:17

I am trying to work out the best way to design a class that has its properties persisted in a database. Let\'s take a basic example of a Person. To create a new per

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 14:55

    With C# 3.0 class initializers I no longer bother of providing a constructor that allows me to initialize all properties:

    var person1 = new Person
    {
        FirstName = "Kate";
        LastName = "Middleton";
    };
    

    As far as the Save method is concerned I usually put them in a separate repository class:

    public int Save(Person person) 
    {
        ...
    }
    

    and then when I need to save a person I do:

    var person1 = new Person
    {
        FirstName = "Kate";
        LastName = "Middleton";
    };
    var id = new PersonsRepository().Save(person1);
    

提交回复
热议问题