Tuesday, 25 September 2012

Create a new Git remote repository on a windows network share

I've created remote repositories a few times on a network share, but each time I'd spent too many hours trying to recreate my steps. Most of the google posts seem to come from non-windows users so doesn't help me much. If you are a windows/git user I'll show you how to achieve this in a few easy steps.

In the command prompt navigate to the network share directory making sure all required permissions are set and issue the following command.

git init

Next create a dummy file e.g. a README.txt file in the remote repository and commit this file.

git add -A

git commit -m 'Initial cimmit'

This will make the repository a non empty non bare repository and force the creation of the master branch. Now navigate to your local folder and clone the remote repository with the following command.

git clone "network share directory/working directory"

Navigate back to the remote repository and set the following config

git config --bool core.bare true

Now you can resume normal business by working in your local folder and when ready to commit to the remote repository, do the following


git add -A
git commit -m 'Added some good code'
git push origin master

Happy coding!!!.

Monday, 30 July 2012

Automate Nuget Package Creation using MSBuild

There are lots of blogs on this topic, but none of them helped me achieve my goal 100%. To state my problem, I wanted a Continuous Integration strategy which meant that every class library I built for the other members of the team would be accessible via a Nuget package. Every checkin will 
  • Trigger a new version of the Class Library to be built
  • A nuget package created with the new version number of the class library included in the name of the package.
  • The package will be moved to a network shared location where the other team members can consume the package
In a previous blog post I describe how you can create an Assembly file and share it across all your projects in the solution.

Now we create a MSBuild file and add and call the following targets:

<Target Name="CreatePackage">
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) spec -f" />      
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) pack MyProject.csproj -Verbose -Prop Configuration=Release" />
</Target>

<Target Name="MovePackage">    
       <Exec WorkingDirectory="$(WorkingFolder)"
          Command="move /y *.nupkg $(PackageDir)" />
</Target>

The NugetApp variable represents the path to the Nuget.exe command line utility used to create the Nuget package. In my case, I downloaded the utility into a folder on the same level as the working folder and named it "Tools" with subfolder "Nuget", I set the NugetApp property to like so:

<NugetApp>$(WorkingFolder)/../Tools/Nuget/Nuget.exe</NugetApp>

The PackageDir property is whatever network share location of your choice.

Now run your your MSBuild file in TeamCity.

Happy building!!!.

Thursday, 19 July 2012

ASP.NET MVC 3 Share App_GlobalResources files in all Class Libraries

I wanted to add all my global resource/.resx files e.g. Error Messages into the  the same folder in the ASP.NET MVC 3 App_GlobalResources folder and shared the settings in all referenced class libraries. Well, the idea is to have only one file to maintain, as opposed to ErrorMessage resource file per class library. So I went through the following steps:


  • Write-Click the ASP.NET MVC 3 project and Add ASP.NET Folder App_GlobalResources.
  • Change the Access Modifier to "Public" from "Internal". If this is greyed out in the VS 2010 UI, open the accompanying .Designer.cs file and change the access level to "Public".
Inside the ASP.NET MVC 3 project you can access this resource using the class generated in the Designer.cs file. Life is a little different from inside a references class library, but not too complicated. If you open the .Designer.cs file you will notice the Strongly-Typed references utilises the ResourceManager class in the System.Resources Namespace.

I will use the same class from the class libraries. Find the code below;

var temp = new ResourceManager("Resources.ErrorMessages", Assembly.Load("App_GlobalResources"));
var resourceNameString = temp.GetString("My_Resource_Name");

Of course in a real application it will be advisable to create a custom strongly-typed and Interfaced class 
with properties that will return the values of your settings, so you can unit test your main application code by mocking the settings in your resource files.

Happy coding!!!!.

Friday, 9 March 2012

Convert XMLDocument to XML string in JavaScript

Some Chart controls do utilize xml documents for rendering data. If you need to update the data in the chart without a roundtrip to the server the function below can help you extract the xml string, update the content and send the results back to the Chart control.

getXmlString: function (xmlDocument) {
var xmlStr;
if (typeof XMLSerializer != "undefined" && !($.browser.msie && parseInt($.browser.version) == 9)) {
xmlStr = (new XMLSerializer()).serializeToString(xmlDocument);
}
else {
xmlStr = xmlDocument.xml;
}
return xmlStr;
}

Monday, 13 February 2012

Upgrade ASP.NET MVC 2 (Net 3.5) to MVC 3 (Net 4.0)

There are a few blog posts on this topic, but I found most of them don't fit all scenarios. Indeed this post is no different. Put together they might rebuild the world, who knows. Anyway, these
are the steps I went through to implement my upgrade.

Step 1: Start Visual Studio 2010 and run the Solution (.sln) file. Follow the steps to perform the upgrade. Change the target Framework version from 3.5 to 4.0 for all projects in the solution.

