My Adventures in Coding

February 24, 2014

C# – Mocking a method to return a different value when called a second time using Moq

Filed under: .NET,c#,Mocking,Testing — Brian @ 2:36 pm
Tags: , , , ,

I have been using Moq as my primary mocking framework in C# for several years now. Overall I really like it, the syntax is easy to read, the mock setup is straight forward, etc. However, there are a few cases I need to mock infrequently and I always forget how to do the setup. Often because I am cycling between Scala, Python, and C#. So in this post I am just listing those three cases in hopes they may help others, yet at the same time as a reference to myself!

So for this example, first here is my example service that we will be mocking in our tests:

namespace MoqExample
{
    public interface ISomeService
    {
        SomeStuff GetNextStuff();
        void DoStuff();
    }

    public class SomeService : ISomeService
    {
        public SomeStuff GetNextStuff()
        {
            return new SomeStuff();
        }

        public void DoStuff()
        {
        }
    }

    public class SomeStuff
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Return a different value the second time a mocked method is called

Let’s say I have a call in a service that returns the next item to process, such as pulling in customer requests from a queue that need to be processed in order, one at a time. In this case I might want to have a test that demonstrates the application can handle two different requests back to back, or even handle the case where there is an item to process, but the next item returned is empty, meaning there is nothing left to process. If I need to test this case there are several simple ways to do this with Moq.

The more well known approach is to just use a Queue and have the mock call Dequeue and return the result each time the mocked method is called.

[Test]
public void MogMethodThatReturnsADifferentValueWhenCalledASecondTimeUsingAQueue()
{
    Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
    var queueStuff = new Queue<SomeStuff>();
    queueStuff.Enqueue(new SomeStuff { Id = 1, Name = "Real" });
    queueStuff.Enqueue(null);
    _mockSomeService.Setup(x => x.GetNextStuff()).Returns(queueStuff.Dequeue);

    Assert.IsNotNull(_mockSomeService.Object.GetNextStuff());
    Assert.IsNull(_mockSomeService.Object.GetNextStuff());
}

The alternative is to use a feature in Moq called Sequences which allows you to set multiple return values, that will be returned one at a time in order, each time the mocked method is called.

[Test]
public void MogMethodThatReturnsADifferentValueWhenCalledASecondTimeUsingSequences()
{
    Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
    _mockSomeService.SetupSequence(x => x.GetNextStuff())
		    .Returns(new SomeStuff {Id = 1, Name = "Real"})
		    .Returns(null);

    Assert.IsNotNull(_mockSomeService.Object.GetNextStuff());
    Assert.IsNull(_mockSomeService.Object.GetNextStuff());
}

Throw an exception the first time a mocked method is called, return a value the second time

In this case I want to test that my application will handle the case that when a call to a service throws an exception, it will retry and if it receives a valid response on the second try, process the request successfully and continue. You can accomplish this by using a feature in Moq called “Callback”.

[Test]
public void MogMethodThatThrowsAnExceptionFirstTimeCalledAndAnObjectWithSecondTime()
{
    Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
    var calls = 0;
    _mockSomeService.Setup(x => x.GetNextStuff())
	.Returns(() => new SomeStuff {Id = 1, Name = "Real"})
	.Callback(() =>
	{
	    calls++;
	    if (calls == 1)
		throw new Exception("Failure");
	});

    Assert.Throws<Exception>(() => _mockSomeService.Object.GetNextStuff());
    Assert.IsNotNull(_mockSomeService.Object.GetNextStuff());
}

Now, it is true that you can use a Moq Sequence to return a different value each time a mocked method is called, but as far as I can tell you can only use this where the valid value is first and throwing an exception is the last item in the sequence. In my case above I explicitly wanted to test that an exception was thrown on the first call and a valid value was returned on the second call. However, if all you need to test in your code is how it handles a valid value on the first call and an exception being thrown on the second call, you can use a Sequence for your mock setup.

[Test]
public void MockMethodThatReturnsAnObjectFirstTimeCalledAndThrowsAnExceptionSecondTimeUsingSequences()
{
    Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
    _mockSomeService.SetupSequence(x => x.GetNextStuff())
	.Returns(new SomeStuff())
	.Throws<Exception>();

    Assert.IsNotNull(_mockSomeService.Object.GetNextStuff());
    Assert.Throws<Exception>(() => _mockSomeService.Object.GetNextStuff());  
}

Mock a void method to throw an exception

Lets say I have some void method that normally just silently does some task for me and has no need to have a return type, such as a call to write a stat or a log entry. However, if I want to test how my application handles the case when this call throws an exception, I can use the following setup to mock this method.

[Test]
public void MogMethodThatThrowsAnExceptionIsVoidAssertExceptionIsThrown()
{
    Mock<ISomeService> _mockSomeService = new Mock<ISomeService>();
    _mockSomeService.Setup(x => x.DoStuff()).Throws(new Exception("Failure"));

    Assert.Throws<Exception>(() => _mockSomeService.Object.DoStuff());
}

March 27, 2013

C# – Using Unity for Dependency Injection in an ASP.NET MVC 4 Web API

Filed under: .NET,c#,MVC,REST — Brian @ 11:27 pm
Tags: , , ,

When I created my first REST API since I started working with MVC 4 Web API, immediately I wanted to figure out how to handle dependency injection. When I had previously worked in Java I used Spring, in Scala I used Google Guice, and so now I was on the hunt for another dependency injection tool. There are several options out there, such as Spring .NET and also Ninject, however for now we decided to go with Unity because of it’s simple setup and maturity.

With MVC 4 Web API the application creates an instance of the controller class whenever a REST call is made. So for example if you have the REST endpoint /cars it will create an instance of the controller CarsController to service the request. Now, this all works fine until you change the constructor on CarsController to take a parameter. Once you do this, you will need a dependency injection framework to handle knowing how to resolve the dependencies for CarsController when one is being created. So let’s use this as our example.

Note: For this example I am using:
Visual Studio 2012 Express Web and version 2.1.505.2 of the Unity NuGet package.

Now, in Visual Studio 2012 NuGet is built right in, so no need to install the NuGet Plugin anymore (Woohoo!)

Step 1 – Create a new Web API Project

Create a new Web API project in Visual Studio:

  • File -> New Project
  • Installed -> Templates -> Visual C# -> Web
  • Give the project the name: UnityExample
  • Click “OK”
  • Select Project Template -> Web API
  • Click “OK”

That’s all! Now you have a new Web API project called UnityExample.

Step 2 – Install Unity NuGet Package

For installing NuGet packages you can use either the GUI tool or the console.

To use the Package Manager Console tool:

  • To open the console: Tools -> Library Package Manager -> Package Manager Console
  • Now at the console prompt enter the command: Install-Package Unity

Now, if you prefer to use the GUI tool:

  • In Solution Explorer, right click on the solution and select Manage NuGet Packages for Solution
  • Ensure Online is selected, then in the search box type Unity
  • The Unity package will be displayed at the top of the list then click Install

Step 3 – Create an IOC (Inversion of Control) Container

You will need to create an IOC container that impelements IDependencyResolver. I did not write my own, I got this code from here.

using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;
using Microsoft.Practices.Unity;

namespace UnityExample
{
    // This code is from:
    // http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
    // The website also has a detailed explanation of how to setup dependency injection

    public class ScopeContainer : IDependencyScope
    {
        protected IUnityContainer container;

        public ScopeContainer(IUnityContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            this.container = container;
        }

        public object GetService(Type serviceType)
        {
            if (container.IsRegistered(serviceType))
            {
                return container.Resolve(serviceType);
            }
            else
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (container.IsRegistered(serviceType))
            {
                return container.ResolveAll(serviceType);
            }
            else
            {
                return new List<object>();
            }
        }

        public void Dispose()
        {
            container.Dispose();
        }
    }

    class IoCContainer : ScopeContainer, IDependencyResolver
    {
        public IoCContainer(IUnityContainer container)
            : base(container)
        {
        }

        public IDependencyScope BeginScope()
        {
            var child = container.CreateChildContainer();
            return new ScopeContainer(child);
        }
    }
}

Step 4 – Create a new class called NameService.cs

The following class with be the dependency that we will inject into our new controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UnityExample
{
    public class NameService
    {
        private string _myName;

        public NameService(string myName)
        {
            _myName = myName;
        }

        public string GetMyName()
        {
            return string.Format("My name is: {0}", _myName);
        }
    }
}

Step 5 – Create a new Controller called NameController which requires NameService as a constructor parameter

To create the new controller NameController:

  • Right click on the Controllers folder and select Add -> Controller
  • Enter Controller name: NameController
  • From the Template drop-down select Empty API controller

Now enter the following code in your new NameController class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace UnityExample.Controllers
{
    public class NameController : ApiController
    {
        private NameService _nameService;

        public NameController(NameService nameService)
        {
            _nameService = nameService;
        }

        public string Get()
        {
            return _nameService.GetMyName();
        }
    }
}

Step 6 – Setup Dependency Injection with Unity in Global.asax.cs

Now the final step is to create an instance of NameService and tell Unity to use that instance anytime an instance of NameController is created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

// Add usings for Unity and Controllers
using Microsoft.Practices.Unity;
using UnityExample.Controllers;

namespace UnityExample
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Create a new Unity dependency injection container
            var unity = new UnityContainer();

            // Register the Controllers that should be injectable
            unity.RegisterType<HomeController>();
            unity.RegisterType<NameController>();

            // Register instances to be used when resolving constructor parameter dependencies
            unity.RegisterInstance(new NameService("It Worked!!!"));

            // Finally, override the default dependency resolver with Unity
            GlobalConfiguration.Configuration.DependencyResolver = new IoCContainer(unity);
        }
    }
}

Step 7 – Now let’s run the app and see that it worked!

Now run the app: Debug -> Start Without Debugging or (Ctrl F5).

When a browser opens, you will see the usual default home page for a .Net MVC 4 Web API application.

Now add the following to your url: /api/name. For example: http://localhost:62283/api/name.

Note: Remember the default path for all ApiControllers in Web API always starts with /api.

You should see the following output:

<string>My name is: It Worked!!!</string>

That is all!

Now that you have a working example, I recommend you read more about how it works in the following article:

Using the Web API Dependency Resolver

January 21, 2013

LINQ to SQL – Unit Testing a Repository

