Free interface configuration with lambdas in C #

Many open source projects use the configuration class and lambda to refine the configuration of a complex object. For example, a massive transition . A simple configuration would be like this.

Bus.Initialize(sbc =>
        {
            sbc.UseMsmq();
            sbc.VerifyMsmqConfiguration();
            sbc.VerifyMsDtcConfiguration();
            sbc.UseMulticastSubscriptionClient();
            sbc.ReceiveFrom("msmq://localhost/test");
        });

When you hover over Initializein Visual Studio, it says the parameter to call the method Action<ServiceBusConfigurator>. I was wondering if anyone could show a simple example of how to use this template for a class. I don’t even know what to name this type of template, and my “GoogleFu” is not working yet. In this particular case, I understand that the method works according to a singleton pattern. But I'm sure this is an instance method for a class.

+5
source
3

An Action<ServiceBusConfigurator> - , ServiceBusConfigurator, "" (void).

.NET BCL ( 3.5) : Action<T>, Action<T1, T2> ( ..) , , Func<Tresult>, Func<T, Tresult> ( ..). ) , Tresult.

, , , - . Bus.Initialize ServiceBusConfigurator (sbc), sbc .

. , :

 // this is not actual mass transit source code
 public class BusCreator
 {
     public static IBus Initialize(Action<IConfiguration> action)
     {
         // create the config instance here
         IConfiguration config = CreateDefaultConfig();

         // let callers modify it
         action(config);

         // use the final version to build the result
         return config.Build()
     }
 }

, ( IBus ) . , :

 IBus result = BusCreator.Configure(cfg => cfg.BusType = BusType.MSMQ);

:

  • , . , Configure .

  • cfg Configure . ( ).

+5

, , " " . Action - , .

+1

" ", , , , .

:

, -, . , , , .

- , , . , ( ) : , .

, Bus. , , :

  • ServiceBusConfigurator , Initializa() ( );
  • Bus / ServiceBusConfigurator, Initialize() this ( );
  • Buscan be static and visible to ServiceBusConfigurator, which, in turn, can change configuration properties Buswhen called ReceiveFrom()(extremely confusing and, hopefully, very unlikely).

Here are some strategies that have just come to mind. Many others can be offered!

0
source

All Articles