How can I design this better? (Avoiding a switch statement with Object Oriented design)

前端 未结 6 841
不思量自难忘°
不思量自难忘° 2020-12-30 11:23

I know a little bit about Object Oriented design, but I\'m not sure how to go about using those principles in my code. Here\'s what I\'m working on:

    pub         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 11:43

    To write less code but improve readability the the level of declaritive code you can swith to a dictionary with delegates for each database. That can easily be extended and is very readable. With that more functional approach in mind you get something like

    void Query(Agency agency, Citation queryCitation)
    {
        Dictionary> QueryMap = new Dictionary>
        {
            { "SQL", QueryOracle},
            { "PIC", QueryPic}
        };
    
    
        queryCitation.AgencyCode = agency.AgencyCode;
    
        QueryMap[agency.ClientDb.Type](agency, queryCitation);
    }
    

提交回复
热议问题