This project has moved and is read-only. For the latest updates, please go here.

Id as int!

Aug 25, 2013 at 8:15 PM
In the documentation, I read that you can implement IEntity to modify the Id content. But how can I choose a different type for the Id? I wish to store my Id as an int, and not a string representation of a number, is that possible?
Aug 25, 2013 at 10:17 PM
No you can't. Not with MongoRepository. The whole point of an Interface is that it guarantees an implementation. The IEntity interface only requires one thing: the object must implement an Id property of type string.

Is there a specific reason you require an int? It might not be as problematic as one would think (in terms of performance etc.). If you insist on using an int, use the Mongo C# driver directly; MongoRepository is just a simple abstraction layer on top of it.
Aug 26, 2013 at 12:17 PM
Because this way, I would get to keep my old entities made for Entity Framework and I could switch my IoCContainer to resolve Mongorepositories instead of SQLRepositories whenever I want.
Aug 26, 2013 at 12:28 PM
I started work on IEntity<T> (where Id is T) this morning; I'm not sure if this will work out throughout the rest of the project and it might take a few days until I get around to finishing it but it should provide a means of using another type for Id.
Aug 26, 2013 at 12:34 PM
Sounds great, looking forward to it =) !
Aug 28, 2013 at 7:22 PM
Edited Oct 26, 2013 at 12:58 AM
See this commit. There's some work to be done (mostly updating the XML Doc) but most importantly I'd like to keep the interface in tact (see the TODO comments in the "unit test" file RepoTests.cs (which, at some point, need to be thrown overboard and implemented decently as well)). I'm not quite sure if that can be done, and I haven't got the time right now to sit down for it. Maybe later this week. But if you could download this version and give it a whirl and see if this solves your problem (and maybe encounter new ones) that would be nice.

I managed to keep the interface intact. Jay!
Sep 1, 2013 at 10:25 AM
Edited Sep 1, 2013 at 10:35 AM
I just gave the Project a look. It doesn't seem to be possible to initialize a MongoRepository<T,U>without specifying the redundant U. To simplify what I'm trying to say :
    class Foo<A, B>
        where A : Bar<B>{}

    class Bar<T> { }

    class ConcreteBar : Bar<string>{}

    var foo = new Foo<ConcreteBar,string>();   // There is no way to initialize it like new Foo<ConcreteBar>();

But besides that , I'm not seeing any obstacles to why this should not solve the problem of having entities with id of T. I Agree that it may look bad to other users though.

Anyway, as for my own Project, I decided to completely rewrite my database. After all, if I use MongoDB, I actually want the _id to be type of ObjectId.