Thursday, 3 July 2014

Namespaces in C#


Namespaces

Namespaces are used to organize code at the highest logical level. A Namespaces in organization construct. It helps us how our code is arranged. It makes our code clear.

Tuesday, 1 July 2014

What is Service
A service is a program that performs a task and that a client application can communicate with through well-defined messages. To call a Web or WCF service, the client passes it a message, which consists of XML. The message will typically include a request for the service to perform an action, such as retrieve or update the inventory information for a product. If the service returns data to the client, it passes back a message, also consisting of XML. 

The client and the service need to agree on what the XML will look like. The most common message format is Simple Object Access Protocol (SOAP). SOAP defines the format of XML request and reply messages and how clients and services encode the data in the messages. 

What is Difference Between Object Oriented and Service Oriented
 
In object-oriented programming, objects are tightly coupled. To call the Inventory component, a client application creates an instance of the component and therefore, controls its lifetime. In a service-oriented application, services and clients are loosely coupled. To call the Inventory service, a client does not instantiate an instance of a component. It simply passes a message to the service and may receive a message in return. 

Service-oriented applications are loosely coupled. All communication occurs through messages. SOAP defines the format of messages. Contracts define the contents of messages. A service will publish a contract that specifies what methods clients can call, what arguments those methods take and what, if anything, the methods return. The contract can also specify security requirements and whether a transaction is required.

Why Use Windows Communication Foundation?

WCF provides a number of benefits over ASP.NET Web Services, including:
  • Support for sending messages using not only HTTP, but also TCP and other network protocols.
  • The ability to switch message protocols with minimal effort.
  • Support for hosting services on hosts other than a Web server.
  • Built-in support for the latest Web services standards (SOAP 1.2 and WS-*) and the ability to easily support new ones.
  • Support for security, transactions and reliability.
  • Support for sending messages using formats other than SOAP, such as Representational State Transfer (REST). 

What is Serialization

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
  The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.

Uses of Serailization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
Types of Serialization
Binary Serialization
Binary serialization is used for serializing an object which uses network steams, or convert a WCF object type and store in the file system. Binary serialization is used to get data back from a network stream and other types without any loss. The binary object can be serialized to disk, memory, network, etc. The class public and private members are serialized and restored back as the original value in the deserialization process. This process is used to store large object values in persistence storage or to send them through a network and restore back when required. 

The serialization process is useful when a large object has to be stored in persistence storage. The object oriented class hierarchy may involve complexity for storing and retrieving values. This process is useful for resolving complexities automatically. The Common Language Runtime will manage the object reference and circular references between classes automatically.

Binary serialization uses binary format for understanding communication between .NET Framework applications. It saves metadata information along with an object. It helps for restructuring the object when it deserializes an object from persistence storage. A binary formatter is faster than other serialization formatters. It can serialize generic and non-generic collections. This serialization supports serializing private and public members. 

XML And Soap Serialization

The XmlSerializer class is derived directly from the Object base class. XML serialization does not include type information. XML serialization serializes only public fields and properties of the object into an XML stream that confirms with the specific XSD language. 

he System.Xml.Serialization namespace has all the classes and interfaces for serializing and deserializing values. SOAP serialization confirms to the SOAP specification. Web applications can use the XmlSerializer class to create XML and send across applications. XML serialization converts only public fields and properties of an object into the XML stream.

Serializer in WCF

Basically their are two Serializer in WCF
1- DataContactSerializer
2- NetDataContactSerializer

 Dat­a­Con­tract­Se­ri­al­izer does not include the CLR type infor­ma­tion in the seri­al­ized XML, which allows for loose cou­pling between the client and server because they don’t have to share the same CLR types.
How­ever, this also means you often have to re-create the same types on the client, though Visual Stu­dio han­dles most of this for you when you add a new ser­vice ref­er­ence to your project. How­ever, rather than dese­ri­al­iz­ing the orig­i­nal type, mes­sages are dese­ri­al­ized into a new, but iden­ti­cal CLR type on the client.

NetDataContactSerializer
While Dat­a­Con­tract­Se­ri­al­izer is used by WCF by default, there is another seri­al­izer which you can use if you wish to share the same CLR types between the client and server – the Net­Dat­a­Con­tract­Se­ri­al­izer. The Net­Dat­a­Con­tract­Se­ri­al­izer dif­fers from the Dat­a­Con­tract­Se­ri­al­izer in that it includes the CLR type infor­ma­tion in the seri­al­ized XML and can only be used if the same CLR types are seri­al­ized and dese­ri­al­ized at both ends.
You can use the Net­Dat­a­Con­tract­Se­ri­al­izer on any type which are marked with the Dat­a­Con­trac­tAt­tribute or Seri­al­iz­ableAt­tribute, or types that imple­ment the ISe­ri­al­iz­able interface.

Here is the DataContractSerializer version of the Person data
<Customer xmlns="http://www.contoso.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <FirstName>Zighetti</FirstName>
  <ID>101</ID>
  <LastName>Barbara</LastName>
</Customer>
And here is the version from the NetDataContractSerializer
<Customer z:Id="1" z:Type="NetDCS.Person" z:Assembly="NetDCS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="http://www.contoso.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
  <FirstName z:Id="2">Zighetti</FirstName>
  <ID>101</ID>
  <LastName z:Id="3">Barbara</LastName>
</Customer>

Wednesday, 7 May 2014

Sql Server

To Know About the List of Procedure in Database

select * from sys.procedures

To Know About the List of Trigger in Database
select * from sys.triggers

To Know About the List of Tables in Database
select * from sys.tables

