Dot Net/Wcf/Sql server

Sunday 2 October 2016

Difference between loose coupling and tight coupling

What is Tight Coupling:-

When the components are highly dependent to each other, then if you want to change one component then it will affect the other component. Suppose I have developed a small application and using tight coupling then if I make any small change in the class then I have to change all the places where I have used the object of that class then there will be no problem but suppose we have developed a enterprise application where single class used thousand times then if any change is required to the class then I have to modify all those places where this class is used. So it is difficult maintain code in this scenario. Some codes and picture are given below to describe tight coupling in a tabular form.

What is Loose Coupling:-

Main motive of loose coupling is to reduce the inter-dependency of the components of a system. Because it will reduce the risk on changing one component will require changes in any other component. So a loose coupling is nothing but a design strategies which increase stability of entire framework, flexibility of a system and more maintainable.  Instantiation of the actual implementation will take place somewhere else in the code. This is the one of the most important reasons why dependency injection frameworks are being used. Some codes and picture are given below to describe tight coupling in a tabular form.

Real life example of Tight Coupling and Loose Coupling are

One of the best examples I have found on stackoverflow.com by BKSpurgeon

Tight coupling
Think of your skin. It's stuck to your body. It fits like a glove. But what if you wanted to change your skin colour from say white to black? Can you imagine just how painful it would be to peel off your skin, dye it, and then to paste it back on etc? Changing your skin is difficult because it is tightly coupled to your body. You just can't make changes easily. You would have to fundamentally redesign a human being in order to make this possible.
God was not a good object orientated programmer.
Loose coupling
Now think of getting dressed in the morning. You don't like blue? No problems: you can put a red shirt on instead. You can do this easily and effortlessly because the shirt is not really connected to your body the same way as your skin. The shirt doesn't know or care about what body it is going on. Clothes can easily and painlessly be changed. Perfect example of loose coupling.

Now i have describe the difference in coding standard in both coupling :

Tight Coupling
Loose Coupling
class CallingClass 
private  bool  isValidate;    
public int SetMethod() 
if (isValidate) 
return new mainClass ().Parameter; 
return 0; 
      } 
     public void Printmethod() 
new mainClass ().Main_method (); 
class mainClass
public int Parameter; 
public void Main_method() 
Console.WriteLine(Parameter); 

class CallingClass 
private ISanpleInterface  intr_imp;
public CallingClass  (ISanpleInterface  intr_variable)
{ intr_imp= intr_variable;
}
public int SetMethod() 
{ intr_imp. SetMethod();
}
public void Printmethod() 
intr_imp..Show (); 
   interface ISanpleInterface 
int SetMethod(); 
void Show(); 
class mainClass : ISanpleInterface 
public int Parameter=123; 
public int SetMethod()
{ return parameter;
}
public void Show () 
Console.WriteLine(Parameter); 


Loose Coupling advantages
This is a small code example, but in the large project is will be very hard to make those changes in every class. 







Friday 30 September 2016

Service Oriented architecture (SOA)

What is Service?


 A service is an Independent system which accepts one or more requests and returns one or more responses via a set of published and well defined interfaces. A service is implemented by one or more components, and is a higher-level aggregation. COM or COM+ (Learn more about COM+ component of .net,Learn more about COM component of .net)components functionality limited within homogeneous environments; service orientation fills the gap by establishing reuse in heterogeneous environments (.net service can communicate with java or another client) by aggregating one or more components into a service. Service communicates through message using open standards (Use XML). Learn more details about XML

What is Service Oriented architecture?


In reality, SOA is a collection of well-defined services. Basically SOA use loosely coupled service to get the desire result easily.  Implementation details of a service are hidden from the client/consumer so any change in the implementation doesn’t affect the service until the contract between them is change. Service providers are components that execute some business logic based on predetermined inputs and outputs, and expose this functionality through an SOA implementation. This allows systems based on SOA to respond more quickly and cost effectively for the business. The main difference between component and SOA is that, SOA provide a open standards message which is not specific to any programming language or platform. As a result, you can achieve a high degree of loose coupling and interoperability across platforms and technologies. In a traditional client-server world, the provider will be a server and the consumer will be a client
Note:
Tight coupling is when a group of classes are highly dependent on one another. This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.
Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns. Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.










Figure 1–1. How SOA components interact with each other




Picture description


A repository contains a description of the services, where the providers register their services and consumers find what services are provided.


