Tuesday, March 25, 2014

DataContract vs Serializable WCF

When applied to a class Serializable attribute serializes everything inside of it including private fields thus the developer has no control as what to expose.

When DataContract attribute is applied to a class it will serialize only the elements decorated with DataMember attribute thus the developer will have more control.

ServiceContract Name property WCF

This property can give your contract a name which is different from the actual interface name. So this way if you decide to change the interface name of a already running WCF service your client wont break.

Ex:

 [ServiceContract(Name="IGoodbyeService")]

    public interface IHelloService

    {

        [OperationContract]

        string GetMessage(string name);

    }


Now the contract name would be IGoodbyeService rather than  IHelloService.

To verify in the WSDL generated check the porttype property it should be IGoodbyeService

Note: Same can be done for OperationContract as well 

Monday, March 24, 2014

Basic Elements of WCF Appconfig

All config items should be inside ,

<system.serviceModel>

This is the WCF namespace provided by Microsoft.

Inside the <system.serviceModel> element you have the <services> element. This is where you configure all your services.
For each service you have a <service> element inside <services> element.
A particular service can be exposed using more than one endpoint. (Ex: http , net.tcp etc)
So for each endpoint in a service you configure a <endpoint> element.
An endpoint element will have three essential things,

  1. address
  2. binding
  3. contract  
Here is a  sample config (not complete) to get an idea.

The service name is HelloService (class and namespace both are HelloService) and has two endpoints (http and net.tcp) for the service.


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

Saturday, March 22, 2014

system.web vs system.webserver

I have not worked in IIS6 and mostly I have worked on IIS 7+ and this came up when I was following a very old article (2003) ASP.Net article.

Anyway system.web is IIS6 and classic mode.
system.webserver is IIS7+  and integrated mode.

One scenario you might come across is when adding httpmodules to your ASP.Net project. If you want backward compatibility then you should add the module section to both system.web section (which is there by default for all ASP.Net projects ) and to system.webserver.

<system.web>
    <httpModules>
    </httpModules>
</system.web>

And

<system.webServer>
    <modules>
    </modules>
</system.webServer>


Sunday, March 9, 2014

SQL JOIN Example

Joins can be quite perplexing initially hopefully this simple example will help. 

Joins are used to combine data from multiple tables (usually two tables)

There are three types of joins,

1. INNER
2. OUTER - FULL, LEFT, RIGHT
3. CROSS


For all examples I'll be using the below given tables and use the Name field as the condition.

                                                                         Table1
Id1
Name1
1
Padmika
2
Dissanayake
3
Sukitha
4
Pahan


                                                                        Table2
Id2
Name2
1
Fernando
2
Malaka
3
Padmika
4
Sukitha


1. INNER JOIN

Returns only the set of records that match the Name field in both Table 1 and Table2

SELECT * FROM Table1
INNER JOIN Table2
ON Table1.Name1 = Table2.Name2
Result,

Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
3
Sukitha
4
Sukitha

* This gets executed in the following manner, first get the first record from Table 1 get its name field compare it to Table 2 first record name field if it’s a match return it, now compare with Table2 second record name field…  etc.

You will see that only the records that have a common name is returned
This is similar to the intersection of two sets (Table1 and Table2) based on Name.  


2. OUTER JOIN

2.1 FULL OUTER JOIN

Returns all records in Table 1 and Table 2, with matching records from both sides where available. If there is no match, the missing side will contain null.

SELECT * FROM Table1
FULL OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2

Result,
Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
2
Dissanayake
null
Null
3
Sukitha
4
Sukitha
4
Pahan
null
Null
null
Null
1
Fernando
null
Null
2
Malaka

This is similar to the union of two sets (Table1 and Table2) based on Name.

2.2 LEFT OUTER JOIN

 Returns the complete set of records from Table 1 (Left table), with the matching records for Name field (where available) in Table 2. If there is no match, the right side will contain null.

SELECT * FROM Table1
LEFT OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2

Results,

Id1
Name1
Id2
Name2
1
Padmika
3
Padmika
2
Dissanayake
null
null
3
Sukitha
4
Sukitha
4
Pahan
null
null



Special case of the above result would be to select records that do not have any matching records from the right table (Table2)

To do this just add a ‘WHERE Table2.Id2 IS NULL’ clause to the above query.

Ex: You have a Customer table and an Order table (Has a CustomerID column) and want to find out which customers have not placed any orders  

2.3 RIGHT OUTER JOIN

Similar to the above but all the right table (Table2) records will return rather than the left table ones.

SELECT * FROM Table1
RIGHT OUTER JOIN Table2
ON Table1.Name1 = Table2.Name2


Results,

Id1
Name1
Id2
Name2
null
null
1
Fernando
null
null
2
Malaka
1
Padmika
3
Padmika
3
Sukitha
4
Sukitha


Note : When inspected carefully you will see that in this example,
OUTER JOIN result = LEFT JOIN result + RIGHT JOIN result – INNER JOIN result

Saturday, March 8, 2014

SQL COUNT Example

COUNT returns the number of rows in a select statement (Also note that it doesn't consider null values),

Note : I'll be using the AdventureWorks2008R2 sample database which can be downloaded from here,

http://msftdbprodsamples.codeplex.com/releases/view/59211


Example 1,

/* By using the * we return the number of records in the Sales.Customer table */
select

count(*)

from Sales.Customer;



Example 2,

/* By using StoreID inside the COUNT() function we return the number of store id's in the Sales.Customer table */

select

count(StoreID)

from Sales.Customer;


Example 3,
  /* By using "DISTINCT StoreID" inside the COUNT() function we return the unique number of store id's in the Sales.Customer table */

select

count(distinct StoreID)

from Sales.Customer;
Note: From a performance point of view it is better to use 1 instead of * inside the COUNT function.

Saturday, March 1, 2014

DDL vs DML in SQL Server

DDL
Data Definition Language (DDL) statements are used to define the database structure or schema. Examples:

  • CREATE - to create objects in the database
  • ALTER - alters the structure of the database
  • DROP - delete objects from the database

DML
Data Manipulation Language (DML) statements are used for managing data within schema objects. Examples:


  • SELECT - retrieve data
  • INSERT - add data
  • UPDATE - modify data
  • DELETE - remove data
Note : This is not limited to SQL Server