views:

136

answers:

2

So, i'm looking at the TekPub sample for ASP.NET MVC2 (http://mvcstarter.codeplex.com/) using DB4o and there are a bunch of templates to create controllers etc, the generated code looks like:

    public ActionResult Details(int id)
    {
        var item = _session.Single<Account>(x=>x.ID == id);
        return View(item);
    }

Now, my understanding is that using DB4o or a simliar object database that ID's are not required, so how/what exactly do I pass to enable this kind of templated code to function?

UPDATE: Both answers were useful, I have modifed the templates to use GUID as the ID. I will add any relevant code/notes here once I see how it works out.

UPDATE: So, what I have done (which works exactly as I would expect) is 1. Add an ID to my model i.e.

public Guid ID { get; set; }
  1. Initialize the Guid in the class constructor like so

    ID = Guid.NewGuid();

and that's it, all working.

A: 

It depends.

Db4o tracks and identifies the objects by their in-memory object identity. The same object in the database is always represented by the same object in memory. For example in simple desktop application you don't need a object-id.

Now the picture changes as soon as objects are serialized or have to be identified across web-requests. When the object is serialized and deserialized again it has another object identity. So db4o doesn't recognize it anymore. The same is true for web requests. So in such scenarios it necessary to introduce an id for the objects. There are different possibilities for id's, see these SO-Questions: link db4o, Linq and UUIDs and Db4o MvcApplication

In your case above the Accound-class brings its own id-field. However how this id is generated I cannot tell from that snippet.

Gamlor
thanks, this makes sense. In this case the ID has not been created (yet)
Craig McGuff
A: 

In the MVCStarter the method you are describing is actually:

public ActionResult Details(string id)
{
var item = _session.Single<Account>(x=>x.ID == id);
return View(item);
}

And id is a string representation of a Guid.

db4o to the best of my knowledge won't generate an int ID automatically...it has no idea what the last one was. So a Guid is used on the object and set on the constructor.

Webjedi
I see now - a bit of digging, the add controller template is using int, I just need to mess with it to make it more DB4o-ised
Craig McGuff