Filed under: .NET,c#,LINQ,Testing — Brian @ 10:47 pm
Tags: , ,

Over the last few months I have been using LINQ to SQL for accessing a SQL Server database from an MVC 4 WEB API application. Overall it works very well, and when looking at the SQL it generates, it does generate very efficient SQL. Also, I really like the LINQ to SQL syntax as well, very simple and clean, and it is easy to translate a SQL query into the LINQ to SQL syntax.

So normally in the past when I was testing a repository in Java/Scala projects that used Hibernate, we just used SQLite for our unit tests. But since the syntax used in LINQ to SQL is just LINQ expressions, I was wondering if it would be possible to unit test the LINQ expressions in my repository by just passing in a list of objects.

I looked at several existing solutions for unit testing repositories using LINQ to SQL, which looked good, but they each had the same problem. They were creating a repository with a DataContext being provided in the constructor, which allowed it to be mocked in a test. But, the problem with this approach is that the DataContext is designed to be used for a single database transaction, so you should always create and dispose of a new DataContext for reach request to the database. For Example I would expect each method in the repository to look like:

using (var db = new EmployeeDataContext(new DataContext(_connectionString)))
{
    return (from employee in db.Employees() where employee.Id == id select employee).SingleOrDefault();
} 

Instead of:

return (from employee in existingContext.Employees() where employee.Id == id select employee).SingleOrDefault();

The only way this other approach would work reliably would be if the application were creating a new instance of the repository class on every request, but I did not want to do that. So the solution I came up with to get the tests working the way I wanted AND while still creating a new DataContext for each transaction, was to create a wrapper class for the DataContext. This allowed me create a stub for the DataContextWrapper that could be used to replace the real one in a test. This may not be the most eloquent solution, but it worked for what I needed. If anyone has a cleaner solution for accomplishing this, please let me know!

Let’s use the standard database “Employee” example, just to be consistent.

Note: I used Moqand NUnit in this example.

Employee Entity
Just a standard LINQ to SQL entity object

[Table(Name = "dbo.Employee")]
public class EmployeeEntity
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true)] public int Id { get; set; }
    [Column] public string FirstName { get; set; }
    [Column] public string LastName { get; set; }
    [Column] public DateTime StartDate { get; set; }
    public override string ToString()
    {
        return string.Format("Id={0}, FirstName={1}, LastName={2}, StartDate={3}", Id, FirstName, LastName, StartDate);
    }
}

Employee Data Context
DataContext for accessing the “Employees” table in our database.

public interface IEmployeeDataContext : IDisposable
{
    ITable<EmployeeEntity> Employees();
    void ExecuteCommand(string command, params object[] parameters);
    void SubmitChanges();
}

public class EmployeeDataContext : IEmployeeDataContext
{
    private readonly DataContext _dataContext;

    public EmployeeDataContext(DataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public ITable<EmployeeEntity> Employees()
    {
        return _dataContext.GetTable<EmployeeEntity>();
    }

    public void ExecuteCommand(string command, params object[] parameters)
    {
        _dataContext.ExecuteCommand(command, parameters);
    }

    public void SubmitChanges()
    {
        _dataContext.SubmitChanges();
    }

    public void Dispose()
    {
        _dataContext.Dispose();
    }
}

Data Context Wrapper
Now, let’s wrap the EmployeeDataContext in another class that has a method called CreateDataContext() which allows us to create a new EmployeeDataContext.

public interface IDataContextWrapper
{
    IEmployeeDataContext CreateDataContext();
}

public class DataContextWrapper: IDataContextWrapper
{
    private readonly string _connectionString;
    public DataContextWrapper(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IEmployeeDataContext CreateDataContext()
    {
        return new EmployeeDataContext(new DataContext(_connectionString));
    }
}

Employee Repository
The constructor of our EmployeeRepository now takes a parameter of type IDataContextWrapper. So in each method that calls the database, it can now call _dataContextWrapper.CreateDataContext() in a using block to create a new DataContext for the request and also ensure the DataContext will be disposed.

public interface IEmployeeRepository
{
    EmployeeEntity AddEmployee(EmployeeEntity employee);
    EmployeeEntity GetEmployee(int id);
    List<EmployeeEntity> GetAllEmployees();
    List<EmployeeEntity> GetEmployeesByLastName(string lastName);
    List<EmployeeEntity> GetEmployeesByStartDate(DateTime minDate, DateTime maxDate);
}

public class EmployeeRepository : IEmployeeRepository
{
    private readonly IDataContextWrapper _dataContextWrapper;

    public EmployeeRepository(IDataContextWrapper dataContextWrapper)
    {
        _dataContextWrapper = dataContextWrapper;
    }

    public EmployeeEntity AddEmployee(EmployeeEntity employee)
    {       
        using (var db = _dataContextWrapper.CreateDataContext())
        {
            db.Employees().InsertOnSubmit(employee);
            db.SubmitChanges();
            return employee;
        }    
    }

    public EmployeeEntity GetEmployee(int id)
    {
        using (var db = _dataContextWrapper.CreateDataContext())
        {
            return (from employee in db.Employees() where employee.Id == id select employee).SingleOrDefault();
        }        
    }

