Monday, January 25, 2010

S.O.L.I.D principles

S: The Single Responsibility Principle
O: The Open Closed Principle
L: The Liskov Substitution Principle
I: Interface Segregation Principle
D: The Dependency Inversion Principle

Five principles that made the OOP world is re-use and easily maintain.
All thing is found by Robert Martin (a.k.a. Uncle Bob). And today, i find a link that write very good about S.O.L.I.D principles. You can find this at here.

Friday, January 22, 2010

How to references mutilple databases in SQL Server 2005

Today, when working with SQL Server 2005, I have a issue. This situation as "I have a select statement that references to multiple tables that reside in 2 different database". After finding in internet and helping from my team leader, I found a solution as below;

+ Using Synonyms in SQL Server 2005 for taking references to other database (other database server)
USE "Database using references";
GO
IF OBJECT_ID('dbo.OrgUnit', 'SN') IS NOT NULL
DROP SYNONYM dbo.OrgUnit;
GO
CREATE SYNONYM dbo.OrgUnit FOR ["name of database referenced"].security.orgunit.OrgUnit;

select * from dbo.OrgUnit


Note: I you want to know about "name of database referenced", using as
select * from sys.servers


+ Adding:
USE master;
GO
EXEC sp_addlinkedserver
'"IP Address"',
N'SQL Server'
GO


+ And now you can select the table that reside in the other location as

USE "Database using references";
GO;
select * from dbo.OrgUnit


For example:

Assume we have [Client] database at local and [Security] database at the remote side

USE master;
GO
EXEC sp_addlinkedserver
'SECURITY_DATABASE_ADDRESS',
N'SQL Server'
GO

select * from sys.servers

USE Client;
GO
IF OBJECT_ID('dbo.OrgUnit', 'SN') IS NOT NULL
DROP SYNONYM dbo.OrgUnit;
GO
CREATE SYNONYM dbo.OrgUnit FOR [SECURITY_DATABASE_ADDRESS].security.orgunit.OrgUnit;

select * from dbo.OrgUnit

Tuesday, January 19, 2010

Thinking about Patterns in ASP.NET for developer

Today, when try to finding about pattern, I found the new series articles about patterns for developer. It's really attractive me for exploring pattern in ASP.NET.

This is a three parts: part 1, part 2 and part 3 at http://devx.com
And this is a some pattern that apply for ASP.NET website at http://www.developerfusion.com here and here

Maybe, It's not new (because publish in 2007) but as me this very useful.

Post, Redirect, Get Pattern in ASP.NET MVC


The ASP.NET MVC pattern tends to lead itself into a more simplified and "true" HTTP experience by re-introducing patterns that have been lost, or at least, not followed in many years. One such pattern is the Post, Redirect, Get (PRG) pattern in which it is "to help avoid duplicate form submissions and allow web applications to behave more intuitively with browser bookmarks and the reload button" (Wikipedia).

A normal ASP.NET Web Form Lifecycle has the following pattern

1. HTTP GET of "Create.aspx"
2. HTTP POST of "Create.aspx"
3. Validation Fails, "Create.aspx" is Re-Rendered
4. HTTP POST of "Create.aspx"
5. Item is created, "Create.aspx" is Re-Rendered with confirmation message

The major problems with this Postback pattern, is that hitting the Refresh button of your browser in steps 3 or 5 will re-post your submitted data. Step 5 is more of a problem as it could possibly re-submit that created information. Granted, there are steps that you can take to approach this problem, but this is how default ASP.NET Web Forms are treated.

Taking this same approach within ASP.NET MVC, can be achieved in the same manner by rendering a your "Create" view from your POST action. For example:

1. HTTP GET of "/products/create", "Create" view is rendered
2. HTTP POST to "/products/submit"
3. Validation Fails, "Create" view is rendered
4. HTTP POST to "/products/submit"
5. Item is created, "Confirm" view is rendered

As you'll notice, the same problems we had with ASP.NET Web Forms exists with ASP.NET MVC. The really nice option, is that ASP.NET MVC gives you a lot more "freedom" of how the workflow is processed. If we strictly follow the PRG pattern within ASP.NET MVC, it would look something like

1. HTTP GET of "/products/create", "Create" view is rendered
2. HTTP POST to "/products/submit"
3. Validation Fails, redirect to "/products/create", "Create" view is rendered
4. HTTP POST to "/products/submit"
5. Item is created, redirect to "/products/confirm", "Confirm" view is rendered

As you'll notice, where we previously could have had issues in step 3 or 5 before, we no longer have issues. If a user presses the Refresh button in either of those steps, they'll not get the lovely "Would you like to resubmit the form data" confirmation as featured below - instead, the page just reloads.






All detail information, please go ASP.NET MVC - Using Post, Redirect, Get Pattern

Monday, January 18, 2010

WCF Error - The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state

While working on a WCF service, I ran into the following error.

The communication object, System.ServiceModel.Channels.ServiceChannel,cannot be used for communication because it is in the Faulted state.

This occurred when called Close method on instance of my WCF service client object. For a moment it took me by surprise. The code has been working fine and I was doing the right thing of closing connection to my WCF service after I was done using it. When I looked at the stack trace, I realized that it got thrown from a finally block of a method where I was calling a method on my WCF object. And before that there was an exception thrown. After further investigation it turned out that my WCF service was not running. So when I called method on the object, I got EndpointNotFoundException exception thrown when method was called. And in the same exception handling block I tried to call Close on client object. Since the client was already in bad state so next exception was thrown. So as a best practice, before you call Close method on your service object, check if its in Open state or not. This is exactly like a database connection object where you can check if its in open state or not. The following code snippet shows the change that I made to check for state of the WCF client object.


if (_serviceClient.State == CommunicationState.Opened)
{
_serviceClient.Close();
}

Sunday, January 17, 2010

The first post in blogspot

I will try to make some thing that i learned from Internet (certainly about OOP)