I have a solution with many projects:
Yupii.Games -> Class Library where settings, utilities are shared with projects
Yupii.Games.Web -> Web app
Yupii.Games.Api -> Web api
I am using mongoDB as database and I have many collections but I want to respect collection name convention for example:
Monster (is an entity) to monsters the collection name.
I have a json file in Yupii.Games named GlobalSettings.json:
"MongoDBCollections": {
"Collections": [
{ "Monster": "monsters" },
{ "User": "users" }
...
]}
In the Yupii.Games.Api I have something like:
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;
)};
}
How can I reach to GlobalSettings.json -> "MongoDBCollections" -> Collections[] to have also a service for :
services.Configure<GlobalSettings>(options =>
{
options.Collections = CollectionsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();
});
Then loop to get the associate collection name:
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);
I want to be able to do the same in Yupii.Games.Web
Actually, I am able to make every to work fine in a different way but the code and the structure is awful. If any body can show a better and clean way plz.
I am using Visual Studio 2017 asp.net core 2.0
I hope everything is clear...
UPDATE
In Yupii.Games.Web one of the method make calls this way:
HttpResponseMessage response = client.GetAsync(RESTVersion + GetCollectionName() + e).Result;
and the GetCollectionName():
public string GetCollectionName()
{
string collectionName = typeof(T).Name;
... // get the collection name by associating the typeof(T)
return name;
}
In Yupii.Games.Api --> Startup.cs
this is what the startup constructor look like:
Both GlobalSettings json files are in Yupii.Games (Class Library).
I add a property:
then in ConfigureServices:
MongoDBSettings class don't have a Collections property but inherit from GlobalSettings.cs in Yupii.Games project
Before I can loop through:
Click right on both GlobalSettings json files --> go to properties --> and copy to Output Directory should be copy always
So now I can loop through the collections :
So this is it! Thanks for helping guys (@Jaya and @BOR4) :)