Claudio's profileClaudio Lassala in Softw...PhotosBlogListsMore ![]() | Help |
|
May 27 Checking out the Source Analysis for C#I've just spent about 30 minutes checking out the recently announced Microsoft Source Analysis for C#. I definitely love the idea, but I'm not sure I'll be using it just yet. Here are some of my impressions (again, I haven't spent a lot of time on it yet)... All using directives must be placed inside of the namespaceHmm, but VS itself violates that rule on every class we create...! The property must not be placed on a single line. The opening and closing curly brackets must each be placed on their own lineHuh? But that an auto-implemented property. What would we want to split that into multiple lines?! Variable names must not start with 'm_'Hmpf, that's the standard with have here at the company... A closing curly bracket must not be preceded by a blank lineOk, this one I like. Tabs are not allowed. Use spaces insteadHuh? Thanks, but no, thanks. I've seen a lot of people complaining online about this one too. What's the problem with using tabs?? The call ... must begin with 'this.'...Ah, I like that one. This is something we've made as part of our guideline here, but couldn't use FxCop to trap for that, since the 'this.' doesn't make into the IL. Now, the problem here is that, again, the code that VS generates violates this rule all over the place, such as on the example below: I'll continue to use this tool to see if it's going to stick with me. I've tried it on a very small project, and got 246 violations; the vast majority where produced by code generated by VS. On a big project, I'd probably end up with a LOT more violations, so I'm not sure I can live with that. I believe there is a way to disable some of the rules, but haven't looked for it just yet... Anyway, I do appreciate Microsoft releasing tools like this one. Hopefully, more and more developers will start paying more attention to how they write code. Interesting training materialAs an MVP, I got offered a license to InnerWorkings training material. I've been checking out some of it, and it looks pretty interesting. I noticed they had some material on Domain-Driven Design Patterns, Enterprise Patterns, and some related topics that I'm always interested on, so I decided to try it out. There is a "Developer Interface" app, where we keep all the material purchased. Each course has a number of tasks, and each task presents the scenario to be solved, the challenges, and reference material (with links to chapter of great books such as Eric Evan's on Domain-Driven Design). The task provides a way to launch the sample project in Visual Studio. Once in VS, we get an "InnerWorkings" Tool Window that gives us quick access to the problems we need to address in the solution, and the useful links. You can either try to address the problems by yourself, or cheat and see the solution. Once you're done with it, you may click the 'Judge Project Code" button that gets added to VS to have your code analyzed to see if you've implemented things in the way that was expected. I'll certainly be checking out some of the other courses they have available (I need to catch up with some stuff such as WCF, WPF, Silverlight, LINQ to XML, etc.). May 16 Getting rid of the slow help engine in Visual StudioIt's been a few years already since the last time I've installed VS' help (MSDN) on my machines. The thing is huge, slow to come up, and it didn't use to be that good. I just got used to using a SlickRun magicword to quickly search google, which searches not only MSDN, but also other resources (often much better than MSDN) such as CodeProject, blogs, etc. Since I'm connected almost 100% of the time, why use the slow local MSDN library? Also, every once in a while a reach for the F2 key in order to start a "rename" refactoring in Visual Studio, and and up pressing F1 by mistake, and then there go 2 minutes of my life, waiting for the help window to come up (even though, again, I don't even have MSDN installed). Talking to Mike today, I remembered I used to have a little VS macro that'd search on Google the text I have selected in VS. I thought it'd be good to just re-map the F1 key to that macro. It solves to problems: allows me to quickly perform a Google search from VS, and it doesn't get me stuck waiting for the VS help window to come up if I've hit the F1 key by mistake. :) You can find an example of such macro here. In order to change the keybinding, go to Tools-Options-Keyboard, search for the "GoogleSearch" macro, and bind the F1 key to it. Matching my own record: 5 and a half years at EPS!Wow, today, May 16 2008, I'm matching my personal record at working with a company: 5 and a half years, or to be more precise, 2003 days. :) Here's a little history:
The reader have probably noticed the pattern: after the company where I've worked for over 5 years, my stay at every job was getting shorter and shorter. Eventually I've decided to go solo as an independent consultant. I had my clients, I've put together some conferences for software developers, etc. That lasted for about 8 months. I wanted to have no strings attached to any company because at that time there was a good chance EPS would be hiring me, so I wanted to make sure I could join the company at any time. And that happened in November of 2002. The rest, as they say, is history. :) May 15 Material from Tulsa School of Dev 2008 is onlineI've just uploaded the material I've presented last week at the Tulsa School of Dev 2008. The material includes slide decks and source code for my three sessions (C# 3.0, Design Patterns, and LINQ). The material can be downloaded here. Attributes on constants? Haven't thought about that...I'm currently reading this article on CodeProject where the author is creating an MVP framework (seems like a good article so far...). In there, the author applies attributes to constants of a type. I had never thought that was actually possible. I want to make sure I keep that at the back of my mind, because I think I'll be using it sometime soon. As a quick reminder, this is how that can be implemented: public class SomeClass { [Special("Some special attribute")] public const string SomeConst = "This is some constant"; } public class SpecialAttribute : Attribute { public string Foo { get; set; } public SpecialAttribute(string foo) { Foo = foo; } } This blog post has a handy method to get a list of constants defined on a type (including its baseclasses): /// <SUMMARY> /// This method will return all the constants from a particular /// type including the constants from all the base types /// </SUMMARY> /// <PARAM NAME="TYPE">type to get the constants for</PARAM> /// <RETURNS>array of FieldInfos for all the constants</RETURNS> private FieldInfo[] GetConstants(System.Type type) { ArrayList constants = new ArrayList(); FieldInfo[] fieldInfos = type.GetFields( // Gets all public and static fields BindingFlags.Public | BindingFlags.Static | // This tells it to get the fields from all base types as well BindingFlags.FlattenHierarchy); // Go through the list and only pick out the constants foreach (FieldInfo fi in fieldInfos) // IsLiteral determines if its value is written at // compile time and not changeable // IsInitOnly determine if the field can be set // in the body of the constructor // for C# a field which is readonly keyword would have both true // but a const field would have only IsLiteral equal to true if (fi.IsLiteral && !fi.IsInitOnly) constants.Add(fi); // Return an array of FieldInfos return (FieldInfo[])constants.ToArray(typeof(FieldInfo)); } And here's a little test method to show how to get the attributes out of the constants of a type: [TestMethod] public void TestMethod1() { SomeClass obj = new SomeClass(); FieldInfo[] fields = GetConstants(obj.GetType()); Assert.AreEqual(1, fields.Length); Assert.AreEqual("SomeConst", fields[0].Name); object[] attribs = fields[0].GetCustomAttributes(false); Assert.AreEqual(1, attribs.Length); SpecialAttribute special = attribs[0] as SpecialAttribute; Assert.IsNotNull(special); Assert.AreEqual("Some special attribute", special.Foo); } May 14 Vista gets on my nerves...This is just stupid: I have a folder somewhere that I've created. It's not under any security sensitive folder such as Program Files or Windows. I've copied some files into that folder. Then, I tried deleting the files. Vista asks me typical security questions, and I say "yes, just delete the damn files". Eventually it just comes back to me and tells me "access is denied". Then I try moving the files into a subfolder. It has no problems with that. Tried deleting the subfolder; no luck. Moved the files back to the original location. Next, I try opening a Command Prompt, running it as admin. I tried deleting the files there, no success either. Finally, I tried running Windows Explorer as admin, and voila, the stupid thing lets me delete the files that I HAVE CREATED!!! Oh joy. How hard should it be for me to delete a stupid file that I've created myself, sitting on a folder that I've also created, outside of any O.S. folder??? May 13 Lambda Expression and Object MappersHere's a simple implementation of an object mapper using lambda expressions that I've shown yesterday at the Tulsa School of Dev. Say we need to copy some data from one object to another. For this example my objects are a Book and an AlternateBook. The class are shown below: public class Book { public Book() { } public Book(string title, int yearPublished) { Title = title; YearPublished = yearPublished; } public string Title { get; set; } public int YearPublished { get; set; } public string AuthorFirstName { get; set; } public string AuthorLastName { get; set; } } public class AlternateBook { public string AuthorFullName { get; set; } public string Title { get; set; } } I could have a mapper that worked like this: Mapper mapper = new Mapper(book, alternateBook); mapper.AddMap("Title", "Title"); mapper.AddMap("AuthorFirstName", "AuthorFullName"); That approach would have some problems, though:
The approach using Lambda expression would look like this:
Points to note:
Now, in case you're not familiar with lambdas yet: the expression does NOT get executed when we are passing it as a parameter to the AddMapping method. At that point we are only registering the m apping; the actual execution occurs whenever we want it to. I would probably, say, at the application start, register all mappings I'm likely to use. Eventually, when I need to actually process the mapping (copying some data from one object to another), I can just call the ApplyMapping, passing in the objects involved with the mapping. For example:
The code above should produce the following results: So, how's the Mapper class implemented? Here we go: public class Mapper<TSource, TTarget> { private Collection<Action<TSource, TTarget>> m_Mappings = new Collection<Action<TSource, TTarget>>(); public Collection<Action<TSource, TTarget>> Mappings { get { return m_Mappings; } } public void AddMapping(Action<TSource, TTarget> action) { Mappings.Add(action); } public void ApplyMappingsTo(TSource source, TTarget target) { foreach (var mapping in Mappings) { mapping(source, target); } } } The class keeps a list of all the mappings within its Mappings collection, which is a collection of Action<TSource, TTarget> delegates. Both TSource and TTarget are Generic types provided to the class. The AddMaping method takes in the delegate (which we pass in as a lambda) and adds it to the Mappings collection. The ApplyMappingsTo method takes in instances for the source and target object, iterates through the Mappings collection, and invokes each mapping (don't forget it, the mapping is a delegate). That's it. Again, this is just a simple implementation. I'm pretty sure there's a lot of improvements to be done on top of this, and there's definitely something some people wrote out there around the same lines; I'll be looking for that myself in order to see how I can improve this. For instance, one thing that pops up is: what if the developer messed up and specified the mapping going from the target to the source instead? Like so:
We certainly don't want to copy the title from the Alternate Book to the Book object. I don't know of way yet to have the compiler catch that problem (any ideas?). During runtime, though, I know I could have the AddMapping method analyze the Expression Tree for the lambda it was given, and make sure that the target appears at the left side of the = sign, and the target appears at the right side. Another improvement that could be made is some optimization. For instance, itake the following code snippet:
There, the developer has used string concatenation to build up the value to be assigned to the property. The AddMapping method could have some logic that analyzes the expression tree, and if it finds something like string concatenation, it could replace that with using a StringBuilder. Of course, such optimization should only be done if we can clearly see that as being something that we really needed to squeeze better performance out of. Also, it'd be convenient to have the mapper just figure out what properties to copy from the source to the target by looking for properties that map 1:1 (that is, property name and type exist on both objects), and then let the developer only supply special mappings for properties that require transformation. I'll be doing some more research on better ways to do this kind of stuff. May 12 The Temple of the KingAfter some pretty tough weeks with a lot of work and little sleep, I spent an afternoon with my friends Milton and Lucas having some fun making some home-recording of Rainbow's Temple of the King. Nothing like having some fun to relieve some of the stress. :) Check it out: May 10 The Twitter thing is not so bad after allA few weeks ago I mentioned I had broken down and signed up for Twitter. Even though I have not yet fell for it yet, I do some of its value. It does have an obvious problem: if you're not careful, you can quickly get carried away and become and totally unproductive person. What I've been doing is to have a Twitter client open in one of my computers, and every once in a while I check out what my peers are up to (usually on short breaks or when my computer is taking to long to process something). Here are the things I've either experienced or seen that I kind of dig:
One other thing I'm not liking about it right now is the fact that I don't own an *i*Phone, I just own *a* phone; a regular Sony phone that has a tiny little web browser, which makes it *really* hard to type anything on it and post to Twitter, or to follow posts. And an iPhone isn't getting in my budget plans for the foreseen feature (there's just too many other things taking higher priority). Note: I was typing this post offline; when I got online, I saw a tweet from somebody pointing out to a post by Rick that's very similar to this one of mine. :) May 05 Tulsa's School of DevThis Saturday I'll be speaking at the Tulsa's School of Dev. I've had a great time last year, so I'm looking forward to going there again, do some presentations, attend others, and meet some old buddies there. Here are the talks I'll be delivering:
The full agenda can be found here. That's a free event, so make sure to check it out! |
|
|