    public List<EmployeeEntity> GetAllEmployees()
    {
        using (var db = _dataContextWrapper.CreateDataContext())
        {
            return (from employee in db.Employees() select employee).ToList();
        }  
    }

    public List<EmployeeEntity> GetEmployeesByLastName(string lastName)
    { 
        using (var db = _dataContextWrapper.CreateDataContext())
        {
            return (from employee in db.Employees() where employee.LastName == lastName select employee).ToList();
        }    
    }

    public List<EmployeeEntity> GetEmployeesByStartDate(DateTime minDate, DateTime maxDate)
    {
        using (var db = _dataContextWrapper.CreateDataContext())
        {
            return (from employee in db.Employees() where employee.StartDate >= minDate && employee.StartDate <= maxDate select employee).ToList();
        }
    }
}

Stub Data Context Wrapper
Now let’s create a stub class for our DataContextWrapper which will be used to replace the real one in our unit tests.

public class StubDataContextWrapper : IDataContextWrapper
{
    private readonly IEmployeeDataContext _dataContext;
    public StubDataContextWrapper(IEmployeeDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public IEmployeeDataContext CreateDataContext()
    {
        return _dataContext;
    }
}

Stub Employee Table
Also, we will need to create a stub of the employee table, which will be used in our tests to wrap the list of EmployeeEntity objects we will return as the result from our EmployeeDataContext when it is mocked in our tests.

public class StubEmployeeTable : ITable<EmployeeEntity>
{
    protected List<EmployeeEntity> internalList;

    public StubEmployeeTable(List<EmployeeEntity> list)
    {
        internalList = list;
    }

    public void Attach(EmployeeEntity entity)
    {
    }

    public void DeleteOnSubmit(EmployeeEntity entity)
    {
        internalList.Remove(entity);
    }

    public void InsertOnSubmit(EmployeeEntity entity)
    {
        internalList.Add(entity);
    }

    public IEnumerator<EmployeeEntity> GetEnumerator()
    {
        return this.internalList.GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.internalList.GetEnumerator();
    }

    public Type ElementType
    {
        get { return this.internalList.AsQueryable().ElementType; }
    }

    public System.Linq.Expressions.Expression Expression
    {
        get { return this.internalList.AsQueryable().Expression; }
    }

    public IQueryProvider Provider
    {
        get { return this.internalList.AsQueryable().Provider; }
    }
}

Employee Repository Unit Tests
Now we can write our unit tests around the EmployeeRepository. The first step is to mock the EmployeeDataContext. Next we create a StubDataContextWrapper passing in the mock object. Then to create the EmployeeRepository we pass in the StubDataContextWrapper. We setup some EmployeeEntity objects with data we will use for testing, then finally we set the test data as the return value for the Employees() method on the mock of our EmployeeDataContext, using the StubEmployeeTable to wrap the result, to make it look like a response from a LINQ to SQL query.

[TestFixture]
public class EmployeeRepositoryTest
{
    private Mock<IEmployeeDataContext> _mockEmployeeDataContext;
    private IDataContextWrapper _stubDataContextWrapper;
    private IEmployeeRepository _employeeRepository;

    [SetUp]
    public void Setup()
    {
        _mockEmployeeDataContext = new Mock<IEmployeeDataContext>();
        _stubDataContextWrapper = new StubDataContextWrapper(_mockEmployeeDataContext.Object);
        _employeeRepository = new EmployeeRepository(_stubDataContextWrapper);

        var employee1 = new EmployeeEntity { Id  = 1, FirstName = "John", LastName = "Smith", StartDate = new DateTime(2013,01,10)};
        var employee2 = new EmployeeEntity { Id  = 2, FirstName = "Frank", LastName = "Smith", StartDate = new DateTime(2013,01,15)};
        var employee3 = new EmployeeEntity { Id  = 3, FirstName = "Stan", LastName = "Johnson", StartDate = new DateTime(2013,01,20)};

        var employees = new List<EmployeeEntity> {employee1, employee2, employee3};
        _mockEmployeeDataContext.Setup(x => x.Employees()).Returns(new StubEmployeeTable(employees));
    }

    [Test]
    public void AddEmployeeShouldAddNewEmployeeWhenDoesNotExist()
    {
        _employeeRepository.AddEmployee(new EmployeeEntity { FirstName = "New", LastName = "Employee", StartDate = new DateTime(2013, 01, 25) });
        Assert.AreEqual(4, _employeeRepository.GetAllEmployees().Count);
    }

    [Test]
    public void GetAllEmployeesByIdShouldReturnEmployeeWhenIdExists()
    {
        var results = _employeeRepository.GetAllEmployees();
        Assert.AreEqual(3, results.Count);
    }

    [Test]
    public void GetEmployeeByIdShouldReturnEmployeeWhenIdExists()
    {
        var result = _employeeRepository.GetEmployee(1);
        Assert.AreEqual(1, result.Id);
    }

    [Test]
    public void GetEmployeesByLastNameShouldReturnEmployeeWhenLastNameMatches()
    {
        var results = _employeeRepository.GetEmployeesByLastName("Smith");
        Assert.AreEqual(2, results.Count);
    }

