2011-05-26 82 views
1

这是我迄今为止关于MongoDB的IRepository,并且想知道我是否在正确的线上?MongoDB IRepository数据库连接

public abstract class Repository<TEntity> : IRepository<TEntity> { 

    private const string _connection = "mongodb://localhost:27017/?safe=true"; 
    private MongoDatabase _db; 
    protected abstract string _collection{get;} 

    public Repository() { 
     this._db = MongoServer.Create(_connection).GetDatabase("Photos"); 
    } 

    public IQueryable<TEntity> FindAll() { 

     return this._db.GetCollection<TEntity>(_collection).FindAll().AsQueryable(); 
    } 
} 

这样我就可以创建我的PhotoRepository类,它从这里继承并提供所需的_collection名称。

我只是想确保我在正确的位置以正确的方式打开与db的连接。

回答

1

是的,这很好。当传递相同的连接字符串时,MongoServer.Create将返回相同的 MongoServer实例,因此可以根据需要多次调用MongoServer.Create。