This site uses cookies. By continuing you accept the use of cookies.I Accept

As a developer it’s very common to “google” for code samples, when you’re set out to write code that you have either forgotten or if you’re not quite sure how something works. I always use Google for this, because I believe they have the best results. And frankly because I haven’t given Bing a fair chance.

Now, Microsoft has released a new extension for Visual Studio 2013; Bing Code Search for C#. The extension does exactly what its name implies. But, it does it in a fashion that is incredibly clever. Your search result is displayed inside Visual Studio and it only shows the working Code Sample, as opposed to Google, where you have to pick out several results. Or when searching StackOverflow.com where it’s very likely that 10 people answer the same question with variations of the same answer.

Here’s how it works.

Install the extension from the Visual Studio Gallery.

image

Click on the  Bing Logo and the text “How do I …”.

image

Enter your query in the search field.

image

In this example I got 8 results, which I can browse between. Click on the Accept-button to insert the script to your code file.

image

Beautiful, Microsoft!

At Mindbite we develop web applications and websites using SiteFactory CMS as a content engine. In almost every project we develop custom modules or enhancements that extend the existing features of the system. For every module we create Class Libraries with objects reflecting the contents of each project’s database. All SiteFactory CMS websites use Microsoft SQL Server as a database server, and when the solution is hosted at Mindbite we specifically use Microsoft SQL Server 2008 R2 which is a fast and powerful database server.

We have a saying at Mindbite that we never do the same thing twice, and to live up to that we have different methods. For one, code re-use is a major part of our work method, as I’ve written about earlier. A more concrete way of incorporating this is our own tool Mindbite Code Tools.

Mindbite Code Tools is a Windows application designed to perform recurring time consuming tasks in just seconds. The smallest feature is a SQL Query parser that parses queries cutting off unnecessary query data. Another tool takes a list of privates and creates a set of properties in c# or vb. The major tool is a Class Library Builder which is kind of an Entity Framework lite.

Class Library Builder

The Class Library Builder (or as Microsoft should have shortened it; CLB) lets our developers connect to any Microsoft SQL Server and retrieve a list of databases. When selecting a database the user is presented with a list of tables. From here the user selects a table and specifies a Class Library name and in less than a second the builder sets up a new Class Library in c# or vb that contains:

  • Proper Namespace references.
  • An “Item” class containing:
    • Fields for all columns in the table.
    • Properties for all columns in the table.
    • Several useful constructors (based on our development model)
    • Fully functional methods for saving changes to the database (INSERT, UPDATE & DELETE).
    • All data input/output is automatically configured with the SiteFactory CMS Data Access Layer.
  • A class containing an “Items” property which is a List of Item.

The code that is generated is ready to use instantly, without any compilation errors.

The Class Library Builder is used on a daily basis by our developers, and it saves us a lot of time and money for our customers. Instead of doing the same thing twice in every project we can focus on what really counts, and that is creating smart solutions for the customers.

Closing comments

Before Mindbite Code Tools we spent hours creating the same kind of code structure in every project. Now, we don’t have to, and I couldn’t work a single day without this tool. Code Tools has been a part of our work method for a few years now and it’s being enhanced regularly. And for every new feature we get to focus on the things that matter.

Yesterday Microsoft released Internet Explorer 9 RC. One of the new features is Windows 7 JumpList integration. This morning we released a new update to SiteFactory CMS with full support for IE9 JumpList.

By default there are three standard activities in the SiteFactory CMS JumpList; New page, Write new Blog Post and Global Variables manager. We will be pushing out new updates with new standard activities later on.

This makes SiteFactory CMS one of the first Content Management Systems with built-in JumpList integration.

image

Extending SiteFactory CMS with custom activities

In this update we also released a new Eventhandler that is triggered when the system is pushing the JumpList Activities to the output. This gives our developers a way to add or modify existing JumpList Activities.

image
An example showing how to add a new Activity to the JumpList.

image
The result of the example extension.

One of the greatest things about SiteFactory CMS is the ability to extend it with Extensions. To make this possible, we developed an extension engine that was introduced in SiteFactory 3.0 SP3, and enhanced in SiteFactory CMS 4.0.

The extensions interacts with Eventhandlers in the system and we are adding more Custom Eventhandlers regularly.

Today we have Custom Eventhandlers in these areas:

Pages

The Page Object has the following Eventhandlers:

OnServing
Every time a page is sent to the output this Eventhandler kicks in and the PageServingEventArgs presents the HTML-output (Body) and you are able to make changes to it just before it is sent to the output.

image
A simple example that converts the Body content to Upper Case.

OnSaving
When a page is created, modified or deleted OnSaving is triggered. With the PageSavingEventArgs you can access the altered Page Object and also the type of change that was made.

image
This example sends an e-mail regarding the change.

Blog Module

The Blog Module currently has these two Eventhandlers that are similar to the ones in the Page Object.

OnServing
Is triggered when a blog post is sent to the output.

OnSaving
Is triggered when a blog post is created, modified or deleted. This is very useful when you want to attach a class for publishing a blog post to services like Facebook and Twitter.

Mindbite Core Foundation Server

The Core has these powerful Eventhandlers:

Security.GenericSecurity.OnSigningOn
When a user Signs On to SiteFactory CMS with internal credentials or public credentials this Eventhandler is triggered which gives the developer the ability to catch the User Object. It is also possible to cancel the Sign On Sequence.

Google.SiteMap.OnServing
Every time a change is made in the system, the Google Sitemap is regenerated (sitemap.xml) with new urls, timestamps and other SEO data. By default only Page Objects are represented in the Google Sitemap, but if the web site has other urls that must be added to the sitemap, the developer can use this Eventhandler to append new nodes in the sitemap.

image
This example adds the dummy url “http://www.dummydomain.com” to the Google Sitemap, and appends some SEO data.

Cms.Sitemap.OnServing
This Eventhandler is similar to the Google Sitemap Eventhandler, but it affects the Web Sitemap in the web site.

image

 

Closing comments

Extensions is a powerful and easy part of SiteFactory CMS, and it’s used in almost every project.

In my 4th part in this series I’ve decided to blog about Mindbite Core Foundation Server and our Update Server that keeps SiteFactory CMS 4 web sites updated with the latest version.

Last week I started developing a solution for a customer, with the target of creating an Excel Workbook programmatically.

Since we use Microsoft .Net Framework for all our solutions everything is familiar and it’s easy to get started with development against Office applications.

The solution gathers a huge amount of data and fills around 20-30 Worksheets with tables and charts. Everything is based on several filters sent to our application. So the data, number of Worksheets and the design of the charts are designed based on this.

I came across several obstacles in this solution that I want to share.

Debugging

It’s hard to debug an application that is communicating with the Excel Interop. The application simply crashes and the Excel Instance is not killed automatically. So I used one of my screens for the Task Manager, which made it easy for me to kill the instances every time my app crashed.

I still haven’t figured out how to debug this yet.

Performance

When working with the cells in your Worksheet, you must define a Range to work with. The range consists of cells and you can read and write data to them. It didn’t take long before I noticed that every time you put a value in a cell, a call is made to the Excel Interop. Even when putting small amounts of data in a small amount of cells you get slow performance.

The solution is working with Multi Dimensional Arrays, and putting the data into the array. When all data is in its place, you simply write the array to a designated Range in the Worksheet.

image

The example above is an array that works as a matrix. Since I  want to put numbers, strings, dates and formulas in the cells I use object in the array.

After declaring my array I put the data into it:

image

When it’s time to put the data into the Worksheet I do as follow:

image

This performance workaround may seem a little odd, and it’s nothing you’re used to when working with .net. In this case it’s completely necessary.

Using hidden data sources for Charts

In our case we must create several charts based on data that we don’t want to visualize for the person who opens the Excel Workbook. Our solution holds around 150 charts spread over around 25 Worksheets, and the data source for each Chart is unique.

Since (by what I know) you must use cell data as a data source for any chart in Excel we had to create around 150 “tables” containing the data. We felt it would be ugly to display this data in a Worksheet, displayable for any reader.

The solution we used was to create a Worksheet and filling it with data. Then we created the chart and used the data as a Data Source. The last thing we did was to hide the Worksheet that held the data.

You can hide a Worksheet in two ways:

image

This hides the Worksheet, but the user is able to make it visible in the Workbook. So we used the following instead:

image

This completely hides the Worksheet, and the user is unable to make it visible (maybe there is some way but I haven’t found it yet).

Compatibility

This isn’t really an obstacle, other than that a user may use an old version of Office Excel. But when calling the Excel Interop to Save the document to disk, you can provide the version of Excel you want to save for.

image

In our solution, that will be used by any Internet user, we target an older version of Microsoft Excel.

Closing comments

It’s both fun and interesting to work with Visual Studio Tools for Office, and it gives us a great chance to develop better business applications.

The solution we are working on now isn’t made public yet, but I will follow up with more blog posts about this later on.
I will dedicate at least one future blog post to code examples and tips.

As we say in Sweden; Swim Calm!