2016-09-28 4 views
0

焼き込みアセンブリの設定をラップする設定サービスがありますが、コマンドラインでこれらの設定を上書きしたいと思います。 現在、このコードが正常に動作している:私はコマンドライン文字列を使用して設定を上書きすることができますので、jsonをインタフェースにデシリアライズする簡潔な方法は?

class Program 
    { 
     static void Main(string[] args) 
     { 
      ISettings opSettings = new OperationalSettings(); 

      var commandLineTest = "{Url:'http://overridenurl.com'}"; 

      ISettings commandSettings = new CommandLineSettings(commandLineTest); 

      var configService = new ConfigService(opSettings); 
      var configServiceUsingCmdOpts = new ConfigService(commandSettings); 
     } 
    } 

これで:

public interface ISettings 
    { 
     string Url { get; } 
    } 

    public class OperationalSettings : ISettings 
    { 
     public string Url { get { return ServiceSettings.Default.Url; } } 
    } 

    public class CommandLineModel 
    { 
     public string Url; 
    } 

    public class CommandLineSettings : ISettings 
    { 
     private readonly CommandLineModel _model; 

     public CommandLineSettings(string serialisedSettings) 
     { 
      _model = JsonConvert.DeserializeObject<CommandLineModel>(serialisedSettings); 
     } 

     public string Url { get { return _model.Url; } } 
    } 

    public class ConfigService 
    { 
     private readonly ISettings _settings; 
     public ConfigService(ISettings settings) 
     { 
      _settings = settings; 
     } 
     public ISettings settings { get { return _settings; } } 
    } 

そして、テストドライバコードを。しかし、私が好きではないことは、私は新しい設定を持っている場合、私は今、4ヶ所でこれを追加する必要があるということです。

  • インタフェース
  • 設定ラッパー(OperationalSettings)の具体的な実装
  • デシリアライズのコマンドラインモデル
  • デシリアライズされたモデルをラップするコマンドライン設定の実装。

さらにプロパティを追加すると、スケーラビリティに問題があるようです。多くのコードを変更することなくこれを達成するより効率的な方法がありますか?

+0

を使用するには、ここExpandoObjectクラス

を使用し、静的メンバを持っていませんps://msdn.microsoft.com/en-us/library/ee461504.aspx –

+0

@BRAHIMKamel私はCommandLineModelを動的にスワップしましたが、これはうまくいくようです。これは唯一の解決策ですか? – user183872

+0

私の答えを見てくださいあなたのケースで便利かもしれないビルトインオブジェクトがあります –

答えて

0

あなたが唯一の実行時にメンバーを追加および削除することができ、オブジェクトを必要とするが、それは特定の操作を定義する必要がないような単純なシナリオを持っていれば、そうでない場合DynamicObject

public class CommandLineModelDictionary : DynamicObject 
    { 
     // The inner dictionary. 
     Dictionary<string, object> dictionary 
      = new Dictionary<string, object>(); 

     // This property returns the number of elements 
     // in the inner dictionary. 
     public int Count 
     { 
      get 
      { 
       return dictionary.Count; 
      } 
     } 

     // If you try to get a value of a property 
     // not defined in the class, this method is called. 
     public override bool TryGetMember(
      GetMemberBinder binder, out object result) 
     { 
      // Converting the property name to lowercase 
      // so that property names become case-insensitive. 
      string name = binder.Name.ToLower(); 

      // If the property name is found in a dictionary, 
      // set the result parameter to the property value and return true. 
      // Otherwise, return false. 
      return dictionary.TryGetValue(name, out result); 
     } 

     // If you try to set a value of a property that is 
     // not defined in the class, this method is called. 
     public override bool TrySetMember(
      SetMemberBinder binder, object value) 
     { 
      // Converting the property name to lowercase 
      // so that property names become case-insensitive. 
      dictionary[binder.Name.ToLower()] = value; 

      // You can always add a value to a dictionary, 
      // so this method always returns true. 
      return true; 
     } 
    } 

を見てかかることがありますあなたは動的オブジェクトでHTTを見てみることができる方法

class Program 
    { 
     public static dynamic Dyn { get; set; } 

     static void Main(string[] args) 
     { 
      dynamic model= new CommandLineModelDictionary(); 
      model.Prop1 = "Foo"; 
      model.Prop2 = "toto"; 



      Console.WriteLine(model.Prop1); 
      Console.WriteLine(model.Prop2); 
      //otherwise you can use 
      dynamic dynModel = new ExpandoObject(); 
      dynModelop1 = "Test1"; 
      dynModel2 = "Test2"; 
      Console.WriteLine(dynModel.Prop1); 
      Console.WriteLine(dynModel.Prop2); 


     } 
    } 
+0

私は本当に私は現在のアセンブリの設定を取得することができることを言及することを忘れて、 json経由のコマンドラインで。 Jqueryのようなものが少しあります。私はAutoMapperを試しましたが、これを動作させるために苦労しました。 – user183872

関連する問題