Service orientation defines loosely coupled autonomous business systems (or services) that exchange information based on messages. The term services are used in many contexts, but in the context of service orientation, a service is based on four fundamental Principle proposed by the WCF team of Microsoft, in the following sections.
1: • Know your boundaries: A well-defined public interface is the main entry point into the service, and all interactions occur using that.
• Services should be easy to consume: It should be easy for other developers to consume the service. Also, the service interface should allow the ability to evolve over time without breaking existing consumers of the service.
• Keep the service surface area small: As the number of public interfaces grows, it will be difficult to consume and maintain the service. So interface accept well defined message and respond the same.
• Don’t expose implementation details: implementation details kept internal since it uses loose coupling between the consumer and the service.
2: Services need to be isolated and autonomous. The design principles to keep in mind for the second tenet are as follows:
 • Service versioning and deployment are independent deployed system.
• Contracts should not be changed, after publish.
3: Services Share the Schema and Contract, Not the Class Services interacts using policies, schemas, and behaviours instead of classes, which have traditionally provided most of this functionality. The service contract should contain the message formats (defined using an XML schema (Learn more details about XML)), message exchange patterns (MEPs, which are defined in WSDL (what is WDLC?)). The biggest challenge you face is the stability of the service, once it has been published, which can become difficult to change without impacting any of the consumers. The design principles to keep in mind for the third tenet are as follows:
 • Service contracts constituting of message format (define using XML Schema), Message exchange pattern (define in WSDL), and the policy remain same.
• Contracts should be as explicit as possible; this means if any change in contract for newer version of the service then it will not affect the old consumer.
• Do not expose internal data representation publicly; the public data scheme should be absolute.
4:  When we design a service then you should ensure that the policy assertions should be clear if there is an exception on your service. The above four principle help us to design a service. If we know the requirement then properly then it will be very easy to define a service, because that time we can build the service properly. The individual operation within a service logical and it will not contradict the service. Similarly if we use the same operation it is not mandatory to rename it, try to keep the naming as generic as possible. Naming convention initially is not so important, but when your service inventory in the organization grow reuse the existing service to achieve integration within the various groups and systems


Advantages of SOA


Services can provide a significant benefit because you can achieve reuse at many levels of abstraction, as compared to traditional methods. SOA can offer reuse at multiple levels, including code, services, and functionality. This feature enhances flexibility in the design of enterprise applications.

COMPONENTS VS. SERVICES

It is natural to be confused about the terms component and service, and what they mean. A component is a piece of compiled code that can be assembled with other components to build applications. Components can also be easily reused within the same application or across different applications. This helps reduce the cost of developing and maintaining the application once the components mature within an organization. Components are usually associated with the object-oriented programming (OOP) paradigm.

A service is implemented by one or more components, and is a higher-level aggregation. Component reuse seems to work well in homogeneous environments; service orientation fills the gap by establishing reuse in heterogeneous environments by aggregating one or more components into a service and making them accessible through messages using open standards. These service definitions are deployed with the service, and they govern the communication from the consumers of the service via various contracts and policies, among other things.

Saturday 17 September 2016

COM+


Background 
In an enterprise level application if we want to use COM then we need to make a lot of infrastructure-level code to build large-scale application because it must have these features :

• Authentication
• Object pooling
• Transaction management
• Support distributed architecture

but in COM if we want to use it over a network then it will fail to interact and lost its scalability. So For an Large Scale application Microsoft came out with Distributed COM.

Learn more about Component Object Model 
DCOM

DCOM is an Network version of COM and it provide the sharing of object over network. In DCOM the client is completely unknown about the location of the component which provide location transparency. DCOM is not a popular technology of Microsoft because DCOM is a RPC (Remote Procedure Call) protocol. It is not used default port of HTTP, so for communication with other component all the port should be open and it is a major security issue.
MTS 

Microsoft released Microsoft Transaction Server (MTS). It provide the following functionalities :

Shared resource and increase Scalability of an application
Transaction Service : if all the unit of work is completed than the transaction will be treated as completed successfully completed and if any of the work is unsuccessful then the transaction will roll back.
Object Pooling : Object Pool is nothing but a container of objects that are ready for use. MTS Hold the memory resources of object when a client is requested then it released. If another client request for the same component then MTS give the containing  object to the new client and it reduced the server overhead.
Security : MTS ensure that no unauthorized clients can request a component.

COM+

Microsoft come up with new technology which is known as COM+ and it provide all basic COM and a set of additional services like


