Mechanism for updating All Attributes?

Jan 20, 2010 at 8:13 PM
Edited Jan 20, 2010 at 8:18 PM

First off, I'd like to say thanks for the great work!   I've been testing the Simple Savant library out for a project and so far I like it very well.

So, on to my question(s).  My only issues thus far have been just more usage/ignorance on my part... and so given this, I'm sure I'm probably not seeing the whole picture concerning the inner workings of Simple Savant.

Anyway, that's my disclaimer... I'm probably just being stupid here but nonetheless I wonder if maybe I'm missing something.

Mainly this:  Thus far I haven't found a way to update all attributes for a given object...

I've dug into the source a bit and so far I've only seen methods for updating given a set of property values.  So, OK... that leads me to believe that there might be a way to automatically construct these property values, and item mappings, etc given a particular object... but it hasn't jumped out at me yet.  So before I go digging in deeper, I figured it would be easiest just to ask =].

There you have it, that is my basic question... Can I update All Attributes somehow by just providing an object or generating the property values object... or some way less manual than say...

 

var values = new PropertyValues(myId)

values["PropName1"] = "value1";

values["PropName2"] = "value2";

savantobj.PutAttributes<MyObject>(values);

 

Currently, I'm using reflection on my objects to make it a little easier... like so...

 

 

var t = MyObject.GetType();

var values = new PropertyValues(MyObject.MyId);

var props = t.GetProperties().Where(x => (x.Name != "MyId"));

//warning reflection

//TODO: Should try to make better by not reflecting every time AKA Emitting...

foreach (PropertyInfo prop in props)

{

  values[prop.Name] = prop.GetValue(MyObject, null);

}

savantobj.PutAttributes<MyObject>(values);

 

Obviously, sort of painful... but just did quick and dirty for now to handle until I figure out what the proper usage is or create something better.  

Any pointers for me on what I might be missing or any basic built in mechanisms to make this easer?  In follow up, if nothing is built in as of right now... are there any plans to implement?  Also, another follow up if not implemented then would it be helpful if I submitted a less painful generic version that minimizes the reflection per type... like say a generic method along the lines of:  PutAllAttributes<T>(object obj)?  Sorry for so may questions =].

Thanks,

Mat

 

 

            var t = record.GetType();
            try
            {
                var values = new PropertyValues(record.PatientId);
                var props = t.GetProperties().Where(x => (x.Name != "PatientId"));
                //warning reflection
                //TODO: Make better with Reflection.Emit
                foreach (PropertyInfo prop in props)
                {
                    values[prop.Name] = prop.GetValue(record, null);
                }
                values["isActive"] = true;
                SdbData.PutAttributes<PatientRecord>(values);            var t = record.GetType();
            try
            {
                var values = new PropertyValues(record.PatientId);
                var props = t.GetProperties().Where(x => (x.Name != "PatientId"));
                //warning reflection
                //TODO: Make better with Reflection.Emit
                foreach (PropertyInfo prop in props)
                {
                    values[prop.Name] = prop.GetValue(record, null);
                }
                values["isActive"] = true;
                SdbData.PutAttributes<PatientRecord>(values);

 

Coordinator
Jan 20, 2010 at 8:28 PM

Glad it's working well for you. You may want to check out this introductory article (linked on the main project page): http://blog.coditate.com/2009/03/simple-savant-c-interface-to-amazon.html

If that doesn't answer your question, let me know. The release package also contains significant API documentation (MS Help format) that will help.

Jan 21, 2010 at 2:36 AM
Edited Jan 21, 2010 at 3:11 AM

OK, awesome.  Thanks for the quick response.  I didn't realize there was a chm file... It's a bit hard to find.  I looked a bit harder this time and finally found it in the build directory.  So anyway, the chm/MS Help file pointed me in the right direction.

Sure enough... I had just overlooked the CreateValues static method.  And this worked great:

 

var values = PropertyValues.CreateValues(myobjinstance);

SdbData.PutAttributes<MyObject>(values);

Just as I thought, only ignorance on my part =].

Thanks again,

Mat

 

Coordinator
Jan 21, 2010 at 3:10 AM

If you're saving all properties of the object you can simplify even further:

MyObject m = new MyObject();
SdbData.Put<MyObject>(m);

Jan 21, 2010 at 3:29 AM

Yeah, I've used that for saving the object INITIALLY[to create intially] and it works and has always worked fine... BUT at one point while I was testing that didn't work for UPDATING existing attributes... however, I'm sure I was most likely just doing something wrong... so at that time I just started using putattributes because that is what worked for me and I apparently made some bad assumptions.  If the PUT method UPDATES as well as CREATES then that is great.  Very awesome and yes that simplifies nicely.  Apologies for the public forehead smacking session =].  Again, thank you for your help.