Step 2: Remove all references to framework 3.5 assemblies in the web.config file in the ConfigSections. i.e. the following;

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> >
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>

This will probably do for those who want to upgrade to .Net 4.0 without the upgrade to MVC 3. If MVC 3 is the preferred flavour, continue.

Step 3: Download and Install ASP.NET MVC 3.

Step 4: If you intend to use the new Razor View engine like I do, then add the following to the your web.config ConfigSections;

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

Step 5: On the official MVC website there's a white paper on how to go about the upgrade, only problem is I don't need most of it so I cherry picked what I wanted from this and other posts.

Step 6: Replace all occurences of System.Web.Mvc in the web project config files (Web.config) with the MVC 3 version.

i.e. Replace

System.Web.Mvc, Version=2.0.0.0
with

System.Web.Mvc, Version=3.0.0.0
e.g.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

Step 7: For each project in the solution Add/Replace references to the following MVC 3 dlls;

System.Web.Helpers.dll
System.Web.Mvc.dll
System.Web.Razor.dll
System.Web.Routing.dll
System.Web.WebPages.Deployment.dll
System.Web.WebPages.dll
System.Web.WebPages.Razor.dll

These are located in the following folders;

%ProgramFiles%\ Microsoft ASP.NET\ASP.NET MVC 3\Assemblies
%ProgramFiles%\ Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies

Step 8: Last but not least, we want the Razor View to be an option when we use the "Add View" dialogue. As per the white paper, unload the web projects in the solution and replace the GUID {F85E285D-A4E0-4152-9332-AB1D724D3325} with {E53F8FEA-EAE0-44A6-8774-FFD645390401} and reload the project.

Step 9: Go on and do your thing.

Wednesday, 8 February 2012

Extending Backbone.js view using custom JavaScript namespacing

Backbone.js is an open source class library that enables an efficient client-side MVC implementation. Backbone shines if the requirement is to maintain a large collection of data on the client-side without the unnecessary round trips to the server.

I've been working on an ASP.NET MVC 3 project that involves quite a number of pages with similar look and feel. So instead of building every page from the ground up, we decided to developer a vanilla page and use it as a template for all other pages. Anyway, to encourage a reuse of the generic JavaScript file we'd come up with we split the Backbone views into separate files (ala UserControls) so we would reuse them in any other project.

We decided on a Namespacing strategy that would reduce tendency for method name collision.

To wrap a JavaScript class in a namespace;

Regedit.View = function(){
//Write your private functions here
//e.g. function myprivates() {}
return {
//Write your public functions here
//e.g mypublic: function(){}
};
}();
Note this will work only if you already have an existing "Regedit" namespaces already defined and referenced by this file. With this on place we could extend/inherit a Backbone view module like so;

Regedit.View = function () {
return {
extend: function (classProps) {
return Backbone.View.extend({
el: $("#someElement"),
initialize: function (customCollection) {
_.bindAll(this, "render"); //Enable This keyword in render method
//pass the view context on to the derived the derived class
//This will enable the derived class use the "this" keyword to refer this view
if(classProps.initialize != undefined){
classProps.initialize(this);
}
//this.models = customCollection;
},
template: $("#template"),
events: {
"change #someEvent": "SomeEventHandler"
},
SomeEventHandler: function(){
//this.contructor.someDerivedClassMethod();
}
}, classProps);
}
};
} ();

With our custom view in place, we can create a view using our custom module;

var newView = Regedit.View.extend({
//Custom view properties here
initialize: function(viewContext){ //this will be called from the base class
_.bind(viewContext, this.derivedClassMethod)
},
derivedClassMethod: function(){
this.someBaseClassMethod() //The "this" keyword will refer to the current view context
}
});

The class utilises the underscore.js library's _.extend method to extend/inherit a backbone view, but the same technique can be employed to extend a backbone model or collection as well.

This post is not a tutorial for Backbone.js. To learn more about or download Backbone.js, please visit the backbone.js website.

Happy coding.

Saturday, 4 February 2012

GroupBy multiple columns using Dynamic LINQ

It can be quite useful at times to have the capability to identify duplicate objects based on custom rules (or in my case based on user input). Since this input is not known at design time, I started looking for a Dynamic Linq functionality that would take a collection of objects, a list of columns and dynamically group by the list of columns at runtime.

The closest I came to what I was looking for is a listing on Scott Gu's blog. There's a link in the blog to download the DynamicLibrary.

Now the following function can be authored to utilise the "GroupBy" extension method on the downloaded class to perform the Dlinq operation.

var query = customerData.AsQueryable().GroupBy("new ("Title, FirstName, LastName")", "it", null);
var groupedCustomerData = (from IEnumerable<Customer> groupedData in
query select groupedData.FirstOrDefault()).ToList();

return groupedCustomerData;


The "it" keyword is used as the KeySelector parameter if the requirement is to return the whole customer object in the group. The code above should group the customer data collection by the Title, FirstName and the LastName attributes.