简体   繁体   English

如何避免代码中的重复逻辑?

[英]How to avoid duplication logic in the code?

I have two methods:我有两种方法:

static public async Task Delete( Guid id, string path, ViewModelCollection<ProfileData> collection )       
{                                                                                                          
    var jsonText = await File.ReadAllTextAsync( path );                                                    
    var jsonList = JsonConvert.DeserializeObject<List<ProfileData>>( jsonText );                           
    if ( jsonList != null && jsonList.Any() )                                                              
    {                                                                                                      
        var itemToDelete = jsonList                                                                        
            .FirstOrDefault( x => x.Id == id );                                                            
                                                                                                           
        if ( itemToDelete != null )                                                                        
        {                                                                                                  
            jsonList.Remove( itemToDelete );                                                               
        }                                                                                                  
    }                                                                                                      
                                                                                                           
    var output = JsonSerializer.Serialize( jsonList );                                                     
    await File.WriteAllTextAsync( path, output );                                                          
                                                                                                           
    var item = collection.FirstOrDefault( x => x.Id == id );                                               
    if ( item != null ) collection.Remove( item );                                                         
}                                                                                                          
                                                                                                           
static public async Task Delete( Guid id, string path, ViewModelCollection<SubscriptionData> collection )  
{                                                                                                          
    var jsonText = await File.ReadAllTextAsync( path );                                                    
    var jsonList = JsonConvert.DeserializeObject<List<SubscriptionData>>( jsonText );                      
    if ( jsonList != null && jsonList.Any() )                                                              
    {                                                                                                      
        var itemToDelete = jsonList                                                                        
            .FirstOrDefault( x => x.Id == id );                                                            
                                                                                                           
        if ( itemToDelete != null )                                                                        
        {                                                                                                  
            jsonList.Remove( itemToDelete );                                                               
        }                                                                                                  
    }                                                                                                      
                                                                                                           
    var output = JsonSerializer.Serialize( jsonList );                                                     
    await File.WriteAllTextAsync( path, output );                                                          
                                                                                                           
    var item = collection.FirstOrDefault( x => x.Id == id );                                               
    if ( item != null ) collection.Remove( item );                                                         
}                       

The problem, I think, is immediately visible - ProfileData simply changes to SubscriptionData.我认为问题是立即可见的——ProfileData 只是更改为 SubscriptionData。 Question: how to avoid this?问题:如何避免这种情况?

I wanna have this:我想要这个:

static public async Task Delete<T>(...){... var itemToDelete = jsonList.FirstOrDefault(x => x.Id == id ...)          

The problem is x.Id of unknown class问题是 x.Id 未知 class

The problem is x.Id of unknown class问题是 x.Id 未知 class

Declare an interface IHasId which defines your Id property, and make sure that both ProfileData and SubscriptionData implement that interface.声明一个定义您的Id属性的接口IHasId ,并确保ProfileDataSubscriptionData都实现该接口。

You can then constrain your generic method declaration as follows:然后,您可以按如下方式约束您的泛型方法声明:

static public async Task Delete<T>(...) where T : IHasId
{ ... }

(Obviously, if you already have a base class for ProfileData and SubscriptionData which defines the Id property, you can just constrain your generic parameter to that base class.) (显然,如果您已经为定义Id属性的ProfileDataSubscriptionData设置了基数 class,则可以将通用参数限制为该基数 class。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM