Steoates
Steoates

Reputation: 3168

Object Auditing

currently we have quite a chunky auditing system for objects within our application, the flow goes like this..

-Class implements an interface

-Interfaces forces the class to override some methods for adding properties that need auditing to a List of KeyValuePairs

-Class is then also needs to recreate the objects state from a list of key value pairs

Now the developer needs to add all this to there class, also our objects change quite often so we didn't just serialise the class.

What I would like to do is to use attributes to mark the properties as auditable and then do everything automatically so the developer doesn't really need to do anything.

My Main question is - I know people always say reflection is slow, how slow are we talking? what performance hits am I going to get from looking through the class and looking at attributes against a property and then doing any required logic?

thanks for any help

Ste,

Upvotes: 0

Views: 260

Answers (4)

CodesInChaos
CodesInChaos

Reputation: 108830

Reflection is the way to go here. If it's too slow(measure!) you can throw in a bit of caching, or in the worst case generate an Expression<T> and compile it.

There are two phases in your problem:

  1. Figure out which properties you want, and return a list of their PropertyInfos. You need to do this only once per type, and then you can cache it. Thus performance of this step doesn't matter.
  2. Getting the value of each property with PropertyInfo.GetValue.

If this step 2 is too slow, you need to generate an Expression in step 1, and the overhead over manually written code goes down to a single delegate invocation.

Upvotes: 0

Wouter de Kort
Wouter de Kort

Reputation: 39898

It's hard to give a specific answer because it depends on what adequate performance is for your application.

Reflection is slower then normal compiled code but when worrying about performance problems it's always better to have something that works and then use profiling to find the real performance bottleneck and optimize.

Premature optimization could lead to code that's much harder to maintain so your developers will be less productive.

I would start with using reflection and write a good set of unit tests so you know your code is working. If performance turns out to be a problem you can use the Visual Studio profiler to profile your unit tests and discover the bottlenecks.

There are some libraries that can speed up reflection or you could use Expression trees to replace your reflection code if it's to slow.

Upvotes: 1

Kane
Kane

Reputation: 16812

Reading this response from Marc I would suggest that Reflection should be fine for most application needs.

Before making any fundamental changes I would suggest running a profiler to find the bottlenecks in your code. If you identify the reflection / auditing process is the major pain point use an IL Emit and try again.

Upvotes: 0

Tigran
Tigran

Reputation: 62276

If the performance ok or not depends on your app context. So it's difficult to say if it is slow or fast for you, you should try it by yourself.

Most probably, imo, it would give pretty acceptable performance, but again I have no idea where you gonna use it.

Like other solutions that come to my mind, could be:

  • Sqlite, where to save the key/value data
  • Aspect Oriented Programming (like a PostSharp) to generate a data in compile time.

But the first thing I would try, is a Reflection, just like you think.

Upvotes: 0

Related Questions