Archive for .net

Microsoft and their huge problem in the clouds…

// October 31st, 2009 // 4 Comments » // .net, cloud computing, microsoft, open source

I just recently read an article from Krishnan Subramanian, which I believe is very interesting: Microsoft’s Huge Cloud Problem.

I agree with most of the article’s comments. They have to be taken with a grain of salt, since most of is speculation. Very smart speculation, but speculation none the less. But What I do disagree completely, is the following line:

“..cloud is an evolution from the web and .NET was never a platform of choice in the web…”

I agree that the cloud is an evolution of the web, but the article talks about choice, who is it referring to? Is it the open source community? Or is the enterprise community? or is it both?

Obviously as an Open Source advocate, .Net or even Mono would not be your web platform of choice. You usually go to either PHP (which is the leader in the Open Source community) Ruby or Python (just to name a few, I know there are a lot more).

But in the enterprise world, .Net is very much relevant, and in most of the cases it is the platform of choice. I know that this is a huge market and the competition is strong, but to completely dismiss Asp.Net as not a platform of choice is far from the truth.

Asp.Net and .Net are very much relevant right now, and it will stay that way for a long time. Whether Azure succeeds or not.

It is a mistake to think that everything will be in the cloud. What will prevail are hybrid environments. That is why I think Microsoft will not only survive this (even though is going to be a really difficult climb), but it will remain relevant.

Google’s view of *EVERYTHING* in the Cloud is not very down to earth (hence the name, everything in the clouds). And in my opinion, it will never get there. A lot of things are going to be done in the cloud, and probably the majority, but not all. We are creatures of choices, and we will keep our options open.

Now with the open source movement, Microsoft has done a lot. And I actually think we should thank Miguel de Icaza and his team for this. He might be called a traitor by some, but I think he is the biggest Trojan Horse of all. He has been pushing Microsoft to open source (with the help of so many).

But let’s think about Mono for a minute. Microsoft already released the source code for .Net in a very closed license, which I see as a glass box (look but don’t touch). It is getting there, to that openness that the article is talking about. They know they have to do it. But they don’t know how.

Now, Mono is a very good example. They have been reproducing the signatures and interfaces to use .Net on Linux and it works like a charm. Also they have been adding their own mix.

Microsoft will end up releasing .Net as an Open Source project, it will not be soon though. They already have their own license for that. With what Mono has done, when Microsoft plans to release, the integration with Mono will make it easier to hit the market.

The article is right about one important thing, in order to compete in the clouds, they have to kill Windows as an Desktop OS. But I think it will prevail as Windows Azure. That is why the word “Windows” appears in there.

Just one more thing before I close this rant. I think the mistake that Netscape did with Mozilla, is a learning experience that can be applied anywhere. When Netscape decided to build their browser from Scratch instead of fixing their bloated browser at the time. They lost too much time, and they lost the browser wars. They should have fixed their browser, not start a new one, which ended up with the same problems. It eventually got fixed when the community did the right thing and fixed it with Firefox, but they didn’t not start from scratch, they fixed Mozilla.

Microsoft is the browser and we (the community) are Netscape. Are we going to kill Microsoft so that Apple or Google takes its place? And then end up with the same problems all over?

I wouldn’t really want Apple in Microsoft’s shoes. I can see what they can do with their App Store. They have so much to learn. It would be like going back to the 90’s. We already went this route with Microsoft so many times, and now, Microsoft is learning.

How about Google? I wouldn’t want Google either. They are still too young, and we haven’t seen their evil yet, which scares me a lot. They not only have a lot of power in the internet, they hold most of our data, and they want *ALL* of it. Everybody has an evil side, and Google is not any different. We just haven’t seen it yet.

Microsoft is a known evil, let’s fix it. Why change it for a new one, when this evil has already been changed so much, and it is learning to live with the community?

Well enough of rants…I’m going back to work.

Important links:

Post to Twitter Tweet This Post

Easy .Net Transaction Management with Transaction Scope

// April 3rd, 2009 // No Comments » // .net, SQL Server, ado.net, software development

Transactions are a common technique to ensure consistency of the data when using database applicationsfor example with Sql Server. The System.Transactions namespace in the .Net framework simplifies Transaction Managements considerably.

This time we are going to talk about TransactionScope, which is part of the System.Transactions assembly. Using TransactionScope to manage transactions is fairly simple, yet powerful. Let’s look at an simple example using ADO:

TransactionScope tranScope = new TransactionScope();

SqlConnection connection = new SqlConnection(connectionSettigs);
SqlCommand cmd = new SqlCommand(query, connection);

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

transScope.Complete();

In the example we first initialized the TransactionScope object, then we initialized the connection (very important point, we will talk about it later), execute the command, etc. And then at the end we called the method Complete for the transaction scope object.

