0

I've got a situation where I have data classes that are generated by a database with various properties on them e.g which I cannot modify.

public DataClass
{
    public string PropertyX {get; set;}
    public int PropertyY {get; set;}
    public float PropertyZ {get; set;}

}

Some of these classes may have 20 or so properties. When updating the data a "transient" copy of the "persistent" data is made, then the properties updated on the transient and copied from the transient to persistent.

Which is fine although if only changing one property isn't very efficient.

I wanted to find out if there is a way in c# that I could create a list of flagged properties or add attributes onto certain flagged properties I wish to update.

So the end result would be (please note this is all pseudo)

 DataClass transientObj = new DataClass(Transient);
 [FlagPropertyToUpdate] //This is the bit I have no idea how to do
 transientObj.propertyX = "updateOnlyMe!";
 DataClass persistantObj = new DataClass(Persistant);
 UpdateData dataUpdater = new UpdateData(transientObj,persistantObj)

 dataUpdater.save();

 public UpdateData 
 {
      public void save(){
          //some how know to only update propertyX and not all three properties
      }  
      public UpdateData(DataClass trans, DataClass pers)
 }

Any assistance on how I could go about doing this (and if its possible) would be highly appreciated!

Tim Kalinowski
  • 306
  • 5
  • 18

2 Answers2

1

I think View Model's will fit here.

The ViewModel is an abstraction of the View or a conceptual state of the data as opposed to the real state of the data in the Model.

So in this case you will have a class like:

public DataClassViewModel
{
    //Define all relevant properties here.
    ...
    public DataClassViewModel(DataClass model) //Constructor
    {
       //Initialize the view model from the model. 
    }

    public DataClass GetModel()
    {
       //Depending on changes in the view model, model could be updated here.
    }

    public void UpdateData()
    {
    }
}

You can read more about view model's here:

Model-View-ViewModel (MVVM) Explained
How we do MVC – View models What is ViewModel in MVC?

Although the articles point to MVC/MVVM UI architectures, view model is quite a general concept.

Community
  • 1
  • 1
A G
  • 21,087
  • 11
  • 87
  • 112
  • Thanks Aseem, i'll look into this, the database software this company uses (http://www.jade.co.nz/jade/index.htm) generates a basic model although it doesn't have the 'RaisePropertyChanged' concept unfortunately. I'll keep reading into it. – Tim Kalinowski Sep 10 '13 at 06:56
1

I'd recommend creating a DTO (Data Transfer Object) that supports dirty-flagging the relevant properties. That way your classes can remain unchanged. Have a look at Automapper.

Then again, I'd invite you to reconsider your assertion that updating the complete set of properties is inefficient, assuming they're simple types. Writing to an entire row in a database is generally no more expensive than writing a single column. I'd be more concerned wth concurrency in the situation you describe.

Eric Lloyd
  • 509
  • 8
  • 19
  • Thanks Eric, i'll look into it. The database in question is Jade http://www.jade.co.nz/jade/index.htm and is completely object orientated. So the properties are often references to other objects. I'm still doing more research, unfortunately the database software doesn't have that much information on it although its a requirement. – Tim Kalinowski Sep 10 '13 at 07:01
  • Given that Jade is a commercial ORM product, I'd imagine they've worked out how to efficiently track which objects have been persisted to the database and avoid needless effort. Their whole *raison d'etre* is to let you avoid having to think about such things. – Eric Lloyd Sep 10 '13 at 07:55