Software. Architect, design, develop. Enjoy!

Home


0 Comments

Designing Azure Tables. Autocomplete Scenario.

03.31.11 Posted in Software by Vladimir

Azure tables have some advantages over RDMS tables. But there are some challenges in dealing with these tables because of their NoSQL nature:

  • Table can have only two keys: Partition Key and Row Key
  • There are no “foreign keys”, which makes normalization a bad approach

Let’s take a look at two scenarios that addresses these challenges.

Here is a typical structure of the Customers table in RDMS:

image

The most typical approach to define the structure of the data in the RDMS is data-centric: first, the structure of the table is defined based on the data, second, the usage of the data (based on the application functionality) is analyzed and table is altered accordingly, usually in form of additional indexes.

With Azure tables the approach should be usage-centric: the functionality of the application and the way the data is used should define the structure of the table.

In our example with customer data, quick analysis  of the application shows that most of the time users will do a search for a customer by name when they fill out invoices. Application assist in this task with auto-complete functionality: after typing first 3 letters of the customer’s name, application will suggest a list of the customers:

image

Here is one of the possible ways to store Customers in the Azure table that addresses this usage scenario:

image

First three letters of the customer’s name are used as the Partition Key. Some unique identifier is used as the Row Key (this is Id field from the relational table). This structure provides fast access to the list of the customers by first 3 letters of their name.

This is not the “best & only” solution and it has it’s issues, but it demonstrates that designing Azure (or any NoSQL) tables requires a different approach comparing to the RDMS world.


0 Comments

NuGet. Add same package reference to multiple projects

03.21.11 Posted in Software by Vladimir

NuGet is a great addition to the .NET environment. It saves time – your most valuable resource.

Most of the time I use NuGet UI to add library reference via Project > Add Library Package Reference menu. But adding reference to the same package in multiple projects using UI is not the most time-saving approach.

My Bills and Cash Flow solution has 10 test projects. They have the same naming pattern: CashFlow.[ComponentName].Tests:

image

All of them use Moq and Should Assertion Library. It’s very easy to add reference to these two packages to all 10 projects using NuGet console

First, launch NuGet console via Visual Studio menu: Tools –> Library Package Manager –> Package Manager Console. Then use this command to add reference:

 get-project *.Tests | install-package Moq 

It may take some time depending on the number of projects.


0 Comments

Multiple models and explicit keyword

02.07.11 Posted in Software by Vladimir

For many years I’ve always had one entity class that represented some business object (e.g. Customer class as a representation of customer) and used it throughout all layers: at UI, business layer and DAL. Developing BizTalk application this year I’ve used the same approach. I’ve quickly realized that this approach does not work well from many perspectives: testability, separation of concerns and scalability (both execution and development). The approach that resolved these issues was to use multiple schemas with mappers. It worked great.

That’s when I’ve realized that the same approach should be used for my .NET applications: core entity with models specific to particular tasks (UI or data access) and mappers/converters. So in places where I’ve used single Customer class I have multiple models now:

Multiple Schemas Customer

Multiple models require multiple converters/mappers. There are multiple ways to implement these mappers. Automapper is a great tool for this purpose.

A minor issue I’ve had with these mappers is the implementation pattern: .NET has a few ways to handle converters: Convert class, To… methods, … I have considered the following patterns:

var model = new CustomerListModel(customer);
var model = Converter.ToCustomerListModel(customer);
var model = customer.ToListModel();

But finally I’ve decided to go with a cast implementation using explicit keyword:

var model = (CustomerListModel)customer;

It’s a matter of personal taste, of course, but this way looks … more elegant for me. It’s possible to use implicit cast, but I think the implementation losses some clarity in that case and may be slightly confusing.


0 Comments

Azure and ASP.NET MVC 3 with Razor

01.09.11 Posted in Software by Vladimir

Scott Hanselman has a very helpful post about deploying ASP.NET MVC 3 with Razor to a Windows Server without MVC installed. But what about WIndows Azure?

MVC 3 with Razor plays nicely with Windows Azure. Following are just two steps that helped me to launch my application on Azure:

Make sure that your MVC project explicitly references the following assemblies:

  • Microsoft.Web.Infrastructure
  • System.Web.Helpers
  • System.Web.Mvc
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor

In the properties for these assemblies set Copy Local to True.

image


0 Comments

Effective multitasking tip

12.19.10 Posted in Productivity by Vladimir

I’ve been working on multiple projects recently. I’ve been managing multiple work projects effectively for a long time. I’m talking about working on multiple projects outside of my job. I am pretty busy at my work. In addition I do an open source development project. I’ve started this blog. And I’m developing one more application that is very important for me.

My normal day goes like such: wake up early, do some development for one of my personal projects, work on a personal project during commute to work, do my job, commute home and work on one of my projects again and may be some coding after dinner. I love my personal projects a lot and it gives me enough enthusiasm to keep this schedule.

Juggling job and personal projects was challenging for me at first. And that’s where I’ve developed this process that I would like to share:

Every time I’m going to stop working on one project and work on another I record what I was doing and (even more important) what I have to do when I resume working on the same project.

