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" ] ) ;