Tuesday, December 4, 2012

SQL Maintenance Tools

Something to look into next time I need I need to review SQL Server Maintenance: http://ola.hallengren.com/

Tuesday, November 13, 2012

Dumping object to HTML table with a Razor view

Here's a razor view for rendering an object to html table.  Includes support for arrays, anonymous object, and complex objects with twitter bootstrap formatting.  Note: CamelToTitleCase() is a string extension function.

@model object
@{
    ViewBag.Title = "Object Information";
}
    
    @if(!Model.GetType().Name.Contains("Anonymous")){
        
    }
    
        @foreach (var prop in Model.GetType().GetProperties())
        {

}
    
@Model.GetType().Name
@prop.Name @if (prop.PropertyType.IsArray) { var arrayData = prop.GetValue(Model,null) as Array; if (arrayData != null) { foreach (var item in arrayData) { @Html.Partial("ObjectTable",item) } } } else if(prop.PropertyType.Namespace == null || !prop.PropertyType.Namespace.StartsWith("System")){ var data = prop.GetValue(Model,null); if(data != null){ @Html.Partial("ObjectTable",data) } } else { @Convert.ToString( prop.GetValue(Model,null)) }

Monday, October 8, 2012

Dynamic Business Rules in C#

On a few occasions now, dynamic validations have come in handy for externalizing business rules.  One technique I've employed is to use Dynamic LINQ queries with C# expressions being stored in external data stores.  This has worked well in that it prevented me from necessarily having to rebuild and redeploy for simple business rule changes.  The problem with this method is that C# is not necessarily an intuitive language.  This post provides an extension method for the slightly more intuitive SQL syntax:
http://www.sharpregion.com/sql-syntax-c-filtering/

I'll be giving this a try in future projects.  Perhaps offer either method of validation.

Check/toggle 'em all Bookmarklet

While looking for an add-in to assist with pages that do not provide select/unselect all options with massive checklists, I came across this Bookmarklet:

http://www.phpied.com/checktoggle-em-all/#comment-90438

Quite handy.

Wednesday, May 9, 2012

Custom MSBuild Release Script

Below and available here is a sample MSBuild  release script which performs the following:
  • Cleans output directories
  • Compiles "Release" configuration projects
  • Combines all assemblies into one DLL
  • Signs the resulting DLL
  • Creates a nuget package and updates version based on assembly file version
  • Deploys the nuget package to a local nuget server
Dependencies

    
        $(MSBuildProjectDirectory)\..
        $(solutiondir)\_Build.Release.Output
        $(buildoutputdir)\Nuget
        $(nugetdir)\Lib\net40
        $(solutiondir)\tmp
        $(dlldir)\MyTargetAssembly.dll
        $(programfiles)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client
        $(MSBuildProjectDirectory)\Build.Framework.Release.nuspec
        $(MSBuildProjectDirectory)\MJCACert.cer
        \\ecmspackages\Packages
    

    

    
        
    
    

    
        
    

    
        
        
        
        
    

    
        
            
        
    

    
        
    

            
        

        
            
        
        
    

    
        
        
    

    
        
    

    
        
    


Friday, January 13, 2012

Functional Fluent Code Sample

To increase code readability and code refactorability (new word), I find myself using fluent functions more and more.  As a result,  here is a code sample that might be used to toggle visibility of properties for a child view model.


using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System;
public interface ISelectiveDisplay {
    bool IsVisible { get; set; }
    ICollection<string> PropertiesToHide { get; }

}

public abstract class SelectiveDisplay<T> : ISelectiveDisplay {
    public SelectiveDisplay() {
        this.PropertiesToHide = new List<string>();
    }

    [ScaffoldColumn(false)]
    public bool IsVisible { get; set; }

    [ScaffoldColumn(false)]
    public ICollection<string> PropertiesToHide {
        get;
        private set;
    }

    private static string GetPropertyName(Expression<Func<T, object>> property) {
        PropertyInfo propertyInfo = null;
        if (property.Body is MemberExpression) {
            propertyInfo = (property.Body as MemberExpression).Member as PropertyInfo;
        } else {
            propertyInfo =
                (((UnaryExpression)property.Body).Operand as MemberExpression)
                .Member as PropertyInfo;
        }
        return propertyInfo.Name;
    }


    public void HideProperty(Expression<Func<T, object>> prop) {
        this.PropertiesToHide.Add(GetPropertyName(prop));
    }
}