views:

99

answers:

1

Hello everybody,

After hacking more on my current app - once again, I am running into an issue which kills some of the joy I expected from my domain model.

The problem here is that the aggregate root / most important class in my domain model doesn't have consistent ID values for its entries. (E.g messed up : 1..3..5..12..150..157.. and so on | this is because it's a database that is being updated since MS Access 1997 and now upsized to MSSQLServer).

Is there a way to still get the real next object in the current collection ( which means that it is not just based on its ID value ) ?

What would you guys suggest me to do in this case?

And is it a good idea to:

  • Delete ID field and regenerate it?
  • Create some HelperMethods to get the next object in the current? (How would you achieve that.. ?)
+2  A: 

Are the IDs at least ordered, even if there are gaps? If so, simply order by the id and iterate over the enumeration. If you need access to the next object within a filter (Where for instance), you can use the extension method that provides access to the index in the collection.

Ex. 1:

 var query = db.Root.OrderBy( r => r.ID );
 foreach (var obj in query)
 {
    ....
 }

Ex. 2:

var query = db.Root.Where( (r,i) => db.Root.ElementAt(i).Name == r.Name );
...
tvanfosson
a little question though:How do I determine where the i is in (r,i). I have got an IEnumerable and my code iterates trough it now.Should I create a field in my ViewModel and set it every time I iterate through it? (int ItemNr) ?
Shaharyar
Why would you need to know the position in the collection in your ViewModel? I would think you would store the ID of the item in the ViewModel and simply retrieve it by ID: `db.Root.SingleOrDefault( r => r.ID == id )` or for a collectin of models chained to the next item `db.Root.Where( (r,i) => new RootModel { ID = r.ID, NextID = db.Root.ElementAt(i+1) != null ? db.Root.ElementAt(i+1).ID : null } )`
tvanfosson