To Know about the List of Procedure
select * from INFORMATION_SCHEMA.ROUTINES where Routine_Type='Procedure'


To Know about the List of Functions
select * from INFORMATION_SCHEMA.ROUTINES where Routine_Type='Function'

http://www.mpez.co.in/Discom_Batch/rest/getETransactionCount/ETAAL_JBL/JBL@123/08-05-2014

http://164.100.72.97/webservice.asmx?wsdl






Monday, 31 March 2014

Shrink Log File In Sql Server 2008

To Know About DataBase Size

sp_helpdb "dbname"
GO


--Then Execute this query
ALTER DATABASE "dbname"
SET RECOVERY SIMPLE;
GO


-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (dblogfilename,1);
GO






-- To Know About Dblogfilename
SELECT file_id, name
FROM sys.database_files;
GO



DBCC SHRINKFILE (1, TRUNCATEONLY);

Thursday, 13 March 2014

SQL Server Procedures, tables and triggers

  • To Know about the details of Procedure: 
    • sp_helptext yourprocedurename 
  • To Know about the List of all Procedure in  a particular database: 
    • select * from sysobjects where xtype='P'
  • To Know about the List of all tables in  a particular database: 
    • select * from sysobjects where xtype='U'
  • To Know about the List of all trigger in  a particular database: 
    • select * from sysobjects where xtype='tr'
  • To Know about the List of all trigger on particular table: 
    • sp_helptrigger yourtablename
  • Enable/Disable Trigger
    • ALTER TABLE table_name DISABLE TRIGGER tr_name 
    • ALTER TABLE table_name ENABLE TRIGGER tr_name
      
      
       

Monday, 28 October 2013

Difference Between Var and Dynamic Keyword in C#

Many people have expressed confusion around the difference between var and dynamic in C#.  For both of them, the type is inferred rather than explicitly declared. 
dynamic test = 1;
var test2 = 2;
If I hover my mouse over the “var” in the code above, IntelliSense will show me that the compiler has correctly inferred that it is an Int32.  If I hover over “dynamic”, it will continue to be typed as “dynamic” since dynamic types aren’t resolved until runtime. 
However, var is statically typed, and dynamic is not. 
// Can a dynamic change type?  
dynamic test = 1;
test = "i'm a string now";  // compiles and runs just fine
var test2 = 2;
test2 = "i'm a string now"; // will give compile error
This is one of the key differences between dynamic and var.  A var is an implicitly typed variable that is inferred by the compiler, but it is just as strongly typed as if you had explicitly typed it yourself using “int test2 = 2;”.  A dynamic variable bypasses all compile-time type checking and resolves everything at runtime.  
I’ll comment out the last line in the code above to get the code to compile, and add some code to verify the types of the variables. 
// Can a dynamic change type?  
dynamic test = 1;
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
test = "i'm a string now";  // compiles and run just fine
Console.WriteLine("Dynamic as " + test.GetType() + ": " + test);
var test2 = 2;
//test2 = "i'm a string now"; // will give compile error
Console.WriteLine("Var as " + test2.GetType() + ": " + test2);
This produces the following output:
Dynamic as System.Int32: 1
Dynamic as System.String: i'm a string now
Var as System.Int32: 2




Object Dynamic Var
Can able to store any kind of value, because object is the base class of all type in .net framework. Can able to store any type of the variable, similar to old VB language variable. Can able to store any type of value but it require to initialize at the time of declaration.

Compiler has little information about the type

Compiler doesn't have any information about the this type of variable.

It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.

Object type can be passed as function argument and function also can return object type Dynamic type can be passed as function argument and function also can return object type Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.

Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Casting is not require but you need to know the property and methods related to stored type No need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.

Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time

Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type. Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code. Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

Difference Between Encapsulation and Abstraction

What is encapsulation?

Encapsulation is the process of hiding irrelevant data from the user. To understand encapsulation, consider an example of mobile phone. Whenever you buy a mobile, you don’t see how circuit board works. You are also not interested to know how digital signal converts into analog signal and vice versa. These are the irrelevant information for the mobile user, that’s why it is encapsulated inside a cabinet.
In C# programming, we will do same thing. We will create a cabinet and keep all the irrelevant information in it that will be unavailable for the user.

Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example of encapsulation.

What is abstraction?

Abstraction is just opposite of Encapsulation. Abstraction is mechanism to show only relevant data to the user. Consider the same mobile example again. Whenever you buy a mobile phone, you see their different types of functionalities as camera, mp3 player, calling function, recording function, multimedia etc. It is abstraction, because you are seeing only relevant information instead of their internal engineering.

We use abstraction in programming languages to make abstract class. Abstract class represents abstract view of methods and properties of class.

Implementation Difference Between Encapsulation and Abstraction

1.  Abstraction is implemented using interface and abstract class while Encapsulation is implemented using private and protected access modifier.

2. OOPS makes use of encapsulation to enforce the integrity of a type (i.e. to make sure data is used in an appropriate manner) by preventing programmers from accessing data in a non-intended manner. Through encapsulation, only a predetermined group of functions can access the data. The collective term for datatypes and operations (methods) bundled together with access restrictions (public/private, etc.) is a class.

3. Example of Encapsulation

Class Encapsulation
{
    private int marks;

    public int Marks 
   {
      get { return marks; }
      set { marks = value;}
    }
}

4. Example of Abstraction

abstract class Abstraction
{
    public abstract void doAbstraction();
}

public class AbstractionImpl: Abstraction
{
    public void doAbstraction()
   {
       //Implement it
   }
}