Is it good to use extension methods only to set default properties?

I am using a set of third-party controls, and I am considering using the extension method to set some properties that are required almost anytime when this control is used in the application.

Is it good to use extension methods for this kind of use? Why or why not?

For more details, the third-party library is DevExpress, and the extension method that I want to write will disable all customization and editing on a specific control.

Therefore, instead of writing

barManager.AllowCustomization = false; barManager.AllowMoveBarOnToolbar = false; barManager.AllowQuickCustomization = false; barManager.AllowShowToolbarsPopup = false; barManager.AutoSaveInRegistry = false; foreach (Bar bar in barManager.Bars) { bar.OptionsBar.DrawDragBorder = false; } 

I could write

 barManager.DisableEditing(); 
+4
source share
1 answer

Yes, you can definitely do it. Since extension methods are just syntactic sugar for static methods, and since you want to create some static helper method to configure this instance of the object, that makes sense.

For completeness only, control configuration is sometimes performed using extension methods related to the smooth interface:

 public static class DataGridExtensions { public static DataGrid AllowColumnConfiguration(this DataGrid grid) { // Add NRE check grid.CanUserResizeColumns = true; grid.CanUserSortColumns = true; grid.CanUserReorderColumns = true; return grid; } public static DataGrid AllowEdition(this DataGrid grid) { // Add NRE check grid.CanUserAddRows = true; grid.CanUserDeleteRows = true; return grid; } } 

So you can use it this way:

 var grid = new DataGrid() .AllowColumnConfiguration() .AllowEdition(); 
+5
source

Source: https://habr.com/ru/post/1492343/


All Articles