When the transaction scope object is initialized, the transaction is effectively created and all commands after that will be protected by the transaction. After the commands are executed, then we decide to either commit the transaction or rollback. Now, for the transaction scope, the method Complete effectively commits the transaction to the database, when Dispose executes the rollback.

For some developers, is quite confusing since most developers are used to:

// Handling the transactions explicitly.
SqlConnection connection = new SqlConnection(connectionSettigs);
SqlTransaction trans = connection.BeginTransaction();

SqlCommand cmd = new SqlCommand(query, connection);
cmd.Transaction = trans;

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

connection.Commit();

Not only is the TransactionScope idiom different but also the Commit and Rollback method names are also different. The reason why the names are also different is because 1) The TransactionScope object is not a transaction per se, it is more of a transaction handler and 2) Because TransactionScope follows the Dispose pattern (just like SqlConnection). Following the Dispose pattern to implement the TransactionScope makes it easy to do this:

// Handling the transactions implicitly.
using(TransactionScope tranScope = new TransactionScope()) {

	SqlConnection connection = new SqlConnection(connectionSettigs);
	SqlCommand cmd = new SqlCommand(query, connection);

	connection.Open();
	int id = cmd.ExecuteScalar();
	connection.Close();

	// If success commit
	if(id > 0) {
		transScope.Complete();
	}
}

The example displayed above shows how to handle implicit transactions using the ‘using‘ keyword. When applying the ‘using‘ idiom on an object which implements the Dispose pattern, the ‘Dispose‘ method of such object will be called when the using block ends.

What the transaction scope is doing is registering the transaction in the connection contained in the transaction scope block. Making it easy to handle transactions in .Net code.

In the case of the example, if the transaction scope is not committed before the block ends, it is effectively rolled back automatically, since the using idiom calls the Dispose method of the selected object. This makes the use of Transactions extremely easy to read and maintain.

Now what happens if there is an exception inside of the TransactionScope block ? Well, the object’s Dispose method is called, which automatically rolls back the transaction.

Now it is very important, while using TransactionScope, that the connection is opened inside of the transaction scope block, otherwise the transaction won’t be registered in the connection.

SqlConnection connection = new SqlConnection(connectionSettigs);

// This will not work. The transaction will not be registered in the connection,
// and it will deadlock your application.
connection.Open();

// Handling the transactions implicitly.
using(TransactionScope tranScope = new TransactionScope()) {

	SqlCommand cmd = new SqlCommand(query, connection);

	int id = cmd.ExecuteScalar();

	// If success commit
	if(id > 0) {
		transScope.Complete();
	}
}

connection.Close();

The above example will not work. What happens is that when the Connection is opened inside the TransactionScope block, the transaction is registered in the connection which means that any command for that connection will be in the transaction until the connection is closed.

Be very careful to initialize the connection inside the transaction scope block, otherwise the transaction won’t register itself in the connection, and your data won’t be protected by the transaction. In some cases, any command inside of the transaction scope block, for which the connection is opened before the transaction block will result in a dead lock. The code above serves as an example of this case.

More information:
MSDN:Transaction Scope Class.

MSDN:Implementing an Implicit Transaction using Transaction Scope

Post to Twitter Tweet This Post

Unit Testing Data-Driven WCF Services

// March 27th, 2009 // No Comments » // .net, Unit Tests, design patterns, services, wcf

Any data driven module, class or method needs to be tested. At this moment anything that is data-drive (which would actually cover almost anything) needs to be verified according to specs to ensure correctness. In this post we will talk about creating tests for Data-Driven WCF Services. For simplicity and ease of use, we are going to use the Enterprise Library Data Application Block to talk to the database.

Before we get deeper into the topic at hand, let’s talk about the unit test environment that I use and recommend for testing data-driven applications.

Testing Context
The basic objective of a unit test is to verify correctness of a an application. Testing every unit in a layer ensures the correctness of the behavior of such units, specially when being used by other units in the application. This way all units can be reused with confidence.

Now in order to maintain confidence in the Unit being tested, we need to ensure that the tests are reliable and that providence confidence in the correctness of the unit under test. When a test sometimes pass and sometimes fails is called an Erratic Test, and in order to maintain confidence we need to avoid such behavior.

Unit Tests, as explained before, focus on testing the a single unit in a system, unlike integration testing. So it is a best practice to have one database instance per developer or tester. Having two developers running the test fixtures at the same time on the same database create an erratic test behavior, which is not acceptable.

Data-Driven Tests
The pattern we are going to follow in this post is a simple form of the Data-Driven Test Pattern, which is described as:

“We store all the information needed for each test in a data file and write an interpreter that reads the file and executes the tests”.

This pattern is mostly used to avoid code duplication, especially when several test cases just change on different data conditions. This way one test can be executed with several test scripts, instead of writing one test for every data condition.

We’ll use a simple form of the pattern, using Sql syntax for scripts instead of Xml data files. I find it easier to just create an Sql script, execute it and continue with the testing.

Each test must have a context setup. There are several patterns that specify that one test method sets up the data context for the others; for example, one test method tests the insertion of data, while the next test method tests the retrieval of the inserted data. I don’t recommend this setting, this leads to Erratic Tests. Every unit test must be independent of each other. This means that every test method must setup its own environment and it must also clean it up to the initial state.

Unit Testing WCF Services
Unit Testing WCF Services is extremely easy. There are two ways to do this:

1) Include the WCF Library as a reference.

2) Load up the WCF Service host and call the service from the test fixture.

Since we are mostly doing unit testing and not integration testing, loading the WCF Library as a reference works fine. For integration purposes I would write one test fixture for each endpoint in the configuration, but this is out of the scope of this post.

Test Code

So we have two service methods: 1) ProjectService.Insert and 2) ProjectService.GetIdByName. This is the interface of such methods:

namespace WCFTesting
{
    using System.ServiceModel;

    /// <summary>
    /// Project Service Contract.
    /// </summary>
    [ServiceContract]
    public interface IProjectService
    {
        /// <summary>
        /// Inserts a new Project.
        /// </summary>
        /// <param name="id">Project Identifier.</param>
        /// <param name="name">Project Name</param>
        /// <param name="description">Project Description</param>
        /// <returns>True if successful, false otherwise.</returns>
        [OperationContract]
        bool Insert(int id, string name, string description);

        /// <summary>
        /// Returns the id of the project that goes by the selected name.
        /// </summary>
        /// <param name="name">Project Name</param>
        /// <returns><c>int</c></returns>
        [OperationContract]
        int GetIdByName(string name);
    }
}

The service method implementation is shown below:

/// <summary>
/// Inserts a new Project in database.
/// </summary>
/// <param name="id">Project Identifier.</param>
/// <param name="name">Project Name</param>
/// <param name="description">Project Description</param>
/// <returns>True if successful, false otherwise.</returns>
public bool Insert(int id, string name, string description)
{
	Database db = DatabaseFactory.CreateDatabase();

	DbCommand command = db.GetStoredProcCommand("[dbo].[InsertProject]");
	db.AddInParameter(command, "id", DbType.Int32, id);
	db.AddInParameter(command, "name", DbType.String, name);
	db.AddInParameter(command, "description", DbType.String, description);
	db.AddOutParameter(command, "success", DbType.Boolean, 1);

	db.ExecuteNonQuery(command);

	bool success = (bool) db.GetParameterValue(command, "success");

	return success;
}

/// <summary>
/// Returns the id of the project that goes by the selected name.
/// </summary>
/// <param name="name">Project Name</param>
/// <returns><c>int</c></returns>
public int GetIdByName(string name)
{
	Database db = DatabaseFactory.CreateDatabase();

	DbCommand command = db.GetStoredProcCommand("[dbo].[GetProjectIdByProjectName]");
	db.AddInParameter(command, "Name", DbType.String, name);

	int id = -1;
	using(IDataReader reader = db.ExecuteReader(command)) {
		if(reader.Read()) {
			id = reader.GetInt32(0);
		}
	}

	return id;
}

Note: All code can be found in the attachment at the end of the post.

As you can see the methods are really simple. We are using a simple call to a stored procedure, getting the results and sending them back to the client.

Now let’s look at the actual unit test fixture:

/// <summary>
/// Test the Service method Insert
/// </summary>
[TestMethod]
public void Insert()
{
	using(TransactionScope tranScope = new TransactionScope()) {
		int id = 1;
		string name = "Project 1";
		string description = "Description of Project 1";
		Assert.IsTrue(Service.Insert(id, name, description));
		Assert.IsTrue(ProjectExists(name, description));

		id = 2;
		name = "Project 2";
		description = "Description of Project 2";
		Assert.IsTrue(Service.Insert(id, name, description));
		Assert.IsTrue(ProjectExists(name, description));
	}
}

/// <summary>
/// Test the Service method GetIdByName
/// </summary>
[TestMethod]
public void GetIdByName()
{
	using(TransactionScope tranScope = new TransactionScope()) {
		// Insert the Test Data.
		SqlHelper.ExecuteSqlScript(Db, @"......UnitTestsScriptsInsertTestProjects.sql");

		// Test Service Method.
		int id = -1;
		string name = "Kook";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(1, id);

		name = "Samii";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(2, id);

		name = "CPW";
		id = Service.GetIdByName(name);
		Assert.AreEqual<int>(3, id);
	}
}

