PowerUp是.NET Core的擴展方法庫,它為框架添加了有用的功能。
powerupcore nuget
powerupcore azure nuget
⏩輕量級:目標不是包含5K方法,而是只有日常有用的方法(我認為)
.NET核心兼容
單位測試
100%記錄
解釋了所有擴展方法,並在以下文檔中提供了理由。
簡化驗證wehather所需的語法是字符串內容是否為inter。
為什麼?
刪除重複代碼
if ( "42" . IsInteger ( ) )
Foo ( ) ;從字符串中刪除參數字符串的內容。
為什麼?
刪除重複代碼
"My text" . Remove ( "My" ) // result: " text"
// instead of
"My text" . Replace ( "My" , string . Empty ) ; // result: " text"
Foo ( ) ;給字符串方法格式提供較短的語法。
為什麼?
使代碼短
// .net syntax
string . Format ( "Debug Level: {0} " {1} " {3}" , DebugLevel . Info , "Everything is awesome!" , DateTime . Now ) ;
// PowerUp syntax
"Debug Level: {0} " {1} " {3}" . Format ( DebugLevel . Info , "Everything is awesome!" , DateTime . Now ) ;允許輕鬆將TRING轉換為枚舉。
為什麼?
刪除重複代碼
private enum TestEnum
{
Val1 ,
Val2 ,
Val3
}
var enumVar = "Val1" . ToEnum < TestEnum > ( ) ; 允許獲取枚舉值的可讀描述。
private enum TestEnum
{
[ Description ( "Value with description" ) ]
ValWithDesc = 1 ,
ValNoDesc = 2 ,
AnotherNoDesc = 3
}
var testObject = TestEnum . ValWithDesc ;
string description = testObject . GetDescription ( ) ; 有助於立即從集合中刪除更多元素。
為什麼?
為收藏提供有用的添加功能
sourceList . RemoveRange ( deleteList ) ;執行深層副本框架的集合。
var testList = _fixture . Create < List < clonableObj > > ( ) ;
var clone = testList . Clone ( ) ;
clone . First ( ) != testList . First ( )獲取集合的最後一個索引。
為什麼?
刪除重複代碼
sourceList . GetLastIndex ( ) == ( sourceList . Count - 1 ) 如果給定的參數為null,則引發參數nullexception。
為什麼?
替換.net核心中的guard.argumentnotnull
objectShouldNotBeNUll . ThrowIfNull ( nameof ( objectShouldNotBeNUll ) ) ;
// Inspired on Microsoft.Practices.EnterpriseLibrary.Common.Utility
Guard . ArgumentNotNull ( objectShouldNotBeNUll , nameof ( objectShouldNotBeNUll ) ) ;驗證對像是否為null或不是null。
為什麼?
使語法驗證無效的清潔劑和更人性化的可讀性
var someObject = new object ( ) ;
//Before
if ( someObject != null )
Foo ( ) ;
//PowerUp
if ( someObject . isNull ( ) )
Foo ( ) ;驗證對象值是否在下限和上限之間。
為什麼?
為了簡化語法以驗證一個對象值是否在某個範圍之間
if ( 5 . Between ( 2 , 8 ) )
Foo ( ) ;
if ( 7 . Between ( 7 , 12 , BetweenOptions . Inclusive ) )
Foo ( ) ; 允許僅登錄有關調用方法的信息。
為什麼?
為了避免無聊的代碼並複制粘貼問題,尖端方案是在控制器方法的開頭:
[ HttpPut ]
[ Route ( "[action]" ) ]
[ Produces ( "application/json" ) ]
[ ProducesResponseType ( typeof ( Product ) , StatusCodes . Status201Created ) ]
[ ProducesResponseType ( typeof ( string ) , StatusCodes . Status400BadRequest ) ]
public async Task < IActionResult > AddProduct ( [ FromBody ] NewProduct newProduct )
{
_logger . LogDebug ( $ " { DateTime . UtcNow : dd/MMM/yyyy } | 32: CatalogController.AddProduct()}" ) ;
if ( ! ModelState . IsValid )
return BadRequest ( ModelState ) ;
....現在可以簡單地記錄方法:
.. .
public async Task < IActionResult > AddProduct ( [ FromBody ] NewProduct newProduct )
{
_logger . LogThisMethod ( ) ;
.. .從示例中很容易看到它可以減少代碼少量和錯誤的可能性
Azure中的存儲訪問鍵用於訪問存儲帳戶的身份驗證。
創建一個存儲帳戶時,將為您提供兩個存儲訪問密鑰,即主和輔助訪問密鑰。
請參閱更多https://blogs.msdn.microsoft.com/mast/2013/11/06/why-does-an-azure-storage-account-account-have-two-two-access-keys/
為什麼?
ReflactParse允許您使用主鍵進行冗餘的連接,或者自動切換到Seconday。
< add key = " QueueConnectionString1 " value = " DefaultEndpointsProtocol=https;AccountName=weu########## " />
< add key = " QueueConnectionString2 " value = " DefaultEndpointsProtocol=https;AccountName=weu########## " />
< add key = " QueueReference " value = " myQueueReference " /> var storageAccount = CloudStorageAccountHelper . RedundantParse (
CloudConfigurationManager . GetSetting ( "QueueConnectionString1" ) ,
CloudConfigurationManager . GetSetting ( "QueueConnectionString2" ) ) ;
var queueClient = storageAccount . CreateCloudQueueClient ( ) ;
var myQueue = queueClient . GetQueueReference ( ConfigurationManager . AppSettings [ "QueueReference" ] ) ;