    [Test]
    public void GetEmployeesByStartDateShouldReturnEmployeeWhenStartDateIsWithinRange()
    {
        var results = _employeeRepository.GetEmployeesByStartDate(new DateTime(2013, 01, 14), new DateTime(2013, 01, 16));
        Assert.AreEqual(1, results.Count);
        Assert.AreEqual(new DateTime(2013, 01, 15), results[0].StartDate);
    }
}

That is all. Of course this example can be made more generic, but it is just a simple example. If you have an example that works better for LINQ to SQL, please let me know, I would be happy to hear about it!

August 18, 2012

Scala to C# – The Scala Developers Introduction to C# 4.0

Filed under: .NET,c#,Scala — Brian @ 10:55 pm
Tags: ,

I worked for years in .NET back in the 1.1 to 2.0 days, then moved off to Java, then Python, and finally Scala. However, now I am back to doing some .NET again after being away from it for almost four years. I work in Scala on a daily basis and when I heard I would be doing .NET again I was a bit sad, since I enjoy the syntax in Scala so much. However, since my last experience with .NET was in the 2.0 days, things have changed for the better. With .NET 4.0 a lot of functional style syntax has been incorporated into the language (They have done the opposite of Java and continued to evolve the language). So as I have been working with .NET 4.0, everyday I find myself thinking “If only I had Scala, I could write this in one line”. This has prompted me to actually look for equivalents, and it turns out there are a great deal of them! (Hint: LINQ is a great place to start!)

The funny thing is I am the only person in my office who is willing to work on both sides. I just want to use the best tool for the job, or the tool that makes the most business sense given what needs to be done, the resources available, and what code already exists. So I thought I would write up a few quick Scala and C# examples showing the equivalent syntax to help get you started.

Also, as a note, if you are using C# 4.0, I highly recommend using the re-factoring tool Resharper made by JetBrains who make IntelliJ. This tool has been very handy, often I will write a line or two of code in the old way in C#, and it will highlight and suggest the newer cleaner syntax, and in most cases will actually translate what I have written into the new style (e.g, if you write foreach() {} to loop through a list it will rewrite it as myList.ForEach() etc.). That is a trivial example, but for more complex statements it is a great teaching tool.

ForEach on a list

Let’s create a list of strings and loop through them, printing each string to the console.
Scala:

val someStrings = List("one","two","three")
someStrings.foreach(x => println(x))

C#:

var someStrings = new List<string> { "one", "two", "three" };
someStrings.ForEach(x => Console.WriteLine(x));

Scala has “_” but C# has Method Groups

In the above example any Scala developer would say “But you can write that example with the much cleaner syntax using the underscore”. Yes, that is true I totally agree. In fact I was annoyed that I did not have an equivalent until my ReSharper prompted me in Visual Studio to rewrite the above C# statement using Method Groups. So here they are:
Scala:

val someStrings = List("one","two","three")
someStrings.foreach(println(_))

C#:

var someStrings = new List<string> { "one", "two", "three" };
someStrings.ForEach(Console.WriteLine);

So in C#, since the parameter being passed is a string, and the method takes a string, there is no need to explicitly write it out. This is also very handy when using LINQexpressions, such as “Select”.

Tuples

I was working on a piece of code recently in C# and really wished I could use a Tuple in C#. Well, once again I was surprised to find out it was available:
Scala:

val myTuple = ("Hello", 123)
println(myTuple._1)
println(myTuple._2)

C#:

var myTuple = Tuple.Create("Hello", 123);
Console.WriteLine(myTuple.Item1);
Console.WriteLine(myTuple.Item2);

FoldLeft equivalent is Aggregate

Let’s use the basic introduction to FoldLeft example and calculate the sum of a list of numbers.
Scala:

val numbers = List(1,2,3,4,5)
val total = numbers.foldLeft(0)((result,current) => result + current)
println(total)
//Of course any Scala developer would lean towards the Syntax which I have not found an equivalent for in C#
val total2  = numbers.foldLeft(0)(_+_)
println(total2)

C#:

var numbers = new List<int> {1, 2, 3, 4, 5};
var total = numbers.Aggregate(0, (result, current) => result + current);
Console.WriteLine(total);

As you can see the syntax for the Aggregate function is very similar. In fact it took me a while to figure out what method call in C# was the equivalent to a foldLeft just because of the name. Also just as a note their are two types of Aggregate, “Simple” is where you do not need to specify an initial value, while “Seed” is where you do specify an initial value, so the example above is a “Seed” since we are starting our sum with an initial value of 0.

Yes, they both have the “sum” function as well

I know that is a trivial foldLeft and aggregate example, so just to be clear, yes they both have “sum” as well.
Scala:

val numbers = List(1,2,3,4,5)
val total = numbers.sum
println(total)

C#:

var numbers = new List<int> {1, 2, 3, 4, 5};
var total = numbers.Sum();
Console.WriteLine(total);

Filter equivalent is FindAll

Scala:

val numbers = List(1,2,3,4,5)
val numbersSubset = numbers.filter(x => x > 2)
println(numbersSubset.length)

C#:

var numbers = new List<int> {1, 2, 3, 4, 5};
var numbersSubset = numbers.FindAll(x => x > 2);
Console.WriteLine(numbersSubset.Count);

Let’s combine two list operations in one statement

Scala – filter followed by forEach

val numbers = List(1,2,3,4,5)
numbers.filter(x => x > 2).foreach(println(_))

C# – FindAll followed by a ForEach

var numbers = new List<int> {1, 2, 3, 4, 5};
numbers.FindAll(x => x > 2).ForEach(Console.WriteLine);

Constructing a class with named parameters

Yes, I realize these classes are not equivalent, however, they are just for the examples to use below.
Scala:

case class Person(name: String)
val john = new Person(name = "John Smtih")

C#:

class Person { public string name { get; set; } }
var john = new Person { name = "John Smith" };

Construct a new list adding a new Person object to it at the same time

Scala:

case class Person(name: String)
val johns = List(new Person(name = "John Smith"))

C#:

class Person { public string name { get; set; } }
var johns = new List<Person> { new Person { name = "John Smith" } };

Converting a list of objects of one type into another

Now, just as an example of something a little more fun, let’s take the list of “Person” objects we created in the previous example and convert them into a list of “Employee” objects.
Scala – For Scala we will use foldLeft to accomplish this task

case class Employee(name: String)
val employees = johns.foldLeft(List[Employee]())((result,current) => Employee(current.name) :: result)
employees.foreach(x => println(x.name))

C# – For C# you could use Aggregate, but instead let’s try using the LINQ expression “Select”

class Employee { public string name { get; set; } }
var employees = johns.Select(person => new Employee { name = person.name }).ToList();
employees.ForEach(x => Console.WriteLine(x.name));

That’s all for now. As a side note I highly recommend getting familiar with LINQ, it is very useful. The site 101 LINQ Samples in C# is a great place to get started.

July 5, 2012

C# – Logging Exceptions with Elmah in an MVC 4 Web API Application

Filed under: .NET,c#,MVC — Brian @ 11:08 pm
Tags: , ,

Recently, after having been away from the .NET world for several years, I have started on a new project where we are building REST APIs using MVC 4. One of the first things we wanted to find out how to do was have all exceptions written to a database, as well as have a web interface from which to view exception details. Fortunately there is a very nice open source project available called Elmah.

When I created my first MVC 4 Web API application using Elmah, I was very rusty with .NET so getting Elmah setup and working took me a little longer than expected. Part of the reason was because the setup required piecing together information from multiple sources, since things have changed a bit in MVC 4. So here is a very simple quick start tutortial to help you get up and running with Elmah. If you would like to learn more about Elmah, the following article gives a good overview: Logging Error Details with ELMAH (C#).

NOTE: If you are using NuGet, you can use the Elmah.MVC NuGet Package to install Elmah, which works great. However, if you are not using NuGet, the following instructions will walk you through the setup.

1. Create a new MVC 4 Web API Project

The following article is useful, if you have never created an MVC 4 Web API application before:
ASP.NET WebAPI: Getting Started with MVC4 and WebAPI

2. Download the latest version of Elmah

You can obtain the latest version of Elmah here. At the time of this post, I am using version 1.2 Service Pack 2.

Unpack the zip file ELMAH-1.2-sp2-bin-x64.zip and add the following dll to your project as a reference:
…\ELMAH-1.2-sp2-bin-x64\bin\net-2.0\Release\Elmah.dll

3. Setup Elmah in your Web.config file

To the configSections part of your Web.config, add an Elmah sectionGroup:

<configSections>
<sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
…
</configSections>

To log to a Microsoft SQL Server database (In our case we are using SQL Server 2005), add a connection string for Elmah filling in your database server name and the name of your exceptions database:

<connectionStrings>
        <add name="elmah-sql" connectionString="Data Source=MY_DB_HOST;Initial Catalog=MY_EXCEPTIONS_DB_NAME;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;" providerName="System.Data.SqlClient" />
  </connectionStrings>

Inside the configuration tag add an Elmah section setting the “applicationName” to be some unique string to identify your application:

<configuration>
    <elmah>
        <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="elmah-sql" applicationName="SETMYAPPLICATIONNAME"></errorLog>
        <security allowRemoteAccess="1" />
    </elmah>
    ...
</configuration>

Inside the system.web tag add:

<system.web>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </httpModules>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    ...
</system.web>

In the system.webServer tag add:

<system.webServer>
    <handlers>
      <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
    </modules>
    ...
</system.webServer>

4. Run the Elmah SQL Server database setup script on your Exceptions database

You need to apply the script that creates the Elmah table where exception information will be logged, as well as adds the stored procedure required by the web interface to read exception messages. The script is located in the zip file you downloaded, located at:
…\ELMAH-1.2-sp2-bin-x64\db\SQLServer.sql.

5. Create a custom ExceptionFilterAttribute class

Even though, up to this point we have completed all the required setup for Elmah, it will still not work in our MVC 4 Web API project. To log exceptions to Elmah you must create a custom ExceptionFilterAttribute class and use that to call Elmah when an exception occurs.

public class ElmahErrorAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
{
    public override void OnException(
         System.Web.Http.Filters.HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Exception != null)
            Elmah.ErrorSignal.FromCurrentContext().Raise(actionExecutedContext.Exception);
        base.OnException(actionExecutedContext);
    }
}

6. Register the custom ExceptionFilterAttribute class in your global.ascx.cs file

Finally, you must register your custom ExceptionFilterAttribute class on startup.

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(
        new ElmahErrorAttribute()
    );
    ….
}

That’s it! Now you should be able to run your web app and go to the page:

http://localhost:SOMEPORT/elmah.axd

June 19, 2012

C# – Supporting text/plain in an MVC 4 Web API application

Filed under: .NET,c#,MVC — Brian @ 10:50 pm
Tags: , , , , , ,

If you have just created a brand new MVC 4 Web API application and when posting data with content-type: text/plain to a method in your ApiController that takes a string, you notice that the value in the string is null, this is caused by your application not having support for the media type “text/plain”.

No worries, after some digging I figured out how to get this to work.

Create the following custom MediaTypeFormatter in your application

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace WebApiMvc4Example
{
    public class TextMediaTypeFormatter : MediaTypeFormatter
    {
        public TextMediaTypeFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/javascript"));
        }

        public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            var taskCompletionSource = new TaskCompletionSource<object>();
            try
            {
                var memoryStream = new MemoryStream();
                readStream.CopyTo(memoryStream);
                var s = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                taskCompletionSource.SetResult(s);
            }
            catch (Exception e)
            {
                taskCompletionSource.SetException(e);
            }
            return taskCompletionSource.Task;
        }

        public override bool CanReadType(Type type)
        {
            return type == typeof(string);
        }

        public override bool CanWriteType(Type type)
        {
            return false;
        }
    }
}

Add the custom MediaTypeFormatter into the Application_Start method in your Global.asax.cs file

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace WebApiMvc4Example
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Add a reference here to the new MediaTypeFormatter that adds text/plain support
            GlobalConfiguration.Configuration.Formatters.Insert(0, new TextMediaTypeFormatter());
        }
    }
}

Now add the “FromBody” tag to your POST method that takes in a string in your ApiController

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Http;

namespace WebApiMvc4Example.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/values
        // Add the "FromBody" tag
        public void Post([FromBody] string value)
        {
            System.Diagnostics.Debug.WriteLine("******Woohoo: " + value);
        }

        // PUT api/values
        public void Put(string value)
        {
        }

        // DELETE api/values/5
        public void Delete(int id)
        {
        }
    }
}

That is all. So now if you start up your application:
POST to http://localhost:MYPORT/api/values with headers “content-type: text/plain” and with body “SOMETEXT” it will work just fine now.

I hope this helps!

May 27, 2012

C# – Bulk copying data into MS SQL Server with DataTables

Filed under: c#,SQL Server 2005 — Brian @ 9:45 pm
Tags: , , ,

Recently, we were asked to start pulling data daily from a number of sources (e.g., several REST APIs), aggregating the data, and saving it to a database to be used for generating reports. As usual we want to make sure the application is easy to test (We need to make sure those stats are correct!), but we also need to ensure it performs well because we will be adding possibly hundreds of thousands of rows daily to a number of different tables every time this job runs. We were worried that the bottleneck in this application would be in running all those insert statements against our MS SQL Server database. However, we were in luck, .NET has a handy feature called DataTables.

A DataTable is basically an in memory representation of an MS SQL Server table. DataTables allow you to create the table in memory, add rows to it, edit values in specific columns of a row, etc, until all the data is exactly what you want. Once the DataTable is ready, it is just a simple SqlBulkCopy statement to insert all the data at once. So rather than hundreds of thousands of insert statements, it is just one bulk copy, and rather than taking minutes or longer to run, it just takes seconds to dump all the data into MS SQL Server. Also, because the data is all in memory, it makes it very easy to test all of our stats. We simply pass in the data we would receive and assert on the values in the DataTables. That’s all!

The following is a simple example where we are saving daily sales figures for each sales person.

Create the table

CREATE TABLE [dbo].[DailySalesStats](
	[Date] [smalldatetime] NOT NULL,
	[SalesPersonId] [int] NOT NULL,
	[TotalSales] [int] NOT NULL,
 CONSTRAINT [PK_DailySalesStats] PRIMARY KEY CLUSTERED 
(
	[Date] ASC,
	[SalesPersonId] ASC
)) ON [PRIMARY]