First we have the insert method, which doesn’t require any existing data, so no script is executed before the service method. We do however wrap everything into a TransactionScope, which ensures that whatever the test does, after the TransactionScope block is out of the scope it will rollback all the changes. I will leave the discussion on Transactions for a later post.

Now, the second method is more interesting. The GetIdByName test method actually needs a setup. We could have created a dependency on the Insert method so that the first test method prepares the data that the GetIdByName method will require to test. But we want to avoid Erratic Test behavior, so we created them completely independent.

The GetIdByName is also wrapped in the TransactionScope so that we can ensure that after the test is done, the data is rolled back to its initial state.

The script (InsertTestProjects.sql) contains three sample projects which the service method will retrieve and we will verify the returned data.

As you can see testing Data-Driven Services is very simple. If you pay close attention to the project you will notice that this way of doing this doesn’t only apply to WCF Services, any .Net assembly can be tested this way.

Hope this helps you on your quest for simplicity and correctness!

Download Test Solution

Test Solution Requirements

  • Visual Studio 2008
  • Enterprise Library 4.x
  • .Net 3.5
  • SQL Server 2005

The Database folder contains the table and stored procedures needed by the code. Just modify the ExecuteScripts.bat to set the database authentication info. Also modify the App.config in the UnitTests project in order to run accordingly.

(Disclaimer: This project has been stripped down so that it is easy enough to understand. This second method, iin order to follow Data-Driven Test, should get the expected data from an Xml file, insert it into the database, and compare accordingly. In this sample we have the data hard coded for simplicity. Building an interpreter is our of the scope of this post.)

* References:
- Meszaros, Gerard. “xUnit Test Patterns: Refactoring Test Code”
2007, Pearson Education.
Amazon

Post to Twitter Tweet This Post

Alchemy….

// November 20th, 2008 // No Comments » // .net, RIA, c++

I have not really been a follower of Macromedia (now part of Adobe) or its products; specially Flash, but I just found out about one research project that does capture my attention: Alchemy.

From the press release:

Alchemy is a research project that allows users to compile C and C++ code that is targeted to run on the open source ActionScript Virtual Machine (AVM2). The purpose of this preview is to assess the level of community interest in reusing existing C and C++ libraries in Web applications that run on Adobe® Flash® Player and Adobe AIR®.

This project promises that C/C++ libraries that have no OS dependencies can be run on the Adobe AIR platform. This is HUGE. Just to think that at least 90% of my libraries can be used in Web Applications is awesome.

A not so direct competitor, Microsoft’s Silverlight, does provide a way to reuse C++ libraries via C++/CLI, and it works like a charm; I happend to have a lot of C++ libraries  which have valuable code. Lately I have been reusing those libraries in Web Applications via C++/CLI and WebServices. It works, and it works fine.

I have not worked with Silverlight 2.0 yet; I have played around with it, but I have not done anything that is worth mentioning. In my tests, I see Ican also reuse those C++ libraries with C++/CLI and Webservices. The problem is that this approach will only work on Windows, since C++/CLI is not supported by Mono.

Another approach would be to make the WebServices in native C++, and call it from anywhere: Silverlight, Javascript and .Net. This would work anywhere.

This doesn’t take away the credit for Alchemy, which makes the C++ libraries natively accessible to Adobe AIR applications, in a way this is very close to what C++/CLI does.

The purpose of the Alchemy project is :

Alchemy is primarily intended to be used with C/C++ libraries that have few operating system dependencies. Ideally suited for computation-intensive use cases, such as audio/video transcoding, data manipulation, XML parsing, cryptographic functions or physics simulation, performance can be considerably faster than ActionScript 3.0 and anywhere from 2-10x slower than native C/C++ code. Alchemy is not intended for general development of SWF applications using C/C++.

Comparing it with C++/CLI, I do see an advatage from using C++/CLI to reuse C++ libraries in RIA applications over the Alchemy project: I can reuse .Net libraries with C++ native code, which gives me the best of both worlds.

I did some research on Alchemy, and it seems it doesn’t allow Flex code to be embedded with C++ native code. My research was not very comprehensive to say the least, so I could be wrong. It could also be too soon to tell since this is still in early stages of research and development.

In conclusion, this project seems very interesting, and I’m going to keep an eye out for it.

Post to Twitter Tweet This Post

TechDays ‘08 in Orange County

// October 31st, 2008 // No Comments » // .net, events, microsoft

TechDays is comming to Orange county. It is going to be at the Orange County Hilton in Costa Mesa:

Hilton Orange County/Costa Mesa
3050 Bristol St
Costa Mesa, California, 92626

It is going to be three days (Nov 11-13) and they have two tracks:

1) Developer Track
2) IT Prov Track.

For more information go to: MSDN Events

See ya there!

Post to Twitter Tweet This Post