lundi 24 août 2015

Setting the propery, when not knowing which one

Say I have this class with a few members, for example (this is a contrived example, I'd rather no have a discussion about the intricacies of the real-life design. I really just want to convey the general idea here.):

public class Address
{
    public Guid Id { get; set; }
    public Guid? HouseId { get; set; }
    public Guid? FlatId { get; set; }
    public Guid? SomeOtherBuildingTypeId { get; set; 
}

Now as it happens there exist 3 methods to create an Address:

public void CreateAddressForHouse();
public void CreateAddressForFlat();
public void CreateAddressForSomeOtherBuildingType();

Under the surface this group of methods does the exact same thing, bar setting a different Id property in the Address class. This is causing quite some code duplication in the real life application and I want to rewrite this to something more general.

In my mind I can pass the name of the required property and its value to a CreateAddress function, in something like a Func. But I'm seriously lacking in this respect, where to start? What .NET stuff can I use out of the box? Or what specific keywords should I look for?





Aucun commentaire:

Enregistrer un commentaire