Example of writing sales stats to the dbo.DailySalesStats table using a DataTable and SqlBulkCopy

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataTableExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a datatable with three columns:
            DataTable dailySalesStats = new DataTable("DailySalesStats");

            // Create Column 1: Date
            DataColumn dateColumn = new DataColumn();
            dateColumn.DataType = Type.GetType("System.DateTime");
            dateColumn.ColumnName = "Date";

            // Create Column 2: SalesPersonId
            DataColumn salesPersonIdColumn = new DataColumn();
            salesPersonIdColumn.DataType = Type.GetType("System.Int32");
            salesPersonIdColumn.ColumnName = "SalesPersonId";

            // Create Column 3: TotalSales
            DataColumn totalSalesColumn = new DataColumn();
            totalSalesColumn.DataType = Type.GetType("System.Int32");
            totalSalesColumn.ColumnName = "TotalSales";

            // Add the columns to the dailySalesStats DataTable
            dailySalesStats.Columns.Add(dateColumn);
            dailySalesStats.Columns.Add(salesPersonIdColumn);
            dailySalesStats.Columns.Add(totalSalesColumn);

            // Let's populate the datatable with our stats, which could come from REST APIs, etc.
            // You can add as many rows as you want here!

            // Create a new row
            DataRow dailySalesStatsRow = dailySalesStats.NewRow();
            dailySalesStatsRow["Date"] = DateTime.Now.ToString("yyyy-MM-dd");
            dailySalesStatsRow["SalesPersonId"] = 1;
            dailySalesStatsRow["TotalSales"] = 2;

            // Add the row to the dailySalesStats DataTable
            dailySalesStats.Rows.Add(dailySalesStatsRow);

            // Copy the DataTable to SQL Server
            using(SqlConnection dbConnection = new SqlConnection("Data Source=dbhost;Initial Catalog=dbname;Integrated Security=SSPI;Connection Timeout=60;Min Pool Size=2;Max Pool Size=20;"))
            {
                dbConnection.Open();
                using (SqlBulkCopy s = new SqlBulkCopy(dbConnection))
                {
                    s.DestinationTableName = dailySalesStats.TableName;
                    foreach (var column in dailySalesStats.Columns)
                        s.ColumnMappings.Add(column.ToString(), column.ToString());
                    s.WriteToServer(dailySalesStats);
                }
            }

            // That's it, we are done!
        }
    }
}

The Results

select * from dbo.DailySalesStats

Date                SalesPersonId  TotalSales
2012-05-27 00:00:00	   1	        2

Overall I have been really happy with the DataTable and SqlBulkCopy features in .NET. It is a fast and easy way to load a lot of data into a MS SQL Server database and is a good fit for our statistics gathering job!

September 19, 2009

Validating Xml with a Schema in an NUnit Test (.NET 3.5/C#)

Filed under: .NET,c# — Brian @ 2:19 pm
Tags: , ,

Recently we decided to make some updates to our web app. The front end website is in ASP.NET/C#, while the back end is all in Java and using REST APIs as the interface. One pair of programmers was assigned the task of updating the web app and the other pair was assigned the task of writing the new REST service used by the web app. To ensure that both sides would be able to talk to each other when completed, we wrote an XML Schema to act as our contract and placed it in an SVN external available to both projects.

In our functional test for the REST client side code (.NET/C#) we used a stub from which we could grab the XML being sent. To test that this XML conformed to the schema (contract) we agreed to, we created the following helper class:

Xml Validator Helper Class

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace Test.Helper
{
	public class XmlValidatorTestHelper
	{
		private bool isValidXml = true;
		private string validationError = "";

		public String ValidationError
		{
			get { return "Validation Error: " + validationError; }
			set { validationError = value; }
		}

		public bool IsValidXml
		{
			get { return isValidXml; }
		}

		/// 
		/// Validate an xml document against an xml schema.
		/// 
		public void ValidXmlDoc(XmlDocument xmlDocument, XmlSchema xmlSchema)
		{
			validateParameters(xmlDocument, xmlSchema);
			XmlReader xmlReader = createXmlReader(xmlDocument, xmlSchema);
					
			try
			{
				// validate        
				using (xmlReader)
				{
					while (xmlReader.Read())
					{}
				}
				isValidXml = true;
			}
			catch (Exception ex)
			{
				ValidationError = ex.Message;
				isValidXml = false;
			}
		}

		private static void validateParameters(XmlDocument xmlDocument, XmlSchema xmlSchema)
		{
			if (xmlDocument == null)
			{
				new ArgumentNullException("ValidXmlDoc() - Argument NULL: XmlDocument");
			}
			if (xmlSchema == null)
			{
				new ArgumentNullException("ValidXmlDoc() - Argument NULL: XmlSchema");
			}
		}

		private static XmlReader createXmlReader(XmlDocument xmlDocument, XmlSchema xmlSchema)
		{
			StringReader xmlStringReader = convertXmlDocumentToStringReader(xmlDocument);
			XmlReaderSettings xmlReaderSettings = new XmlReaderSettings 
                        { 
                               ValidationType = ValidationType.Schema 
                        };
			xmlReaderSettings.Schemas.Add(xmlSchema);
			return XmlReader.Create(xmlStringReader, xmlReaderSettings);
		}

		private static StringReader convertXmlDocumentToStringReader(XmlDocument xmlDocument)
		{
			StringWriter sw = new StringWriter();
			xmlDocument.WriteTo(new XmlTextWriter(sw));
			return new StringReader(sw.ToString());
		}
	}
}

Surfacing Validation Errors in your Test

Finding out that the XML our REST client produces matches the contract is great, however when it fails it would save a lot of time if the test just showed WHY validation failed. No problem, in our validation helper class we added a property to allow us to retrieve the validation error message in the event the test failed and output that as the failure message for the test:

XmlValidatorTestHelper xmlSchemaValidator = new XmlValidatorTestHelper();
XmlSchema myXmlSchema = XmlSchema.Read(...);
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument .Load(...);
xmlSchemaValidator.ValidXmlDoc(myXmlDocument, myXmlSchema);

Assert.IsTrue(xmlSchemaValidator.IsValidXml, "XML does not match Schema: " + xmlSchemaValidator.ValidationError);

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 26 other followers