2016-02-26 7 views
6

में मैं IOption<T> को CommandBus पर पास करता हूं, इसलिए मैं अपने ServiceBusSetting कक्षा से सेटिंग्स प्राप्त कर सकता हूं। मैं अपनी बस का एकीकरण परीक्षण करना चाहता हूं। मैं इसे हल नहीं करना चाहता हूं बस new QueueCommandBus का उपयोग करें और इसे IOptions पास करने की आवश्यकता है।आईओपीएस के साथ एकीकरण परीक्षण <> .NET कोर

var services = new ServiceCollection().AddOptions(); 
     services.Configure<ServiceBusAppSettings>(Configuration.GetSection("ServiceBus")); 
     var options = services.BuildServiceProvider().GetService<IOptions<ServiceBusAppSettings>>(); 

     ////Act 
     var commandBus = new QueueCommandBus(options); 

यह ठीक काम करता है, लेकिन मेरे परीक्षण परियोजना में मेरी appsetting.json से IOptions<T> पाने के लिए बहुत जटिल कोड लगता है।

कोई सुराग अगर यह एकमात्र तरीका है या क्या कोई बेहतर तरीका है?

उत्तर

3

आपको ServiceCollection या IServiceProvider बनाने की आवश्यकता नहीं है। IConfiguration इंटरफ़ेस एक Bind() विधि है, या .NET कोर के बाद से 1.1, Get<T> जो आप दृढ़ता से टाइप वस्तु सीधे प्राप्त करने के लिए उपयोग कर सकते हैं:

var config = Configuration.GetSection("ServiceBus"); 

// .NET Core 1.0 
var options = new ServiceBusAppSettings(); 
config.Bind(options); 

// .NET Core 1.1 
var options = config.Get<ServiceBusAppSettings>(); 

मैं अपने AppSettings strongly- करने के लिए इन के रूप में स्थिर तरीकों जोड़ना चाहते टाइप की गई वस्तु, जेएसओएन से उन्हें अपने वेब ऐप और यूनिट परीक्षणों से लोड करने के लिए सुविधाजनक बनाने के लिए।

AppSettings.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.Extensions.Configuration; 

namespace My.Namespace 
{ 
    public class AppSettings 
    { 
     public class ServiceBusAppSettings 
     { 
      public string Setting1; 
      public int Setting2; 
     } 

     public class ApiSettings 
     { 
      public bool FormatJson { get; set; } 
     } 

     public class MySqlSettings 
     { 
      public string User { get; set; } 
      public string Password { get; set; } 
      public string Host { get; set; } 
      public string Database { get; set; } 
      public int Port { get; set; } = 3306; 

      public string GetConnectionString() 
      { 
       return $"Server={Host};Database={Database};Port={Port};Uid={User};Pwd={Password}"; 
      } 

     } 

     public ServiceBusAppSettings ServiceBus { get; set; } = new ServiceBusAppSettings(); 
     public ApiSettings Api { get; set; } = new ApiSettings(); 
     public MySqlSettings MySql { get; set; } = new MySqlSettings(); 

     // Static load helper methods. These could also be moved to a factory class. 
     public static IConfigurationRoot GetConfiguration(string dir) 
     { 
      return GetConfiguration(dir, null); 
     } 

     public static IConfigurationRoot GetConfiguration(string dir, string environmentName) 
     { 
      if (string.IsNullOrEmpty(environmentName)) 
       environmentName = "Development"; 

      var builder = new ConfigurationBuilder() 
       .SetBasePath(dir) 
       .AddJsonFile("appsettings.json", true, true) 
       .AddJsonFile($"appsettings.{environmentName}.json", true) 
       .AddEnvironmentVariables(); 

      return builder.Build(); 
     } 

     public static AppSettings GetSettings(string dir) 
     { 
      return GetSettings(dir, null); 
     } 

     public static AppSettings GetSettings(string dir, string environmentName) 
     { 
      var config = GetConfiguration(dir, environmentName); 
      return GetSettings(config); 
     } 

     public static AppSettings GetSettings(IConfiguration config) 
     { 
      return config.Get<AppSettings>(); 
     } 
    } 
} 

ASP.NET कोर Startup.cs: (हो रही जोरदार टाइप सेटिंग्स आपत्ति इस स्तर पर अक्सर उपयोगी है, अन्य सेवाओं को विन्यस्त जब ...)

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     Configuration = AppSettings.GetConfiguration(env.ContentRootPath, env.EnvironmentName); 
    } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Configure the service collection. 
     services.AddOptions(); 
     services.Configure<AppSettings>(Configuration); 

     // It can also be handy to get the AppSettings object here. 
     var settings = AppSettings.GetSettings(Configuration); 

     // Add framework services. 
     services.AddMvc() 
      .AddJsonOptions(options => 
      { 
       options.SerializerSettings.ContractResolver = new DefaultContractResolver(); 
       // Pretty-print JSON in Development 
       options.SerializerSettings.Formatting = settings.Api.FormatJson ? Formatting.Indented : Formatting.None; 
      }); 

     // Store DB connection info in AppSettings too... 
     var conn = settings.MySql.GetConnectionString(); 
     services.AddDbContext<MyDbContext>(opt => opt.UseMySql(conn)); 
    } 
} 

टेस्ट कक्षा में:

var testDir = AppContext.BaseDirectory; 
var settings = AppSettings.GetSettings(testDir, "Test"); 

//Act 
var commandBus = new QueueCommandBus(settings); 
संबंधित मुद्दे