patterncsharpMinor
Framework for comparing database objects
Viewed 0 times
comparingobjectsdatabaseforframework
Problem
This code allows me to compare two complex objects, which are stored in a relational database, to see what has been removed from the objects. These objects have a unique integer primary key. They are also composed of other objects hierarchically.
The
```
///
/// Data which has a unique integer identifier and is composed of other similar objects
///
interface IHierarchicalDatabaseEntity
{
///
/// The unique identifier
///
int ID { get; }
///
/// The other objects that this is composed of
///
IEnumerable Children { get; }
}
///
/// Helper methods for IHierarchicalDatabaseEntity
///
public static class HierarchicalDatabaseEntityExtensions
{
///
/// Summarises the differences between the old and new database entities.
/// Each entity which is present in the old data but not in the new will result in an element being returned.
///
/// the old database entity
/// the new database entity
/// an enumeration over the types and IDs of data which is present in the old data but not in the new
public static IEnumerable>> Difference(IHierarchicalDatabaseEntity oldData, IHierarchicalDatabaseEntity newData)
{
ListLookup result = oldData.CollateIDs();
result.RemoveAll(newData.CollateIDs());
return result;
}
///
/// Gets a summary of the data within some database entities
///
/// the objects to be summarised
/// a ListLookup where the key
The
Difference method is used within a database application (code not shown here). When an object is retrieved from the database, it is cloned. At the point of saving the "new" object this is compared with the "old" clone, and Difference returns a summary of the types and IDs of those objects which have been removed by the user. Thus the database application can:- Insert new database entities and update existing ones.
- Delete removed records returned by
Differenceto prevent orphaned records in the database.
```
///
/// Data which has a unique integer identifier and is composed of other similar objects
///
interface IHierarchicalDatabaseEntity
{
///
/// The unique identifier
///
int ID { get; }
///
/// The other objects that this is composed of
///
IEnumerable Children { get; }
}
///
/// Helper methods for IHierarchicalDatabaseEntity
///
public static class HierarchicalDatabaseEntityExtensions
{
///
/// Summarises the differences between the old and new database entities.
/// Each entity which is present in the old data but not in the new will result in an element being returned.
///
/// the old database entity
/// the new database entity
/// an enumeration over the types and IDs of data which is present in the old data but not in the new
public static IEnumerable>> Difference(IHierarchicalDatabaseEntity oldData, IHierarchicalDatabaseEntity newData)
{
ListLookup result = oldData.CollateIDs();
result.RemoveAll(newData.CollateIDs());
return result;
}
///
/// Gets a summary of the data within some database entities
///
/// the objects to be summarised
/// a ListLookup where the key
Solution
Couple of quick comments, since you don't really specify what you were looking for.
Seems like everything other than
Your recursive
Your
No reason to
What happens if
expect a list of all the added or deleted
Seems like everything other than
Difference should be private; I probably wouldn't make them extension methods either. I think the whole thing would be better suited to a class rather than 3 or 4 coupled methods.Your recursive
GetAllChildren is an odd implementation that I haven't seen before. It looks like it'd work OK, but doesn't seem standard.Your
ListLookup.Add method seems broken; it never adds the value; I'd probably just replace that class with a Dictionary or something since it doesn't seem to add much value but could be a breeding ground for bugs.No reason to
throw on GetEnumerator; just return the generic IEnumerator.What happens if
oldData or newData is null? Looks like a NRE, when I'd expect a list of all the added or deleted
Ids instead.Context
StackExchange Code Review Q#97040, answer score: 2
Revisions (0)
No revisions yet.