Friday, September 16, 2011

Seterlund.CodeGuard is now on NuGet

My small guard library is now available on NuGet.

The package name is Seterlund.CodeGuard and is available under the New BDS License.

The Guard.That(...) will throw an exception, when some condition is not met

public void SomeMethod(int arg1, int arg2)
{
    // This line will throw an exception when the arg1 is less or equal to arg2
    Guard.That(() => arg1).IsGreaterThan(arg2);

    // This will check that arg1 is not null and that is in some range 1..100
    Guard.That(arg2).IsNotNull().IsInRange(1,100);

    // Several checks can be added.
    Guard.That(arg1)
      .IsInRange(100,1000)
      .IsEven()
      .IsTrue(x => x > 50, "Must be over 500");

    // Do stuff
}


The Validate.That(...) method makes it possible to return a list of all error conditions

public void OtherMethod(int arg1)
{
    // Get a list of errors
    List<string> errors = Validate.That(() => arg1).IsNotNull().GetResult();
}
---
Share:

Tuesday, September 13, 2011

Howto create a simple IOC container

I'm using StructureMap in my current project and it works pretty well.
But how difficult could it be to make a simple IOC container. So I set out to create my own implementation and it proved to be quite easy to get the basics down.

How to setup the container.
var c = new Container();
c.For<IFoo>().Use<Bar>();

In case you need more controll over the creation of the class creation, I have added the posiblitity to use lamdas.
c.For<IFoo>().Use(() => new Bar1("someArgument"));
c.For<IFoo>().Use(ctx => new Bar2(ctx.Get<ISomeinterface>()));

How to resolve
var foo = c.Get<IFoo>();

The road ahead could be to handle scope/lifecycle.

The complete code can be found on GitHub

---
Share:

Friday, May 20, 2011

Disable password policies in ADLDS (Adam)

Since AD LDS uses the password policies from the local system or the domain, there is no way to set a specific password policy for the AD LDS instance. But there is a way to disable the policy so you could handle the password policy in your own code.

The following command disables the policy
dsmgmt "Configurable Settings" Connections "connect to server localhost:389" q "Set ADAMDisablePasswordPolicies to 1" "Commit changes" q q

Note! My instance of AD LDS is running on "localhost:389". Change it to the server and port that you use.

This nice article explains it in more detail
Share:

Thursday, April 7, 2011

Guard your code

You can call it code contracts or an argument guard. This is a simple implementation that can validate that the current state of a variable or method is as you expect it to be. If the validation fails, an exception is thrown. Example code:
public void SomeMethod(int arg1, int arg2)
{
    // This line will throw an exception when the arg1 is less or equal to arg2
    Guard.Check(() => arg1).IsGreaterThan(arg2);

    // This will check that arg1 is not null and that is in some range 1..100
    Guard.Check(arg2).IsNotNull().IsInRange(1,100);

    // Do stuff
}
Source code can be found on GitHub
Share:

Thursday, November 4, 2010

XmlTidy

Needed a tool to tidy some Xml in "Programmer's Notepad" and could not resist to created my own version of XmlTidy. :-)

using System;
using System.Xml;
using System.Xml.XPath;

namespace XmlTidy
{
    public class Program
    {
        private enum ExitCodes
        {
            Success = 0,
            Failure = 1
        }

        private static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                return ShowUsage();
            }

            var inputFile = args[0];
            var outputFile = args.Length == 1 ? args[0] : args[1];
            return Tidy(inputFile, outputFile);
        }

        private static int ShowUsage()
        {
            Console.WriteLine("Usage: XmlTidy <inputfile> <outputfile>");
            Console.WriteLine("if <outputfile> is not specified, the <inputfile> is overwritten.");
            return (int)ExitCodes.Success;
        }

        private static int Tidy(string inputFile, string outputFile)
        {
            var result = (int)ExitCodes.Success;
            try
            {
                var document = new XPathDocument(inputFile);
                var settings = new XmlWriterSettings { IndentChars = "\t", Indent = true };
                var writer = XmlWriter.Create(outputFile, settings);
                document.CreateNavigator().WriteSubtree(writer);
                writer.Close();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
                result = (int)ExitCodes.Failure;
            }

            return result;
        }
    }
}

Share:

Wednesday, November 3, 2010

Programmer's Notepad

Got a new laptop today and have started to install all the cool and useful tools I need. Previously I have used TextPad and Notepad++ as my notepad replacements, but this time I decided to see if there was some other text editors out there. Found "Programmer's Notepad" that looks promising. I looks simple and clean, but yet powerful.
Share:

Tuesday, October 19, 2010

Creating a self-signed certificate with private key

Needed to make a self-signed certificate with a private key for a project I working on. After some research I found that the combination of makecert and pvk2pfx did the trick. Use the following commands in Visual Studio Command Prompt
makecert -r -pe -n "CN=Test" -b 01/01/2010 -e 01/01/2020 -sky exchange Test.cer -sv Test.pvk
pvk2pfx.exe -pvk Test.pvk -spc Test.cer -pfx Test.pfx
When executing the commands above you will be asked for some information like password etc. After you are finished the file Test.pfx includes a self-signed certificate with the a private key.
Share: