ServiceStack and MongoDB

1,649 views
Skip to first unread message

quaffapint

unread,
Apr 30, 2012, 8:29:42 PM4/30/12
to servic...@googlegroups.com
Been really digging MongoDB.  I have a servicestack app I've been working on tied into SQL server, and would love to move away from sql server to mongo, which really fits my needs nicely.  Problem is I don't know how to tie in servicestack to use it.  Right now I'm using OrmLite, along with the built-in credentials auth.

Any suggestions on how best to use something like MongoDB with servicestack, especially being able to use auth with it?

Thanks.
matt

Demis Bellot

unread,
Apr 30, 2012, 8:49:17 PM4/30/12
to servic...@googlegroups.com
As ServiceStack's IOC isn't tied to any particular technology, getting it to work with MongoDb shouldn't be any different to what you're use to. 
Here's something IOC registration found off the InterWebs:

var connString = ConfigurationManager.AppSettings["MONGOHQ_URL"];
var databaseName = connString.Split('/').Last();
var server = MongoServer.Create(connString);
var database = server.GetDatabase(databaseName);
if (!database.CollectionExists("Messages"))
database.CreateCollection("Messages");
container.Register<MongoServer>(server);
container.Register<MongoDatabase>(database);
container.Register<MongoCollection<Message>>(database.GetCollection<Message>("Messages"));

As for having it work as a ServiceStack Auth backend, it just needs to implement IUserAuthRepository, here's the OrmLite and Redis UserAuthRepo back-ends you can use for inspiration :)



Cheers,

Davis

unread,
May 27, 2012, 12:37:47 AM5/27/12
to servic...@googlegroups.com
Hi Demis, as I'm looking at implementing the sample code and AuthRepository, however, I've got a small issue.

The design is to have different mongo db for different session, depending on the MVC route (POC for multi-tenant).

Perhaps you can enlighten me a little on how to design the AuthRepository with dynamic database connection?

Thanks in advance.

Demis Bellot

unread,
May 27, 2012, 2:11:19 AM5/27/12
to servic...@googlegroups.com
There really is nothing magical about Funq - whatever you register gets injected into your services in the lifetime policy you chose.

So it's just basic C# from there, if you don't know what Mongo DB you need to return until runtime, then I'd just register a custom factory that lets a service pass in a shard Id and then the Factory just returns the appropriate db and collection for that shard, something like:

AppHost:

AppSettiongs appSettings = new AppSettings();
container.Register<IMyMongoDbManager>(c => new MyMongoDbManager(appSettings.Get<List<MongoDbConfig>>("MongoDbConfigs")));

Service:

[RestService("/users/{ShardId}/{Id}")]
class MyShardedRequest {
    public string ShardId { get; set; }
    public string Id { get; set; }
}
class MyShardedRequestService : ServiceBase<MyShardedRequest> {
   public IMyMongoDbManager MongoManager { get; set; }
   public object Run(MyShardedRequest request) {
       var mongoUsers = MongoManager.GetDbCollection<User>(request.ShardId);
       var user = mongoUsers.FindOne(Query.EQ("_id", ObjectId.Parse(request.Id)));
       ...
   }
}

Davis

unread,
May 27, 2012, 11:18:13 AM5/27/12
to servic...@googlegroups.com
Thanks for the idea, Demis.

Anyway, I found that my User class is quite different from UserAuth.cs class, and since the UserAuth.cs is based on Int Id (autoincrement), it is different from Mongo's ObjectId type.

I'm planning to create a localized version of ServiceStack.ServiceInterface.Auth classes, say  MyLib.ServiceInterface.Auth. Will it break anything on the way?

Perhaps, I can then use the rest of the ServiceStack MVC Powerpack with a custom-made Auth namespace?


On Sunday, 27 May 2012 14:11:19 UTC+8, mythz wrote:
There really is nothing magical about Funq - whatever you register gets injected into your services in the lifetime policy you chose.

So it's just basic C# from there, if you don't know what Mongo DB you need to return until runtime, then I'd just register a custom factory that lets a service pass in a shard Id and then the Factory just returns the appropriate db and collection for that shard, something like:

AppHost:

AppSettiongs appSettings = new AppSettings();
container.Register<IMyMongoDbManager>(c => new MyMongoDbManager(appSettings.Get<List<MongoDbConfig>>("MongoDbConfigs")));

Service:

[RestService("/users/{ShardId}/{Id}")]
class MyShardedRequest {
    public string ShardId { get; set; }
    public string Id { get; set; }
}
class MyShardedRequestService : ServiceBase<MyShardedRequest> {
   public IMyMongoDbManager MongoManager { get; set; }
   public object Run(MyShardedRequest request) {
       var mongoUsers = MongoManager.GetDbCollection<User>(request.ShardId);
       var user = mongoUsers.FindOne(Query.EQ("_id", ObjectId.Parse(request.Id)));
       ...
   }
}
Reply all
Reply to author
Forward
0 new messages