tag:blogger.com,1999:blog-813074883684404285.post155193876399189261..comments2023-06-26T02:44:10.717-07:00Comments on Kirill Osenkov: Extension methods: one of my favorite C# 3.0 featuresKirill Osenkovhttp://www.blogger.com/profile/09976634721522023791noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-813074883684404285.post-76832542309231314782008-12-11T07:03:00.000-08:002008-12-11T07:03:00.000-08:00Nice article on Extension methods...If possible ca...Nice article on Extension methods...If possible can you post the source code link for the TreeviewAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-52296730528148388122008-02-04T10:57:00.000-08:002008-02-04T10:57:00.000-08:00That's great. Thanks!That's great. Thanks!Kirill Osenkovhttps://www.blogger.com/profile/09976634721522023791noreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-91813172640598999852008-02-04T00:17:00.000-08:002008-02-04T00:17:00.000-08:00The code I've posted is just a short demonstration...The code I've posted is just a short demonstration of my version adapted to your model. In production find/add logic was extracted into separate methodAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-36829019282882538712008-02-03T14:40:00.000-08:002008-02-03T14:40:00.000-08:00Great stuff desco!I like that you use IEnumerable ...Great stuff desco!<BR/><BR/>I like that you use IEnumerable to return the "path" in the tree, this is more flexible than having a fixed-depth tree of depth 3.<BR/><BR/>However I would still recommend extracting the logic into a separate FindOrAdd method. It doesn't necessarily have to be an extension method. This will promote reusing the code and allow you to be more explicit about your intention: for the current node, either find an existing node with a given header, or create a new one if it doesn't exist.Kirill Osenkovhttps://www.blogger.com/profile/09976634721522023791noreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-55577014714061975082008-02-03T13:41:00.000-08:002008-02-03T13:41:00.000-08:00Good sample of usage for such remarkable language ...Good sample of usage for such remarkable language feature as Extension methods.<BR/><BR/>In our application when the same problem appears we use a bit different variant of solution without extension method:<BR/><BR/> static IEnumerable<string> GetProperties(Car car)<BR/> {<BR/> yield return car.Make;<BR/> yield return car.Model;<BR/> yield return car.Year.ToString();<BR/> }<BR/><BR/> private void InitializeTree()<BR/> {<BR/> ItemsControl root = treeView1;<BR/><BR/> foreach (Car car in GetCars())<BR/> {<BR/> ItemsControl current = root;<BR/> foreach (string propertyName in GetProperties(car))<BR/> {<BR/> ItemsControl child =<BR/> current.Items.Cast<TreeViewItem>().FirstOrDefault(i => i.Header == propertyName);<BR/> if (child == null)<BR/> {<BR/> child = new TreeViewItem { Header = propertyName };<BR/> current.Items.Add(child);<BR/> }<BR/> current = child;<BR/> }<BR/> current.Items.Add(new TreeViewItem { Header = car.ToString() });<BR/> }<BR/> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-14994602181088235002008-01-21T14:23:00.000-08:002008-01-21T14:23:00.000-08:00Interesting! Thanks Andrew!Interesting! Thanks Andrew!Kirill Osenkovhttps://www.blogger.com/profile/09976634721522023791noreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-90228408409888542022008-01-21T08:13:00.000-08:002008-01-21T08:13:00.000-08:00Kirill,Just came across your blog. Good stuff. On ...Kirill,<BR/><BR/>Just came across your blog. Good stuff. On the extension methods, I have added the following to my library. Lambda everywhere!. Also have similar methods for IsNull, HasValue & IsEmpty.<BR/><BR/>-Andy<BR/><BR/>>><BR/><BR/>public static bool IsNullOrEmpty(this string value)<BR/>{<BR/> return string.IsNullOrEmpty(value);<BR/>}<BR/><BR/>public static bool IsNullOrEmpty(this string value, Action< string > action)<BR/>{<BR/> if (value.IsNullOrEmpty())<BR/> {<BR/> action(value);<BR/> return true;<BR/> }<BR/><BR/> return false;<BR/>}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-12618258389286302802007-12-13T15:47:00.000-08:002007-12-13T15:47:00.000-08:00Hi Dmitry,you're right, extension methods are not ...Hi Dmitry,<BR/><BR/>you're right, extension methods are not necessary to implement this. The usability of your method looks almost exactly like mine but doesn't use extension methods and doesn't have their drawbacks.<BR/><BR/>Good stuff!<BR/>KirillKirill Osenkovhttps://www.blogger.com/profile/09976634721522023791noreply@blogger.comtag:blogger.com,1999:blog-813074883684404285.post-85044394512901286452007-12-12T03:10:00.000-08:002007-12-12T03:10:00.000-08:00I don't think that extension method for treeView i...I don't think that extension method for treeView is obligatory to implement this classifier for car.<BR/>Following code (written in 'old' style) has some additional lines but hasn't problem with versioning like extension methods (sorry, for my java language, unfortunately has no C# nearly =)).<BR/><BR/>/<BR/>// Instead of extension method<BR/>public class TreeViewClassifier {<BR/><BR/> private final HashMap treeViewItemsToClassifierItems = new HashMap();<BR/> <BR/> private final TreeView treeView;<BR/> <BR/> /**<BR/> * @param treeView<BR/> */<BR/> public TreeViewClassifier(TreeView treeView) {<BR/> this.treeView = treeView;<BR/> }<BR/> <BR/> public Items FindOrAdd( String header ) {<BR/> return Wrap( this.treeView.Items).FindOrAdd(header);<BR/> }<BR/> <BR/> private Items Wrap( TreeViewItemCollection rawItems ) {<BR/> Items items = this.treeViewItemsToClassifierItems[ rawItems ];<BR/> if ( items == null ) {<BR/> items = new Items( rawItems );<BR/> }<BR/> return items;<BR/> }<BR/><BR/> public class Items {<BR/> <BR/> private final TreeViewItemCollection items;<BR/><BR/> /**<BR/> * @param items<BR/> */<BR/> public Items(TreeViewItemCollection items) {<BR/> super();<BR/> this.items = items;<BR/> }<BR/> <BR/> public Items FindOrAdd( String header ) {<BR/> TreeViewItem itemByHeader = this.items.ImplementationOfFindItemByHeader( header );<BR/> if ( itemByHeader == null ) {<BR/> itemByHeader = new TreeViewItem( header ); <BR/> }<BR/> return Wrap( itemByHeader.getItems() );<BR/> }<BR/> <BR/> public void Add( TreeViewItem item ) {<BR/> this.items.Add( item );<BR/> }<BR/> <BR/> }<BR/> <BR/> <BR/>}<BR/><BR/>// In action<BR/>TreeViewClassifier classifier = new TreeViewClassifier( treeView );<BR/> for( Car car : cars ) {<BR/> classifier.FindOrAdd( car.Make )<BR/> .FindOrAdd( car.Make )<BR/> .FindOrAdd( car.Year.toString() )<BR/> .add( new TreeViewItem( car.toString() ) );<BR/> }Anonymousnoreply@blogger.com