In this section, I am going to talk about generic constraints. I have been getting queries around this, hence thought to put some material around the same. Generic Constraints are one of the lovely features of C#, which I guess developers are less using may be because of understanding.
In this particular post, I thought to unveil my book on latest Microsoft ASP.NET Core stack in conjunction with Angular. This is basically hand-book for all those people who like to build Single Page App, right from the scratch meeting all the industry standards.
In this section, we will look at Config-Less Hosting.
WCF does not require config file. It only requires endpoint information. It does not matter which medium it is coming from. You can provide all these information from the code itself. You can get all mandatory information from some source say xml file or database to construct the endpoint. I will give the demo by simply hardcoding the same. Let us go to console hosting. Nevertheless, before making any change in code, let me just comment the config section for WCF in APP.config file. Below is the snippet in the finished state.
My goal here is to use netTcpBinding with this web-hosting. Therefore, if I simply go ahead and change the binding in my web.config file, then it will result me the below error. However, the same can be achieved with the help of WAS.
In this section, we will discuss Web Hosting. Now, in case of Web-Hosting IIS is going to all our hosting. It instantiates and manages the ServiceHost instance automatically. One point to note here that IIS is limited to HTTP. Nevertheless, with the use of WAS (Windows Application Services) we can use other bindings as well. Without, WAS we are using IIS for external web services. In WCF, we used to require .SVC file for browsing point. However, with .NET 4.0 onwards this need has been eliminated. Now, the same thing can be achieved virtually with configuration itself that we will see in a moment.
Hosting is the key ingredient to give life to your service. Without a host, service holds no meaning. Host is the application, which is responsible for listening the calls and then instantiating the service. WCF offers two types of hosting; one is called Web-Hosting which is there since web service days. Few artifacts about this is easy to setup, dependency on IIS and only supported protocol is HTTP. There is a catch involved in this. We will cover the same in coming section. Now, another hosting scenario is Self-Hosting. Self-Hosting covers any application. Any application can be host. It requires little bit of coding but gives very deeper control over your hosting. With this, you can setup custom hosting, which will feature rich. So, without wasting time let us get started.
In this section, we will look at the C# Interactive Coding Window. This is the new feature added in VS 2015 Update 1. This is one of the coolest experimental thing to test your snippet quickly in the editor itself. First of all, In order to open the C# interactive, you need to navigate to View –> Other Windows –> C# Interactive as shown below in the snapshot.
In this section, we will delve further with Interfaces and see Why Interfaces are important for any Scalable, Extend-able and Testable. And, one of the key reasons which I believe basically for using Interfaces is to keep the code future proof and Maintainable. Now, let us consider a below case. Here, I have plain simple class with few properties in it. Now, this will serve as model for me populating the values.
In previous discussions, we have already seen Interfaces and Abstract Classes in action. Now, let us see live implementation of all three around the same use case. In the end, we will also see the differences between the same. And, then you choose, which is best fit for you.
Let us first discuss simple plain class. Below, I have written simple class to calculate area and perimeter.
In this example, I have kept concrete implementation of perimeter calculation but area calculation is virtual method as different objects will have different area calculation, hence it needs to be implemented exclusively in derived class. Now, let us look at the derived class for the same.
Here, virtual method got implemented its way. Now, let’s look at main program for the same.
Now, when I run the above program, it will produce the following output.
This was just the case with simple plain class. Now, Let us look the same example with abstract class implementation. Below is the abstract class for the same.
Here, in abstract class instead of keeping Virtual method, I just kept abstract method and abstract method cannot have body in base class. Now, let us see it’s implementation in derived class.
Fairly simple and clean. Similarly, main program for the same, now looks like
With the above change in place, when I run the same, then it will produce the following output.
Similarly, we can implement the same stuff using interface. Below is the sample Interface for the same and it’s implementation.
Now, the main program for the same will look like as shown below.
With the above change in place, it will produce the following output.
Therefore, in this section, we have seen all three in action. Now, let us discuss few important points around these three.
Plain Class:- Frankly speaking, the disadvantage of simple class or plain class is, it doesn’t enforce to implement the virtual methods in the derived class. Therefore, at compile time it won’t produce any error, but if by chance if you call the same method in main program, then at run time it will throw exception.
Abstract Class:- It enforces to implement the abstract method in the derived class else, we will get compile time error, which is good obviously. However, Abstract classes can contain concrete methods with implementations as well. Now, in C#, one class can get inherited from only one class as c# doesn’t support multiple inheritance. Members of abstract classes can have access to access modifiers like public, private, protected etc. Also, abstract classes can contain anything like Events, Fields, Properties, Constructors, Destructors, Methods, Indexers
Interfaces:- It also enforces compile time checking. You need to implement all the methods upfront. Interface can’t have any implementations inside. It can only have declarations. Also, a class can implement multiple interfaces. Interfaces by default are public. Hence, if we try to add any access modifier to any interface member, we will get compile time error. But, interfaces can have Methods, Events, Indexers or Properties.