CompulsoryCat 1.4 released
Uploaded 1.4 of CompulsoryCat, a small helper lib for dotnet(4).
New stuff is a COM wrapper for creating shortcuts.
Uploaded 1.4 of CompulsoryCat, a small helper lib for dotnet(4).
New stuff is a COM wrapper for creating shortcuts.
Updated CompulsoryCat to version 1.2
The first big news is the Assemblyname functionality for retrieving a tree of assembly references for an application.
The other big news is that I genereated help files and uploaded too.
I am trying out Visual studio unit testing framework, the Microsoft alternative to, for instance, nUnit which was my old preferred testing framework (and possibly still is).
At the same time I am not in a project devoted to Test driven development so I can’t use its religion to write tests first and refuse to show any UI progress to the customer. Right or wrong – that is how I perceive the reality around me right now. But I do want to have tests and waiting for them I make sure I have place holders.
My compromise is to set the tests to Inconclusive (Ignore in nUnit talk). I guess it will come back to bite me…
What I also do is to make sure I have a test to go with every method.
[TestMethod] public void AddOrUpdateTest() { Assert.Inconclusive(); Elvis.Common.Meta.GetMethodName<BLF.Sample>(x => x.AddOrUpdate(null, null)); }
I used the code from
http://stackoverflow.com/questions/1524290/linq-is-it-possible-to-get-a-method-name-without-a-return-type-via-linq-expressio
but I could probably use CompulsoryCat just as well.
I belive that even adhering TDD this technique is good. There are so many times I don’t know what the classes and methods will look like before having done a simple implementation. To write exhaustive tests for a method that will be deleted is a waste. Better then to make sure the simple code runs and not forget to write the tests by having a yellow Inconclusive flag.
Just released version 1.1 of CompulsoryCat on google code.
It is a helper lib for getting the names of methods, classes, properties and other stuff out of the methods, classes etc. It uses lambda or reflection to be as safe as possible.
It is unit tested and has a small exe to show a few examples of its use. There is also a help file.
Way too many times have I written
void MyFunction( int aNumber){ MyHomemadeLogClass.Logg( "MyFunction", "aNumber=" + aNumber.ToString()); ...}
to get logging functionality.
It would be so nice to just write
void MyFunction( int aNumber){ MyHomemadeLogClass.Logg(); ...}
It is no hard to do. Use reflection to get the call stack to get to the method and the parameters. Convert all this to a readable string.
Unfortunately it is not that easy to log the contents of the parameters, one has to get into some sort of debugger area to do that.
class ReflectionUtility { private static System.Reflection.MethodBase GetCallingMethod() { return new System.Diagnostics.StackTrace().GetFrame(2).GetMethod(); } public static string GetCallingMethodFullNameReturnParametertypes() { return MethodFullNameReturnParametertypes(GetCallingMethod()); } private static string MethodFullNameReturnParametertypes(System.Reflection.MethodBase method) { return string.Format("{0} {1}.{2} ({3})", ((System.Reflection.MethodInfo)method).ReturnType, // System.Void, System.Int32 etc. method.DeclaringType.FullName, // MyNamespace.MyClass. method.Name, // MyMethod. string.Join(",", method.GetParameters().Select(p => p.ParameterType.ToString() + " " + p.Name).ToArray()) // () or (int) or (int,string) etc. ); } }
To use this in a Logging class one must implement a GetCallingCallingMethod or even worse, but it would make this example hard to read.
One can use the Conditional attribute to avoid the call when not debugging.
I also put the code on pastebin to make the copy-paste easier. (I have yet to find a good way to present code.)
Update: go to http://compulsorycat.googlecode.com for the source code in a LGPLd project.
When you want to populate a dropdown listbox with customers
class Customer{ int Id; string Name; ...}
and have to write
myDropdownListbox.DisplayMember = "Name";myDropdownListbox.DataMember = "Id";
or
myGridColumnCustomerName.DatatMeber = "Name";
you really get a sour taste in your mouth.
Having magic strings like above spread in your code is really bad since it always compiles but might fail runtime. This means more manual tests. Which in turns means a certain friction against renaming properties while refactoring.
I have been in way too many projects where one table has a field ItemNumber when it should have been ItemCount and where some fields are called Nr while another Number and these names are spread up in the layers to reach the presentation layer where they surface as magic strings like the examples above.
Luckily there is a solution in Dotnet 3 with LINQ. It isn’t the prettiest but it fails compile time when it should and that is considered a good thing.
( I won’t bother with explanation – just read the code. )
When you want to populate a dropdown listbox with customers
class Customer{ int Id; string Name; ...}
and have to write
myDropdownListbox.DisplayMember = "Name";myDropdownListbox.DataMember = "Id";
or
myGridColumnCustomerName.DatatMeber = "Name";
you really get a sour taste in your mouth.
Having magic strings like above spread in your code is really bad since it always compiles but might fail runtime. This means more manual tests. Which in turns means a certain friction against renaming properties while refactoring.
I have been in way too many projects where one table has a field ItemNumber when it should have been ItemCount and where some fields are called Nr while another Number and these names are spread up in the layers to reach the presentation layer where they surface as magic strings like the examples above.
Luckily there is a solution in Dotnet 3 with LINQ. It isn’t the prettiest but it fails compile time when it should and that is considered a good thing.
( I won’t bother with explanation – just read the code. )
// The Code.
<
class ReflectionUtility { public static string GetPropertyName<T, TReturn>(Expression<Func<T, TReturn>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } public static string GetMethodName<T, TReturn>(Expression<Func<T, TReturn>> expression) { var body = expression.Body as UnaryExpression; var operand = body.Operand as MethodCallExpression; var argument = operand.Arguments[2] as ConstantExpression; var methodInfo = argument.Value as System.Reflection.MethodInfo; return methodInfo.Name; } } class MyClass { public int MyField; public int MyPublicProperty { get; set; } public string MyReadonlyProperty { get { return string.Empty; } } public int MyMethod() { return 0; } private MyClass() { } // To make sure the class doesn't need a default constructor. } class Program { static void Main(string[] args) { string fieldName = ReflectionUtility.GetPropertyName((MyClass x) => x.MyField); Console.WriteLine(string.Format("MyClass.MyField:{0}", fieldName)); Debug.Assert("MyField" == fieldName); string propertyName = ReflectionUtility.GetPropertyName((MyClass x) => x.MyPublicProperty); Console.WriteLine(string.Format("MyClass.MyPublicProperty:{0}", propertyName)); Debug.Assert("MyPublicProperty" == propertyName); propertyName = ReflectionUtility.GetPropertyName((MyClass x) => x.MyReadonlyProperty); Console.WriteLine(string.Format("MyClass.MyReadonlyProperty :{0}", propertyName)); Debug.Assert("MyReadonlyProperty" == propertyName); string methodName = ReflectionUtility.GetMethodName<MyClass, Func<int>>((MyClass x) => x.MyMethod); Console.Write(string.Format("MyClass.MyMethod:{0}", methodName)); Debug.Assert("MyMethod" == methodName); Console.Write(Environment.NewLine + "Press any key."); Console.ReadKey(); } < }
Honor those who should. (This link contains a comment regarding generic methods that I’d like to impolement in CompulsoryCat.)
Update: When copy-pasting through manoli.net some angle brackets got dropped. This is, hopefully, fixed now. Otherwise – get the source here: http://selfelected.pastebin.com/f77563a02
Update:
Check out Compulsorycat. It is my F/OSS library with some of these functions.
Honor those who should. (This link contains a comment regarding generic methods that I’d like to impolement in CompulsoryCat.)
Update: When copy-pasting through manoli.net some angle brackets got dropped. This is, hopefully, fixed now. Otherwise – get the source here: http://selfelected.pastebin.com/f77563a02
Update:
Check out Compulsorycat. It is my F/OSS library with some of these functions.