Johnny_D
Johnny_D

Reputation: 4652

Usage of braces without definition of function, struct etc

While analysing of Orchard.cms source code, I've found interesting code, when brackets in C# were used not for definition of any internal object. Seem it was made for some context usage. Can you please clarify what's goal of using brackets like this?

Here is code sample of it:

 builder.RegisterType<DefaultOrchardHost>().As<IOrchardHost>().As<IEventHandler>().SingleInstance();
 {
     builder.RegisterType<ShellSettingsManager>().As<IShellSettingsManager>().SingleInstance();

     builder.RegisterType<ShellContextFactory>().As<IShellContextFactory>().SingleInstance();
     {
         builder.RegisterType<ShellDescriptorCache>().As<IShellDescriptorCache>().SingleInstance();

         builder.RegisterType<CompositionStrategy>().As<ICompositionStrategy>().SingleInstance();
         {
             builder.RegisterType<ShellContainerRegistrations>().As<IShellContainerRegistrations>().SingleInstance();
             builder.RegisterType<ExtensionLoaderCoordinator>().As<IExtensionLoaderCoordinator>().SingleInstance();
             builder.RegisterType<ExtensionMonitoringCoordinator>().As<IExtensionMonitoringCoordinator>().SingleInstance();
             builder.RegisterType<ExtensionManager>().As<IExtensionManager>().SingleInstance();
             {
                 builder.RegisterType<ExtensionHarvester>().As<IExtensionHarvester>().SingleInstance();
                 builder.RegisterType<ModuleFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Modules" }));
                 builder.RegisterType<CoreModuleFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Core" }));
                 builder.RegisterType<ThemeFolders>().As<IExtensionFolders>().SingleInstance()
                     .WithParameter(new NamedParameter("paths", new[] { "~/Themes" }));

                 builder.RegisterType<CoreExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<ReferencedExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<PrecompiledExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<DynamicExtensionLoader>().As<IExtensionLoader>().SingleInstance();
                 builder.RegisterType<RawThemeExtensionLoader>().As<IExtensionLoader>().SingleInstance();
             }
         }

         builder.RegisterType<ShellContainerFactory>().As<IShellContainerFactory>().SingleInstance();
     }

     builder.RegisterType<DefaultProcessingEngine>().As<IProcessingEngine>().SingleInstance();
 }

Upvotes: 4

Views: 181

Answers (3)

lightbricko
lightbricko

Reputation: 2709

The goal in this case is only to make it easier for a developer/human to understand how the types relate hierarchically when reading the code in order to easier understand the code.

(Using curly braces like this will indent the lines in most IDE's, such as Visual Studio)

Upvotes: 5

Henk Holterman
Henk Holterman

Reputation: 273374

Can you please clarify what's goal of using brackets like this?

They are simply blocks. They form a scope, as in :

Method1(1);

{
   int i = 1;  // i is local to this {} block
   Method1(i++);
}
// Here i does not exist any more

Note that the block is in no way related to the previous statement, the lack of empty lines in your sample can be misleading.

But in the posted code, no variables are declared. So they are completely superfluous. I guess they are left over from some code pre-processing.

Upvotes: 4

Tim
Tim

Reputation: 15237

It is simply an organizational thing in this instance. The curly braces create a scope, so that any variables declared inside the brackets are local to that block of code. So that's the normal purpose for them.

That said, in the example you give, there's nothing declared in any of them. In this case, the Orchard folks are just doing it as an organizational method to make the code easier to understand. I'm guessing if you look at the classes in the example you gave, you'd see that they are related in a hierarchical manner similar to the way they are grouped by the curly braces.

Upvotes: 1

Related Questions