10 Tips to Boost Your Productivity with C# and Visual Studio 2008

10 Tips to Boost Your Productivity with C# and Visual Studio 2008

精选 转载

gkvalley 博主文章分类:.NET

文章标签 .NET 职场 WEB 休闲 文章分类 .Net 后端开发

Learn Key Bindings

It's an obvious and trivial thing, but the timesaving will add up, especially for those actions you perform tens or hundreds of times a day such as building and debugging.  Here are some basic bindings every Visual Studio developer should know:

  • Build: CTRL + SHIFT + B
  • Word completion: CTRL + SPACE
  • Start with debugging: F5
  • Start without debugging: CTRL + F5

Even expert Visual Studio developers can benefit by learning new bindings. Download the Visual C# 2008 Keybinding Reference Poster and hang it in your work area.

 

Generate XML Comments with GhostDoc

Instead of typing XML comments by hand, let a tool do the work for you. Although macros and snippets are reasonably effective for this, I would recommend Ghost Doc over any other solution. This free add-in uses customizable templates to generate consistent, English-readable documentation based on the current context.  To use it, right-click (or use CTRL + SHIFT + D) to document the current element.  For example:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_休闲

 

This generates the following documentation (note GhostDoc split the property name into words and created a sentence from it):

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_职场_02

 

 

Auto-Implement Properties

Take advantage of a new feature of C#: auto-implemented properties. Rather than creating a private backing field for your properties, let the compiler do it for you. The following demonstrates the syntax:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_WEB_03

Use the code snippet to make this even faster.  Type prop (the shortcut for an auto-implemented property) followed by TAB TAB.  Then fill in the data type and property name:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_WEB_04 

 

Refactor

The refactor feature in Visual Studio is indispensable for many tasks, especially renaming, but one productivity feature I particularly like is Encapsulate Field. If you are unable to use an auto-implemented property, declare a private field and let Visual Studio generate the Property for you.  To use this feature, right-click on the field and select Refactor > Encapsulate Field...

 

 10 Tips to Boost Your Productivity with C# and Visual Studio 2008_休闲_05

The property is created for you:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_WEB_06

 

 

 

Add Commands to Visual Studio 2008

Install the PowerCommands for Visual Studio 2008 to add several productivity commands such as:

  • Close all documents
  • Copy and paste a class (automatically renames)
  • Remove and sort using statements project-wide
  • Copy and paste references (including a project reference)

Install the Team Foundation Server Power Tools to add several TFS productivity commands such as:

  • Find in source control
  • Open source folder in Windows Explorer
  • Work item templates (can be used to set values on multiple work items at once)

Add your own productivity commands.  For example, to add Reflector so it automatically opens on the current project.

  • Select Tools > External Tools
  • Click Add
  • Name it Reflector and browse to the executable
  • Enter $(TargetPath) for the Arguments

 

Speed up Compilation with Project Configuration

You may build tens of times during a programming session, so don't enable anything that isn't absolute necessary such as code analysis and XML documentation.  Develop in Debug configuration, and switch to Release configuration just before check-in to run code analysis and generate XML documentation. In a large solution I recently worked on, this shaved a minute off compilation time.

The following shows code analysis disabled in Debug configuration:

 

 

 10 Tips to Boost Your Productivity with C# and Visual Studio 2008_职场_07

The following shows code analysis enabled in Release configuration:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_WEB_08 

 

Let Visual Studio Generate Unit Test Code

Although it can't fully automate unit testing yet (check out Pex), Visual Studio does a good job of generating positive unit test code to give you a jump start.  To use this feature, right-click on an element you would like to test and select Create Unit Tests... 

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_.NET_09

Visual Studio generates the following test method:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_职场_10

 

Use Interface-Driven-Design

You probably never thought of an interface as a productivity feature, but it can be if your development process is driven by contracts instead of implementation.  Let me illustrate with a simplified example.  One developer owns the business layer and another developer owns the data access layer, and they need to agree on how to communicate to implement a new feature.  In some business object designs, business components will instantiate data components (or call a static method).  This is a problem from a design standpoint because the two become tightly coupled.  It is also a problem from the productivity standpoint because the business layer developer becomes dependent on the data access layer developer's implementation.  Interface-driven design (IDD) solves this issue.

Rather than the business component developer waiting on the data component developer, they meet to design and implement the interface.  Both developers are then free to go their separate ways and implement the components in parallel.  IDD also enables the business developer to mock the data access component, thereby removing any scheduling dependencies.  The following illustrates the design:     

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_.NET_11

Make a Mockery of Dependencies

Different developers may own layers or features that your component is dependent on, but don't let that slow you down.  Suppose you are responsible for the business layer which depends on the data access layer which in turn depends on database tables and stored procedures. Rather than waiting for dependent layers to be completed, mock the data access layer so you can implement and unit test the business layer. By the way, you should be using mocks anyway; otherwise your unit tests are more than likely integration tests.  I recommend Rhino Mocks.

Here is a sample unit test with mocks:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_.NET_12

Data Drive Unit Tests

If you have a unit test that multiple inputs to fully test, you could write a test method for every possible combination, but data-driven unit tests are more efficient.  When the unit test is run, it loads data from a table and calls the unit test for each row.  You can access the data in the current row using the TestContext.DataRow property.

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_.NET_13

Once the test completes, you can view the results:

10 Tips to Boost Your Productivity with C# and Visual Studio 2008_休闲_14

  • 收藏
  • 评论
  • 举报

上一篇:牛语

下一篇:vs2010 快捷键大全

提问和评论都可以,用心的回复会被更多人看到 评论
发布评论
相关文章

举报文章

请选择举报类型

内容侵权 涉嫌营销 内容抄袭 违法信息 其他

具体原因

包含不真实信息 涉及个人隐私

补充说明

0/200

上传截图

格式支持JPEG/PNG/JPG,图片不超过1.9M

已经收到您得举报信息,我们会尽快审核