全局 Json 文件在一个解决方案中与多个项目共享(asp.net 核心 2.0)
Gloabal Json file shared with many projects in one solution (asp.net core 2.0)
我有很多项目的解决方案:
Yupii.Games -> Class Library where settings, utilities are shared with projects
Yupii.Games.Web -> Web app
Yupii.Games.Api -> Web api
我正在使用 mongoDB 作为数据库,我有很多集合,但我想遵守集合名称约定,例如:
Monster(是一个实体)到monsters集合名称。
我在 Yupii.Games 中有一个名为 GlobalSettings.json 的 json 文件:
"MongoDBCollections": {
"Collections": [
{ "Monster": "monsters" },
{ "User": "users" }
...
]}
在 Yupii.Games.Api 我有类似的东西:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MongoDbSettings>(options =>
{
options.ConnectionString = Configuration.GetSection("MongoConnection:ConnectionString").Value;
options.Database = Configuration.GetSection("MongoConnection:Database").Value;
)};
}
我如何到达 GlobalSettings.json -> "MongoDBCollections" -> Collections[] 以获得服务:
services.Configure<GlobalSettings>(options =>
{
options.Collections = CollectionsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();
});
然后循环获取关联集合名称:
var collectionName = typeof(T).Name;
foreach (var collection in CollectionList)
{
if (collectionName == collection.Key)
return Database.GetCollection<T>(collection.Value);
}
return Database.GetCollection<T>(collectionName);
我希望能够在 Yupii.Games.Web
中执行相同的操作
实际上,我能够以不同的方式使每一个都正常工作,但是代码和结构很糟糕。如果任何人可以展示更好更干净的方式请。
我正在使用 Visual Studio 2017 asp.net 核心 2.0
我希望一切都清楚...
更新
在Yupii.Games.Web 中,方法之一是这样调用的:
HttpResponseMessage response = client.GetAsync(RESTVersion + GetCollectionName() + e).Result;
和 GetCollectionName():
public string GetCollectionName()
{
string collectionName = typeof(T).Name;
... // get the collection name by associating the typeof(T)
return name;
}
我建议您避免从配置中读取任何内容。只需使用最适合您的通用方法即可:
MongoCollection = mongoDatabase.GetCollection<T>(typeof(T).Name.ToLower() + "s");
这样你就永远不必更新配置,你有一个命名约定,如果你想改变命名约定,你只需要在一个地方改变它....
PS 我试图了解您的具体问题,但在多次阅读您的问题后,我仍然不确定您到底需要什么。
在Yupii.Games.Api --> Startup.cs
这是启动构造函数的样子:
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
GlobalSettingsConfiguration = new ConfigurationBuilder()
.AddJsonFile("GlobalSettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"GlobalSettings.{env.EnvironmentName}.json", optional: true)
.Build();
}
两个 GlobalSettings json 文件都在 Yupii.Games(Class 库中)。
我加一个属性:
public IConfiguration GlobalSettingsConfiguration { get; }
然后在配置服务中:
services.Configure<MongoDbSettings>(options =>
{
...
options.Collections = GlobalSettingsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();
});
MongoDBSettings class 没有集合 属性 但继承自 Yupii.Games 项目中的 GlobalSettings.cs
在我循环之前:
"MongoDBItems": {
"Collections": {
"Monster": "monsters",
"User": "users",
"Matrix": "matrices"
}
右击两个 GlobalSettings json 文件 --> 转到 属性 --> 并复制到输出目录应该是总是复制
所以现在我可以循环浏览集合了:
foreach (var collection in CollectionList)
{
if (collectionName == collection.Key)
return Database.GetCollection<T>(collection.Value);
}
就是这样!
感谢您的帮助(@Jaya 和@BOR4):)
我有很多项目的解决方案:
Yupii.Games -> Class Library where settings, utilities are shared with projects
Yupii.Games.Web -> Web app
Yupii.Games.Api -> Web api
我正在使用 mongoDB 作为数据库,我有很多集合,但我想遵守集合名称约定,例如:
Monster(是一个实体)到monsters集合名称。
我在 Yupii.Games 中有一个名为 GlobalSettings.json 的 json 文件:
"MongoDBCollections": {
"Collections": [
{ "Monster": "monsters" },
{ "User": "users" }
...
]}
在 Yupii.Games.Api 我有类似的东西:
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<MongoDbSettings>(options =>
{
options.ConnectionString = Configuration.GetSection("MongoConnection:ConnectionString").Value;
options.Database = Configuration.GetSection("MongoConnection:Database").Value;
)};
}
我如何到达 GlobalSettings.json -> "MongoDBCollections" -> Collections[] 以获得服务:
services.Configure<GlobalSettings>(options =>
{
options.Collections = CollectionsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();
});
然后循环获取关联集合名称:
var collectionName = typeof(T).Name;
foreach (var collection in CollectionList)
{
if (collectionName == collection.Key)
return Database.GetCollection<T>(collection.Value);
}
return Database.GetCollection<T>(collectionName);
我希望能够在 Yupii.Games.Web
中执行相同的操作
实际上,我能够以不同的方式使每一个都正常工作,但是代码和结构很糟糕。如果任何人可以展示更好更干净的方式请。
我正在使用 Visual Studio 2017 asp.net 核心 2.0
我希望一切都清楚...
更新
在Yupii.Games.Web 中,方法之一是这样调用的:
HttpResponseMessage response = client.GetAsync(RESTVersion + GetCollectionName() + e).Result;
和 GetCollectionName():
public string GetCollectionName()
{
string collectionName = typeof(T).Name;
... // get the collection name by associating the typeof(T)
return name;
}
我建议您避免从配置中读取任何内容。只需使用最适合您的通用方法即可:
MongoCollection = mongoDatabase.GetCollection<T>(typeof(T).Name.ToLower() + "s");
这样你就永远不必更新配置,你有一个命名约定,如果你想改变命名约定,你只需要在一个地方改变它....
PS 我试图了解您的具体问题,但在多次阅读您的问题后,我仍然不确定您到底需要什么。
在Yupii.Games.Api --> Startup.cs
这是启动构造函数的样子:
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
GlobalSettingsConfiguration = new ConfigurationBuilder()
.AddJsonFile("GlobalSettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"GlobalSettings.{env.EnvironmentName}.json", optional: true)
.Build();
}
两个 GlobalSettings json 文件都在 Yupii.Games(Class 库中)。
我加一个属性:
public IConfiguration GlobalSettingsConfiguration { get; }
然后在配置服务中:
services.Configure<MongoDbSettings>(options =>
{
...
options.Collections = GlobalSettingsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();
});
MongoDBSettings class 没有集合 属性 但继承自 Yupii.Games 项目中的 GlobalSettings.cs
在我循环之前:
"MongoDBItems": {
"Collections": {
"Monster": "monsters",
"User": "users",
"Matrix": "matrices"
}
右击两个 GlobalSettings json 文件 --> 转到 属性 --> 并复制到输出目录应该是总是复制
所以现在我可以循环浏览集合了:
foreach (var collection in CollectionList)
{
if (collectionName == collection.Key)
return Database.GetCollection<T>(collection.Value);
}
就是这样! 感谢您的帮助(@Jaya 和@BOR4):)