static void AddDataServicesTo(IServiceCollection services, IConfigurationRoot _configuration) { var mongoConnectionString = _configuration["mongoConnectionString"]; var hasMongoCongured = !string.IsNullOrWhiteSpace(mongoConnectionString); if (hasMongoCongured) { services.AddScoped(typeof(IRepositoryContext), (serviceProvider) => { // @jijiechen: detect at every time initate a new IRepositoryContext // may cause a performance issue if (!MongoDbUtils.DatabaseExists(mongoConnectionString)) { throw new ApplicationException("Could not find a database using specified connection string"); } return(new MongoRepositoryContext(mongoConnectionString)); }); services.AddScoped(typeof(Repository <,>), typeof(MongoRepository <,>)); } else { var dataContext = new InMemoryResponsitoryContext(); services.AddScoped(typeof(IRepositoryContext), (serviceProvider) => dataContext); services.AddScoped(typeof(Repository <,>), typeof(InMemoryDataRepository <,>)); } services.AddScoped(typeof(IDataRepository <>), typeof(BaseDataRepository <>)); }
internal static BsonDocument[] GetByName(string name) { name = MongoDbUtils.DotsToApostrophes(name.ToLower()); var pipeline = new[] { MQB.Match(MQB.And(new BsonDocument("name", name))), MQB.AddFields(new BsonDocument("namesegments", MQB.Split("$name", "'"))), MQB.AddFields(new BsonDocument("name", MQB.Substr(MQB.Reduce("$namesegments", "", MQB.Concat("$$value", ".", "$$this")), 1, -1))), MQB.Project(new BsonDocument("_id", 0).Add("assets", 0).Add("namesegments", 0)) }; return(pipeline); }
internal static BsonDocument[] GetWithTermsByDomain(string domain) { domain = MongoDbUtils.DotsToApostrophes(domain); var pipeline = new[] { MQB.Match(MQB.And(new BsonDocument("domain", domain))), MQB.AddFields(new BsonDocument("domsegments", MQB.Split("$domain", "'"))), MQB.AddFields(new BsonDocument("domain", MQB.Substr(MQB.Reduce("$domsegments", "", MQB.Concat("$$value", ".", "$$this")), 1, -1))), MQB.AddFields(new BsonDocument("terms", MQB.Filter("$terms", "term", MQB.And("$$term.scope", BASE_TERMS_SCOPE)))), MQB.AddFields(new BsonDocument("terms", MQB.ArrayElemAt("$terms", 0))), MQB.Project(new BsonDocument("domsegments", 0)) }; return(pipeline); }
internal static BsonDocument[] Filter(string name, bool startWith) { name = MongoDbUtils.DotsToApostrophes(name.ToLower()); string pattern = startWith ? $"/^{name}/" : $"/.*{name}.*/"; var pipeline = new[] { MQB.Match(MQB.And(new BsonDocument("name", new BsonRegularExpression(pattern)))), MQB.AddFields(new BsonDocument("namesegments", MQB.Split("$name", "'"))), MQB.AddFields(new BsonDocument("name", MQB.Substr(MQB.Reduce("$namesegments", "", MQB.Concat("$$value", ".", "$$this")), 1, -1))), MQB.Project(new BsonDocument("_id", 0).Add("assets", 0).Add("namesegments", 0)) }; return(pipeline); }