What's the best way to implement tracking changes on your entities? - c#

The requirement is to track all changes made to an entity, track it to know who did it, when he did, etc. For example, I have a Person entity, and a user has changed the name of the person; I'd like to keep that information somewhere.
What's the best approach for this? Or is there an existing framework to achieve this.
I know that SQL 2008 has support for tracking changes, but it's not an option for now, because a lot of our customers are already using SQL 2005.
Is the Logging Application Block of Enterprise Library a good candidate for this requirement? I've check it out a little bit but, I don't see how I can use it to track the who did it, what value has change, when he did it, etc.
We are using C# and .NET framework for our app.

Standrad way is to use events from INotifyPropertyChanging and INotifyPropertyChanged interfaces. And for collections INotifyCollectionChanged. Once you subscribed to this events you can store information wherever you want.
If you using MSSQL you can try Query Notifications

Some sort of AOP solution (like the one in enterprise library) should provide you with enough possibilities to create such a solution.
You can subscribe to a property-invoke method and log the information.

Related

Logging Data Base changes using Entity Framework and ELMAH (or else)

Our customer request the functionality of logging changes in db.
We need next structure:
"Timestamp", "User", "DB.Table.Field changed", "Value after change"
What we have:
Entity Framework Code First
ELMAH
MySQL Data Base with more than 100 tables (changes should be tracked to all of them)
So we need:
Somehow track what table was changed
What new value was added/updated
Generic functionality (if possible)
Any ideas how to do this?
PS. ELMAH is not a strict requirement. But MySql is :)
Thank you!
Ultimately, what you're looking for is some sort of behavior that keeps track of auditing. ELMAH is not geared towards that.
You most likely want to look in to some sort of Polymorphic object that can keep track of who executed what type of action on a known entity. You're most likely going to want to use some sort of AOP concepts. Libraries like like DynamicProxy2, LinFu should be helpful in this area.
Your goal would be to use proxy objects that wrap your existing entities and "inject" the auditing information that you need to track.
I actually had the same feature request and even though I wrote a tracking layer before for my generic repository I found that there is one available in Nuget/Github that handles this.
https://www.nuget.org/packages/TrackerEnabledDbContext/
https://github.com/bilal-fazlani/tracker-enabled-dbcontext
You can use this and I found that it works great for me. I use it into my Generic repository project.

Automatically log changes made to records in database

I'm wondering what the best way to implement this would be.
Basically our project has a requirement that any change made to records in the database should be logged. I already have it completed in C# using Reflection and Generics but I'm 100% sure that I used the best method.
Is there a way to do this from inside the SQL database?
The big key is that the way our project works, the ObjectContext is disconnected, so we couldn't use the built in Change Tracking and had to do our own compares against previous Log items.
If you're using SQL Server 2008 or higher, you can implement either change tracking or change data capture directly on the database. Note that the latter is only available in the Enterprise edition engine. There are pros and cons to each method. You'll have to review each solution for yourself as there isn't enough requirement information to go on in the question.
If you're using SQL Server 2005 or below, you'll have to resort to a trigger-based solution, as suggested by the other answers.
You want to look at database triggers.
depending on the complexity of your datamodel you could setup on update/insert/delete triggers on the relevant tables - these triggers could log whatever is needed (old/new values, User, timestamp etc.)... see http://msdn.microsoft.com/de-de/library/ms189799.aspx
Look at my blog to see how you can track data changes without database scheme modification:
part1,part2
For your project requirement, SQL trigger is the better solution than the current C# reflection. Becaz triggers provides a way for the database management system to actively control, monitor, and manage a group of tables whenever an insert, update, or delete operation is performed. More over, the requirement is full filled at DataBase layer itself and so hosted as the single solution for various front end applications.

Available frameworks/tools to send change notifications?

Is there a framework that will identify (through comparing the object) when an object property value has changed (user can specify which properties to "watch") and then raise an event/send a notification?
Example: The app stores Person class in a database and keeps a copy in memory. The app retrieves the person data from the database to refresh the copy in memory periodically. Other apps/processes update the person data in the database. I want an event/notification raised when the mood property(list collection of strings: Upbeat,tired, etc.) of the person object is changed.
I know how to code this, but I get the feeling there is something out there that does this. Only two things I can think of that is similar is:
cache notifications, but haven't seen any that allow you to specify which
properties/values to notify on. This would also require one to have a distributed cache server.
sql query notifications - I'm looking into this one.
Your description of the issue sounds a lot like what happens when you work with Entity Framework (and other ORMs). It's possible you could use the Entity Framework, and inspect the objects after they have been processed to detect changes. An example of which you can find here.
Is that the kind of thing you're looking for?
EDIT:
Regarding the Sql Query Notifications, take a look at the SqlDependency Class if you want to head down that route.

Save objects retrieved from web services

I am totaly new to SubSonic, and have never used any other tool like it.
I am not sure if SubSonic would be the right tool, or if there are any other tool that could help. Maybe someone can clarify things for me. :)
The scenario is this:
I have a c# application that fetches data from a set of secured web services, secured through WSE 3.0. The web services expose product data, press releases related to products etc.
The data is deserialized upon retrieval, so the result is a for example a 'Product' object.
Now I want to save this data. There are dozens of different classes that needs to be saved to separate tables.
I started out creating tables and data access logic manually but then I realized maybe this was something that a tool like SubSonic or NHibernate could help with?
Could any of them use the schemas from the web services to create the data access layer and data tables?
If so, how and where would I start?
As I said, I have never used any tool like this before, so I am not sure this is something they could be useful for.
Greateful for any feedback!
your question is not very specific, so i just can say what i'd gonna do. I am not so familiar with SubSonic so i'd prefer NHibernate. It's free and OpenSource. Of course it can help you in this situation, even though you would have to create the mapping by yourself. I assume you already have the classes you want to save, then you could simply annotate them with some NHibernate attributes and then, if not already exists hibernate would be able to create the database for you.
Another benfit would be the use of 'cascade' wich enables you to simply save a product entity and all its attached press releases (or whatever your structure is) would be saved too automatically, though i'd admit that you should use this features with care. NHibernate has a very active community and there are a lot of good example projects and tutorials. Simply use the search for some hints. To Start look on nhibernate.info and for usefull tips and best practices Ayendes Homepage
Hibernate is very powerfull but also complex and it takes some time to get used to all features it provides, but simple CRUD operations are very easy to accomplish. So if you really just want to save/load/delete this data and thats all you wouldn't have to learn HQL or touch the Criteria API.
Hope this helps.

How to do multi user event driven screen updates?

I work on a Delphi program that uses a firebird database to store it’s data. It uses a home grown o.r.m. system. When two users look at the same data, for example in a data grid, and one changes something the other screen gets updated immediately.
The way this works is that the objects in the one program store them self’s in the database. The database has a trigger that registers this change in a auxiliary changes log table. Writing to this changes log table triggers an event. The second program is listening to that event and knows from the entry in the changes log table what object was changed. It will reload that object and raise an event internally in that program. This event will cause all screens that are showing this object and all objects that base their behavior on this object to update.
We would like to switch to .Net and maybe MS SQL.
My question is:
1) How do I do this with MS SQL?
2) Is there a framework that supports this? So I won’t have to port this to .Net
Since the .Net Framework 2.0 and MSSqlServer 2005 versions there is support for change notifications: using the SqlDependency class. You have an example in the codeproject.
Take also a look to some recommendations about using notifications in the msdn.
doesn't sound like this would be too difficult to implement in c#.
try using the "observer pattern" with whatever custom modefications you'd like.

Categories