Friday, September 12, 2008

Query string utility

Today i've writtern simple but useful querystring utility.

It contstruct link with querystring like StringBuilder.

Here is it code and unit test which describes how to use it.

QueryStringBuilder:

///
/// Build links with querystring.
///

public class QueryStringBuilder
{
private readonly Dictionary queryStringParameters = new Dictionary();
private readonly string targetPageLink;

private QueryStringBuilder()
{
}

///
/// Constructor
///

public QueryStringBuilder(string targetPageLink)
{
this.targetPageLink = targetPageLink;
}

///
/// Add parameter to querystring.
///

public void AddParameter(string parameterName, string parameterValue)
{
if (queryStringParameters.ContainsKey(parameterName))
{
queryStringParameters[parameterName] = parameterValue;
}
else
{
queryStringParameters.Add(parameterName, parameterValue);
}
}

///
/// Gets target page link with querystring.
///

/// Targe page link with querystring.
public override string ToString()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(targetPageLink);

int parametersCounter = 1;
foreach (var pair in queryStringParameters)
{
if (parametersCounter == 1)
{
stringBuilder.AppendFormat("?{0}={1}", pair.Key, pair.Value);
}
else
{
stringBuilder.AppendFormat("&{0}={1}", pair.Key, pair.Value);
}
parametersCounter++;
}

return stringBuilder.ToString();
}
}


QueryStringBuilderTest:

[Fact]
public void BuildLinkWithQueryString()
{
var queryStringBuilder = new QueryStringBuilder("http://localhost/");
queryStringBuilder.AddParameter("ProductId", "111");
queryStringBuilder.AddParameter("CustomerId", "23");
Assert.Equal("http://localhost/?ProductId=111&CustomerId=23", queryStringBuilder.ToString());
queryStringBuilder.AddParameter("ProductId", "131");
Assert.Equal("http://localhost/?ProductId=131&CustomerId=23", queryStringBuilder.ToString());
}

Thursday, September 11, 2008

ADO.NET entity cascade delete issue

To enable cascade delete action between two entities. You should first of all add cascade delete action in your DB and then update model .edmx file. Often that all you should do. But sometimes ado.net entity failed to update model correctly and you should fix it сsdl by hand.

To fix it your should open your model with XML editor and find your association and add something like this





Friday, September 5, 2008

SQL Server Diagram authorization problem.

Very often people who works with SQL Server database get the following error when they trying to open database diagrams after restoring database from back up:

Database diagram support objects cannot be installed because this database
does not have a valid owner. To continue, first use the Files page of the
Database Properties dialog box or the ALTER AUTHORIZATION statement to set
the database owner to a valid login, then add the database diagram support
objects.

To fix this problem you should execute this T-SQL script:

EXEC sp_dbcmptlevel 'yourDB', '90';
go
ALTER AUTHORIZATION ON DATABASE::yourDB TO "yourLogin"
go
use [yourDB]
go
EXECUTE AS USER = N'dbo' REVERT
go

Enum in ADO.NET Entity Framework

There is no direct support for enum type in ado.net entity framework. But it is rather easy to solve this problem.

All u need to do is to create int field in database and in entity class and make its getter and setter private and then add public property to your partial class, which type is enum.

Here is an example:

public MyEnum MyEnumProperty
{
get { return (MyEnum) InnerEnumProperty; }
set { InnerEnumProperty = (int) value; }
}

Simple database fixture.

Using .NET TransactionScope it is easy to make a clean up after each test which do something with database.

Here is a sample of testfixture for xUnit.
      
public class DatabaseFixture: IDisposable
{
private readonly TransactionScope transactionScope;
protected DatabaseFixture()
{
transactionScope = new TransactionScope();
}
public void Dispose()
{
transactionScope.Dispose();
}
}

Saturday, August 30, 2008

WCF Tip: Enlarge WCF message size.

To enlarge WCF message size for silverlight communication u should add following binding settings:





maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />





And then add bindingConfiguration="LargeBuffer" to your endpoint properties.

Tuesday, August 19, 2008

Asynchronous method unit testing.

In Silverlight 2.0 all WCF communication is asynchronus. So if u wanna test some code which uses WCF u should work with asynchronus results. First of all I think that it is impossible to test them. But after little work around if found pretty solution.

There is built-in .NET class called AutoResetEvent (u can find in Silverlight too) which u can as signal that your async call is completed.

 

[TestClass]
public class MyTests
{
private AutoResetEvent testTrigger;
private int asyncResult;

[TestMethod]
public void CheckAsyncResult()
{
testTrigger = new AutoResetEvent(false);

....

myTestedObject.Complete += myTestedObject_Complete;

myTestedObject.DoAsync();

testTrigger.WaitOne();

Assert.AreEqual(4, asyncResult);
}

private void myTestedObject_Complete_Complete(object sender, EventArgs e)
{
asyncResult = (MyTestedObject) sender.Result;

testTrigger.Set();
}
}