简体   繁体   中英

Gloabal Json file shared with many projects in one solution (asp.net core 2.0)

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;
    }

I suggest you avoid reading anything from config. Simply have a generic method where it best suits you that does this:

MongoCollection = mongoDatabase.GetCollection<T>(typeof(T).Name.ToLower() + "s");

This way you never have to update config, you have a naming convention, if you please to change naming convention you have to change it in only 1 place ....

PS I tried to understand your specific problem but after reading several times your question I still wasn't sure what exactly you needed.

In Yupii.Games.Api --> Startup.cs
this is what the startup constructor look like:

    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();
    }

Both GlobalSettings json files are in Yupii.Games (Class Library).

I add a property:

public IConfiguration GlobalSettingsConfiguration { get; }

then in ConfigureServices:

        services.Configure<MongoDbSettings>(options =>
        {
            ...
            options.Collections = GlobalSettingsConfiguration.GetSection("MongoDBItems:Collections").Get<Dictionary<string, string>>();      
        });

MongoDBSettings class don't have a Collections property but inherit from GlobalSettings.cs in Yupii.Games project

Before I can loop through:

"MongoDBItems": {
"Collections": {
  "Monster": "monsters",
  "User": "users",
  "Matrix": "matrices"
}

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 :

        foreach (var collection in CollectionList)
        {
            if (collectionName == collection.Key)
                return Database.GetCollection<T>(collection.Value);
        }

So this is it! Thanks for helping guys (@Jaya and @BOR4) :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM