Visual SourceSafe automation

I've been busy this week with quite an interesting little project regarding VSTO and SourceSafe and had the opportunity (misfortune?), working with the SourceSafe API (ssapi.ddl).

The API is pretty straightforward not much to it, I've included some code below, which demonstrates how to do a simple get latest.

The same approach can be used (minus the get method) to build yourself a little treeview, passing the needed node along with the recursion, generating a nested structure.

using SourceSafeTypeLib; using System.IO; class Program { static VSSDatabaseClass database = new VSSDatabaseClass(); private static void getLatest(string Spec, string Local) { VSSItem Item = database.get_VSSItem(Spec, false); Item.Get(ref Local, 0); foreach (VSSItem ChildItem in Item.get_Items(false)) { if (ChildItem.Type == 0) { getLatest(ChildItem.Spec, Path.Combine(Local, ChildItem.Name)); } } } static void Main(string[] args) { database.Open(@"\\sourcesafe\sources\srcsafe.ini", "username", "password"); getLatest("$/SomeProject", @"c:\SomeProject\"); } }

What we've got here, is a recursive method, retrieving all files within a specified repository, the get method will create the needed folders on your local machine, and fetch all files within that folder (excluding folders hence the need for recursion)

We can pass a number of flags (VSSFlags enumerator) to this method to alter its behavior, for whatever our needs may be.

Looking a bit further, you'll notice a number of abstract classes, interfaces an enumerators (imported from the ssapi assembly via interop functionality in .net) – which provide wrappers to the API.

The one you'd be interested in the most is VSSDatabaseClass – which is our main entry point, from there you can build the rest of your needed functionality.


 
Extension methods in C# 3.0

C# 3.0 introduced interesting new functionality, one of them being extension methods, which enables developers to add methods to existing types, without needing to create a new derived type (interfaces, classes, structs)

Extension methods behave much like instance methods, but have a number of limitations they can't for example access private nor public members of the type they're extending. (I can imagine the logic behind this)

Note the following code example and how its being defined, you need a static class (all members must be static) and the first parameter of the extension method contains the "this" keyword, informing the compiler which type we're extending. (you may add numerous parameters you wish to pass to this method, but the first parameter must always define the type you want to extend)

public static class ExtensionMethods { public static string Reverse(this string value) { char[] charArray = value.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } }

Typically (if you did everything right), your extension method should immediately reflect in your IDE (if you're using one), with a little blue arrow.
Extension Method

One can also write overloads to existing methods using extension methods, but cant override existing ones - remember that whenever you're planning to use these methods, the static class containing your extensions, obviously need to be brought in scope with whatever code you're planning to use it with (namespaces etc)

My feelings on this feature, is that its a tool that must only be used if its REALLY needed. (You can open a tin of tuna with a hammer, but there is better ways to do it)

Also if you have access to the source code to a class you need to extend, consider simply adding the method to the class, dont extend it - unless you've got very good reasons.

Imagine the impact of misusing this feature (which is true for most others), having your business logic for example, all over the show extended by extension methods, where you potentially come to the point where functionality gets decentralized, it can most definitely lead to some ugly unmanageable code.

I've noticed an interesting way Microsoft use extension methods in context with Linq - as soon as you include System.Linq (among others) as a namespace, a number of extension methods gets added, which enables developers to query those types with linq.


 
First 1 2 3 4 5  ... Last 
Recent Additions
Visual SourceSafe automation
Short post about the basics of automation in Visual SourceSafe
18 July 2008

Read More

Extension methods in C# 3.0
C# 3.0 introduced interesting new functionality, one of them being extension methods
8 July 2008

Read More

Embedding files(resources) into a web control
Simple demonstration on how to embed files into a web control
2 July 2008

Read More

 
Top 5 Most Popular Blogs
 
Most Recent Blogs
Advertising