{"id":217,"date":"2015-05-23T13:08:48","date_gmt":"2015-05-23T13:08:48","guid":{"rendered":"http:\/\/shivaprogramming.com\/blog\/?p=217"},"modified":"2016-06-04T04:03:50","modified_gmt":"2016-06-04T04:03:50","slug":"refactoring-repositroy","status":"publish","type":"post","link":"http:\/\/shivaprogramming.com\/blog\/refactoring-repositroy\/","title":{"rendered":"Refactoring Repository"},"content":{"rendered":"<pre class=\"lang:default decode:true   \"> First Repo\r\nIpersonRepository repository = new ServiceRepository();\r\nvar people =repository.GetPeople();\r\nforeach(var person in people)\r\n{\r\n    personListBox.Items.Add(person);\r\n}\r\n------------------------------------------\r\nSecond Repo\r\n\r\nIpersonRepository repository = new CSVRepository();\r\nvar people =repository.GetPeople();\r\nforeach(var person in people)\r\n{\r\n    personListBox.Items.Add(person);\r\n}\r\n\r\n-------------------------------------\r\nThirdRepo\r\n\r\nIpersonRepository repository = new SQLRepositroy();\r\nvar people =repository.GetPeople();\r\nforeach(var person in people)\r\n{\r\n    personListBox.Items.Add(person);\r\n}\r\n-------------------------------------------------\r\nAdd new class Repository Factory class\r\n----------------------------------------------\r\npublic static class RepositoryFactory\r\n{\r\n   public static IPersonRepository GetRepository(string repositoryType)\r\n   {\r\n       IPersonRepository repo =null;\r\n       switch(repositoryType) \r\n       {\r\n          case RepoType.Service:\r\n             return new ServiceRepository();\r\n            \r\n           case RepoType.CSV:\r\n            return new CSVRepository();\r\n         \r\n          case RepoType.SQL:\r\n           return new SQLRepository();\r\n          \r\n         default:\r\n            throw new ArgumentException(\"Invalid Repository Type\");\r\n          \r\n       }\r\n       return repo;\r\n   }\r\n}\r\n---------------------\r\n\r\n\r\npublic enum RepoType\r\n{\r\n    Service,\r\n    CSV,\r\n    SQL\r\n}\r\nTake Method and Extract to method FetchData Method that takes string argument\r\n\r\nprivate void FetchData(string repositoyType)\r\n{\r\n    IpersonRepositroy repositroy = RepositoryFactory.GetRepository(repositroyTpye);\r\n    var people =repository.GetPeople();\r\n    foreach(var person in people)\r\n   {\r\n     personListBox.Items.Add(person);\r\n   }\r\n}\r\n\r\n----------------------------------\r\nNow going back to the each method and replace the code \r\n\r\nFetchData( RepoType.Service);\r\nFetchData( RepoType.CSV);\r\nFetchData( RepoType.SQL);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First Repo IpersonRepository repository = new ServiceRepository(); var people =repository.GetPeople(); foreach(var person in people) { personListBox.Items.Add(person); } &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Second Repo IpersonRepository repository = new CSVRepository(); var&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-design-pattern"],"_links":{"self":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":6,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":229,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/posts\/217\/revisions\/229"}],"wp:attachment":[{"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shivaprogramming.com\/blog\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}