|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Table of Contents
IntroductionFXCOP is one of the legendary tools which help us automate reviews using set of rules against compiled assemblies. This article will discuss some basics of FXCOP and then concentrate mainly on how we can add custom rules in FXCOP. I have been writing and recording a lot of architecture related videos on design patterns, UML, FPA estimation, Enterprise application blocks, C# projects, etc. You can watch my videos here. Basics of FXCOPAs the name suggests, the COP in FXCOP means the police. So it’s an analysis tool which runs rules against the .NET assemblies and gives a complete report about broken rules. As it runs on the assembly, it can be run on any language like C#, VB.NET etc. You can download the latest copy of FXCOP from here. The below figure gives a visual outlook of how FXCOP runs the rules on the assembly and then displays the broken rules in a different pane. Using the Existing RulesUsing the existing rules is pretty simple. Open the FXCOP tool and you will see two tabs, one is the targets tab and the other the rules tab. Targets tab is where you add the assembly. You can add the assembly DLL by right clicking and then clicking on add targets. The rules which need to be run can be selected in the second tab ‘Rules’. So click on the tab and select the necessary rules and hit analyze. Adding a Custom Rule - All Connection Objects Should be ClosedTo understand the concept of custom rule, we will take a practical example. What we will do is when any connection object is opened in a method, we will ensure that it’s closed. If it’s not closed, FXCOP will throw an error. Step 1To make custom rules, the first step is to create a class library. FXCOP expects an XML file in which rules are defined. The format of the XML file is shown below. We have named this file as ‘Connection.XML’. The Rule tag has the ‘ <?xml version="1.0" encoding="utf-8" ?>
<Rules>
<Rule TypeName="clsCheck" Category="Database" CheckId="Shiv001">
<Name>Connection object Should be closed</Name>
<Description> Connection objects should be closed</Description>
<Owner> Shivprasad Koirala</Owner>
<Url>http://www.questpond.com</Url>
<Resolution> Call the connection close method </Resolution>
<Email></Email>
<MessageLevel Certainty="99"> Warning</MessageLevel>
<FixCategories> Breaking </FixCategories>
</Rule>
</Rules>
Step 2The first thing is to reference and import the FXCOP SDK. So add a reference to the FxCopSdk.DLL. You can find FxCopSdk.dll where FxCop is installed. Once you have added a reference, you need to import the DLL in the class file. using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.FxCop.Sdk;
Step 3For defining custom rule, you need to inherit from the ‘ public class clsCheck : BaseIntrospectionRule
{
public clsCheck(): base("clsCheck", "MyRules.Connection", typeof(clsCheck).Assembly)
{
}
public override ProblemCollection Check(Member member)
{
…….
……..
}
}
Step 4So the first thing is we convert the member into the Method method = member as Method;
bool boolFoundConnectionOpened = false;
bool boolFoundConnectionClosed = false;
Instruction objInstr=null;
Step 5Every method has an instruction collection which is nothing but the actual code. So we loop through all instructions and check if we find the for (int i = 0; i < method.Instructions.Count; i++)
{objInstr = method.Instructions[i];
if (objInstr.Value != null)
{
if (objInstr.Value.ToString().Contains("System.Data.SqlClient.SqlConnection"))
{
boolFoundConnectionOpened = true;
}
if (boolFoundConnectionOpened)
{
if(objInstr.Value.ToString().Contains("System.Data.Common.DbConnection.Close"))
{
boolFoundConnectionClosed = true;
}
Step 6Now the last thing. We check if the connection is opened, was it closed? If not, then we get the message of the connection.xml file and add it to the problems collection. This problems collection is returned to the FXCOP to display it. if((boolFoundConnectionOpened)&&(boolFoundConnectionClosed ==false))
{
Resolution resolu = GetResolution(new string[] { method.ToString() });
Problems.Add(new Problem(resolu));
}
return Problems;
Step 7Once you have compiled the DLL, add the DLL as rules and run the analyze project button. Below is the project which was analyzed and the ‘ public void addInvoiceDetails(int intInvoiceid_fk,
int intCustomerId,
int intProductid_fk,
double dblTotalAmount,
double dblTotalAmountPaid)
{
SqlConnection objConnection = new SqlConnection();
}
You can see in the output how the method name is displayed with the resolution read from the connection.xml file.
|
||||||||||||||||||||||