E.g.: fixed 80% of the bug #347. Finish bug fixing and update unit tests.

Simple? Yes. But it makes switching from one project to another less painful and increases productivity dramatically. It helps to get into the interrupted project much faster.

I’ve also realized that the same trick can be used in the office as well: if my development activity is going to be interrupted by a meeting I write down: was doing this, start with this after the meeting.

Try it and maybe it will help you too. Or maybe not. It depends.


0 Comments

Unit test legacy code

12.10.10 Posted in Software by Vladimir

I use mocks and fake data a lot these days. Mocking is especially helpful for unit testing. I use Moq framework.

I am not going to talk about the need of unit testing. I think it’s not a topic for discussion anymore. But in order to use unit tests effectively application should be designed for testability. Design for testability is actually not that difficult. Application layers should interact via interfaces rather than concrete classes and should have a mechanism to "inject" a real or mock implementations.

Following this principle gains a lot of benefits and is very easy … when you begin developing a new application. Unfortunately sometimes you have to work with a legacy code that was not designed for testability. What should you do in this case? Throw unit testing away and do manual testing? Well, that’s a solution, but …

Last week I’ve developed a new feature for an existing application and was adding unit tests for it. I’ve quickly realized that because of the call to a legacy method I cannot follow my usual mocking routine: legacy code didn’t have interfaces and, even worse, the class is sealed. For a second I was considering to go without unit tests and hope that in the future everything will be fine. Then I’ve realized that there is a solutions I have refactored my code so that the call to a legacy method can be mocked.

Here is the original method where I have to make a call to a method from a legacy sealed class:

public string Upgrade(Customer customer)
{
    var accounts = legacyClass.CallLegacyMethod(customer);     

    var result = accounts != null ?
        ProcessAccounts(accounts) :
        HandleCustomerWithNoAccounts(customer);     

    return result;
}

After a quick refactoring the same method became unit test friendly:

private Customer customer;
private IEnumerable<Account> accounts;

public IEnumerable<Account> Accounts
{
	get
	{
		if (accounts == null)
			accounts = CallLegacyMethod(customer);
		return accounts;
	}
	set {	accounts = value; }
}

public string Upgrade(Customer customer)
{
	this.customer = customer;

	var result = Accounts != null ?
		ProcessAccounts(accounts) :
		HandleCustomerWithNoAccounts(customer);

	return result;
}

And here is the unit test were I can mock (or fake) the result of the call to that legacy method:

[TestMethod]
public void Upgrade_Mocked()
{
	var fakeAccounts = GenerateFakeAccounts();
	var customer = GenerateFakeCustomer();
	processor.Accounts = fakeAccounts;

	var actual = processor.Upgrade(customer);

	//Assert
}

Unit test unfriendly legacy code is not an excuse for a bad quality of your own code. Just remind yourself that although you cannot control others’ code (and others) you can always control your code (and yourself).


3 Comments

F# Interactive for C# developers

12.02.10 Posted in Software by Vladimir

From time to time I have a need to check what a particular method from some .NET framework class returns. It’s possible to put the call to a separate method, create and execute a simple unit test, and expect the result. There are some applications that provide “dynamic” C# console. It works too, but still it’s too many steps.

This is where Visual Studio F# Interactive comes handy. First, it’s a part of the Visual Studio and you can view it by going to View –> Other Windows –> F# Interactive. Second, you don’t have to know F# to use it.

Here is a simple example where I use F# Interactive to have a quick check of the behavior of the DateTime and DateTimeOffset classes:

F_Sharp_Interactive

Please, note that double ;; at the end of the line is critical.

This is a very simple example, but you’ve got the idea.


0 Comments

Convenience of dynamic

11.28.10 Posted in Software by Vladimir

C# dynamic type comes very handy a number of situations.

TryParse pattern.

I’ve never liked the out parameter modifier. I know, it’s personal, and you are welcome to disagree with me.

The TryParse pattern happens quite often. My preferred approach was wrapping both return values into an object. Or use KeyValuePair. But it’s not an elegant solution for my taste.

Here is how I do it with dynamic these days:

public dynamic TryParse(string value)
{
	dynamic result = null;

	try
	{
		result = new { Success = true, Value = int.Parse(value) };
	}
	catch
	{
		result = new { Success = false, Value = 0 };
	}

	return result;
}

public void Caller()
{
	var total = 0;
	var stringValue = "15";
	var parseResult = TryParse(stringValue);
	if (parseResult.Success)
	{
		total += parseResult.Value;
	}
	else
	{
		//Handle parse error.
	}
}

Returning collection of anonymous types

Another situation is when I would like to return a collection of anonymous types.

public IEnumerable<dynamic> GetUserLastNames()
{
	return users.
		Select(user => new {
			Id = user.Id,
			LastName = user.LastName,
			FirstName = user.FirstName
	});
}

public void BuildLastNamesModel()
{
	var model = GetUserLastNames().
		Select(user => new {
			Id = user.Id,
			Name = string.Concat(user.FirstName, " ", user.LastName)
	}).ToList();
}

There is one more situation where I use dynamic a lot these days: in MVC controller unit tests when I need to check the values of the JsonResult returned by an action.