ninja star Getting Assembly Information Using Reflection

by Michael Ceranski, posted on January 03 2012
I recently took over a WPF application that needed a little bit of refactoring. One of the places that had a lot of repeated code was the about screen. By using reflection a bunch of properties were being created to display things like the assembly title, version, description and etcetera. The original code looked like this: public string AssemblyDescription { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyDescriptionAttribute)attributes[0]).Description; } } public string AssemblyProduct { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute... [More]
Tags: , ,

ninja star Dapper.Net - A Micro ORM that puts you back in control

by Michael Ceranski, posted on May 12 2011
Its fun to reminisce about how database access has evolved over the years. In the early days I used to write parameterized SQL statements directly in my code. Eventually that evolved into using stored procedures. Mainly because by using a stored procedure I could change the way data was gathered without recompiling my source code. Eventually, I abandoned stored procedures altogether when LINQ to SQL was released. Finally, if we fast forward to the current day, my preferred method of accessing data is with EF code first. EF Code first is great because I can spend even less time managing my database and more time solving business problems. With the level of abstraction getting higher and higher it makes you wonder if perhaps the next generation of developers will not even write T-SQL anymore. They will probably laugh at us old timers when we talk about stored procs and T-SQL.  Wait! Back up a minute! Just because we have abstracted ourselves from the database doesn’t mean t... [More]
Tags:

ninja star Automatic Resource File Translation via Google Translate

by Michael Ceranski, posted on September 15 2010
One of the features that I support in WeBlog is localization. That means that all the labels, buttons and text within the application are stored in a resource file. So if you switch from English to Spanish, the text will be automatically displayed in the right language. For more information about how this magic happens, read this article. Since I am only fluent in English I depend on Google translator to make the translations for me. After about 10 minutes of manually translating a spanish resource file, I decided that I needed a way to automate the process. I did a quick Google search and discovered that the translator service utilizes a JSON based API. A little more searching and I found a class library written by Alex Meyer-Gleaves which takes care of making the service calls to the translator API and parsing the results. So the only work I had to do was read the resource file, invoke the translator via Alex’s C# library and save the translated data to a new resource file. Turns o... [More]
Tags:

ninja star Unit Testing Secure Controller Actions with Moq

by Michael Ceranski, posted on August 15 2010
One of the hardest things to unit test in MVC is security. Security is tough to test because there is a lot of setup involved in mocking the HttpContext, the Principal and the Identity. For example, in WeBlog I am using the following code in the Edit Post action. Post post = Repository.FirstOrDefault<Post>(x => x.ID == id); if (post == null) return View("NotFound"); if (!HttpContext.User.CanEditPost(post)) return View("PermissionDenied"); In order to make sure this code works properly I need to test it with an authorized and unauthorized user. Unfortunately, the HttpContext.User will not automatically be created for your tests so you have to mock one for each test that your perform. So lets start this journey by reviewing the code required to mock the HttpContext using the popular opensource library Moq. This code is a combination of code I discovered on Stackoverflow and Scott Hanselman’s MvcMockHelpers: MockContext public static Mock<Htt... [More]

ninja star Custom Configuration Sections FTW

by Michael Ceranski, posted on August 05 2010
As a .NET developer you will probably create hundred of configuration files over the course of your career. Most of the time when we use a configuration file it is for simple things like a database connection string. However, sometimes configuration data needs to be relational and can require a more complex structure than traditional name value pairs. For example, lets say that I am developing a data import tool which uses the configuration file to store data mapping information. Here is an example of what my configuration file might look  like: <import> <jobs> <job name="Foo"> <fieldMappings> <mapping source="column1" destination="TimeStamp"/> <mapping source="column2" destination="Name"/> </fieldMappings> </job> <job name="Bar"> <fieldMappings> <mapping source="column1" destination="DateTime"/> ... [More]
Tags: ,

About the author

MikeMichael Ceranski is a developer specializing in the .NET stack. I have spent time as a DBA, Web Developer and even a network engineer. Up til now most of my career has revolved around the .NET stack but I have recently taken an interest in microcontrollers which has forced me to get acquainted with lower level languages such as C, and C++.

View my resume

Sponsors