• Authentication
• Object pooling
• Transaction management
• Support distributed architecture
• Queued Components (QC)
• Loosely Coupled Events (LCE)
• Just In Time Activation
• Basic Interception Services
The COM+ components containing the business logic are platform-independent and can be moved to a different. If you are hosting a mission-critical application and  need to move your COM+ components from one platform to the other, you can do it without any change in the business-logic code. A major highlight of the COM+  specification is the support for ready-made components. So an enterprise application having n-tire architecture does not matter where the business logic is. It also supports multitier architecture by providing the surrogate process for hosting the business objects. In many ways, COM+ is a combination of COM, DCOM, Microsoft Transaction Server (MTS), and MSMQ in a single product. This application infrastructure includes services such as transactions, resource management, security, and synchronization.
The classes in .NET that can be hosted by the COM+ application and can use COM+ services are called serviced components. Any class in .NET that derives directly or indirectly from the System.EnterpriseServices.ServicedComponent class is called a serviced component class..COM+ is introduce with Microsoft 2000 operating system.



Wednesday 14 September 2016

Component Object model


Overview 

Component based model developed by Microsoft to introduce the language interoperability. To know about COM we first know the scenario when COM concept comes in the .Net. We know that in object orientation concept was  re usability of  the code within the same application, but in any case if i want to call the function of any class from other application of different language then it was not possible.
Another disadvantage of object orientation is that after development of the software any change is required on any classes then the whole application needed to be rebuilt and deploy again to the client machine. Actually the problem is that in this case we are getting the reusability for a particular application not for whole application level.

What is COM?

COM is about clients communicating with objects. COM provide the below facility in application level

Language Interoperability: If we look forward in object orientation concept then class of one language cant not use in another application of other language. Microsoft to introduce the language interoperability’s with component Object model.

Location Transparency: We can deploy the application in anywhere of our machine and use it from any other application of our machine. If any modification required in the component we will only upgrade the COM component and corresponding changes automatically reflect those application where this component has used.

Object Oriented Programming = Polymorphism + (Some) Late Binding + (Some) Encapsulation + Inheritance
Component Oriented Programming = Polymorphism + (Really) Late Binding + (Real, Enforced) Encapsulation + Interface Inheritance + Binary Reuse”


Note: If you are developing your client .NET application on the same machine, you don't need to manually register it. However, if you are working entirely on a new machine, you need to register this component with the regsvr32.exe utility. To register a COM component you have to write the following code regsvr32 "Path where COM DLL placed" Now any modification or up gradation done with the COM component then just build the application and replace the component to the register component path.


Thursday 8 September 2016

Marge rows of a table using coalesce

Suppose we have a report where we want to show all the rows value of a cell in a single unit separated by a comma (,) or '-' then we can use this coalesce function. Before sql server 2008 this function was not available.
First 
Create a table; in this case, i have given a name test08 with values given below

name
a
b
c
d
e
f
g
NULL
h
a

Now, write the following line of code to merge the following rows of this table:

declare @var as varchar(max)
declare @tt as table
 (
 name varchar(max)
 )

  insert into @tt
 select    name fROM  test08

 set @var=''

select @var=coalesce(case when @var ='' then +''+ rtrim(isnull(name,'NA'))-- this line will check value exist or not
else @var +','+ rtrim(isnull(name,'NA'))-- if row value exist then marge eache row seperated by comma(,)
          end,', ')
    from @tt

   Print @var

-------------------------------------------------Or --------------------------------------------------

DECLARE @var1 VARCHAR(1000)

SELECT @var1 = COALESCE(@var1,'') + Name + ';'
FROM   test08


SELECT @var1

OUT PUT :
a,b,c,d,e,f,g,NA,h,a

From the above output we get that same value are added to merge the distinct row use this

declare @var as varchar(max)
declare @tt as table
 (
 name varchar(max)
 )

  insert into @tt
 select    name fROM  test08 group by name

 set @var=''

 select @var=coalesce(case when @var ='' then +''+ rtrim(isnull(name,'NA'))-- this line will check value exist or not
          else @var +','+ rtrim(isnull(name,'NA'))-- if row value exist then marge eache row seperated by comma(,)
          end,', ')
    from @tt
Print @var


--------------------------------OR---------------------------------------------


DECLARE @var1 VARCHAR(1000)

SELECT @var1 = COALESCE(@var1,'') + Name + ';'
FROM   test08
group by name

SELECT @var1