What is this new .NET standard thingy?

If you are .NET developer before 5 years life was simple. If someone asked you what is your domain you would reply “I’m a .NET developer”. It’s not the case anymore. Now we have several .NET and its kind of confusing for any new developers to come in to .NET landscape and pick one.

Background

Initially we had only this .NET Framework. Which is kind of a bigger framework and it allows us to create mobile apps, desktop apps and web apps that run only on Windows PCs, devices and servers

dotnet standard 001

And then around 2011 we had XAMARIN which kind of brought the power and productivity of .NET to iOS and Andriod by reusing skills and code while giving access to the native APIs and performance. But it was going on in a different track till Microsoft acquired XAMARIN on 2016.

dotnet standard 002

And then we have the new .NET  Core which gives you a blazing fast and modular platform for creating server application that run on Windows, Linux and Mac.

dotnet standard 003

So we have all these platforms and different app models like WPF, ASP.NET, IOS, Android etc. that kind of makes sense since some run on web and sum run mobile etc, but when we look at the Base Class Libraries(BCL) for us developers doesn’t not make any sense at all. Since we will be using these base libraries to develop our applications.

dotnet standard  004.PNG

So what are all the problems with this

  • Difficult to reuse skills
    • Need to master all 2 base class libraries
  • Difficult to reuse code
    • Need to target a fairly small common denominator
  • Difficult to innovate
    • We need implementation on each platform

And this is where .NET Standard come in. The Idea is that there are places where it makes sense to have different app models running in different places doing different things However the Standard Base Library should be common. So the Idea is to take all the stuff thats there in each of these frameworks and put it all into one single container called .NET Standard.dotnet standard  005.PNG

What exactly is a .NET Standard?

.NET Standard is just a specification. It’s not something that you would install along with other .NET frameworks. It’s basically a set of APIs that all .NET platform have to implement.

To put this in a better context let me explain it from a web developer perspective. Lets take HTML specification, It’s designed by W3C and all browsers like Chrome, Edge, Firefox etc has to implement in their respective browsers.

.NET Standard current version is 2.0 and it has support 20K more APIs than .NET standard 1.x. And already 70% of Nuget packages are .NET standard complaint.

If you already have a .NET Library published in Nuget and if you haven’t migrated to .NET Standard don’t worry! You library will be still be accessible to developers who are doing .NET Standard and you don’t have to recompile your dll or anything.

Conclusion

If you are .NET developer For all your new projects I think we should start developing against .Net Standard while Microsoft works on unifying the different BCL libraries at the background.

Please stay tuned for more .net core stuff. You can also check out my article on how to create dotnet console application in 3 steps.

How to customize Swagger UI in ASP.NET Web API

Documentation is one of the most important thing to any WEB API. That is going to be the single point of reference for the developers who are going to consume your APIs. An API with bad documentation is never going to get popular among developers. If creating documentation is one tedious process maintaining the documentation is completely different nightmare until now. So here comes the swagger as the savior for the above problems.

What is swagger?

Swagger is one of the most popular API tooling. It helps the developers to design, build and document their API using a swagger definition file.

And that’s not all…

Once you have described your API in a swagger, you’ve opened a treasure chest full of swagger based tools including client generator tools which you can use to generated consumer/client application in a variety of platforms.

Cool. But how can I use it in my ASP.NET WEB API? Noworries! it’s as simple as adding a NuGet package called Swashbuckle to you project.

Create new project

  • Open Visual Studio, If you don’t have one you should go and download now. After all the Visual Studio community edition is free of cost.
  • Once opened Click File->New->New Project

    swaggerui_001_create_project

  • Select ASP.NET Web Application (.NET Framework) and click Ok

    swaggerui002_create_project

  • Then Select Web API Template and click Ok.

Import Swashbuckle

  • Open the Nuget Package manager for the project
  • And browse for the package “Swashbuckle” and install the package. If you are using ASP.NET Core then you need to install Swashbuckle.AspNetCore
  • Start the Web API
  • Now if you browse to <your-root-url>/swagger you should see the swagger documentation like below

Customizing the UI

The UI isn’t bad but sometimes you may want to customize things like including your company name changing font & colors etc. The good thing about the Swashbuckle is it has few extension points that we can use to customize the look and feel.

If you open SwaggerConfig.cs file under App_Start folder you can see that all the configuration that is related to the swagger is present.

Things you can customize in Swashbuckle

  • Stylesheet
  • Javascript
  • HTML
  • Submit methods, Boolean values, etc

Customizing index.html

You can inject your own index.html using this template. Using this you can customize things like editing your company name removing things like api_key input and explore buttons.

  • To do this first you need add an index.html to your project under Content.

    swaggerui005_added index file

  • In Solution Explorer, right click the file and open its properties window. Change the “Build Action” to “Embedded Resource”

    swaggerui006_embedded resource

Once this is done you can open SwaggerConfig.cs and search for EnableSwaggerUi under that you can uncomment line starts with c.CustomAsset(“index”, and you need to change the parameters based on your project. For example if you project name is ECommerce and you have placed the index.html under Content folder. The line should look like below

c.CustomAsset("index", thisAssembly, "ECommerce.Content.index.html");

swaggerui004_enable swagger ui

To customize the styles you can visit this github project and get any css files based on your liking and add them to the project same as you did with index.html.

Once the CSS is added and it is made as an embedded resource. You should edit the SwaggerConfig.cs again like below

c.InjectStylesheet(thisAssembly, "ECommerce.Content.material-theme.css");

Once you done this your swagger UI should look like below

swaggerui007_final view

Please visit the office github link to know more about swashbuckle

Happy coding!!

 

 

 

 

 

 

 

 

 

 

Common mistakes that C# noobs do

Lets face it! all of us when we  initially started programming have done some silly mistakes without understanding the language or a language feature properly. Most of  us C# developers learn these mistakes the hard way. And it’s part of the journey of any beginner level developer in their career. But at the same time it doesn’t mean every one have to learn the hard way. As the saying goes “Standing on the shoulders of giants” we see more farther than our predecessors, not because we have a better understanding, but because we are lifted up and borne aloft on their experience.

As developers, along with developing software we should also develop our skills to better ourselves and to avoid mistakes that we did in the past. So here I have compiled some of the common mistakes that beginner level C# developers do to help you avoid them in future. Please fee free to comment below some of your experience on the topic

Use interfaces properly

I have seen several beginners who are not just new to C# developers but to programming itself do this, where they will declare an interface and will derive the interface in a class but they wont use the interface during instantiation. Don’t understand? let me explain with code!

Lets see a typical interface declaration

public interface IMonitor
{
void Configure();
void Start();
void Stop();
}

public class ActivityMonitor:IMonitor
{
public void Configure()
{
//some code
}
public void Start()
{
//some code
}
public void Stop()
{
//some code
}
}

Now comes the important part of instantiating an object

var wrongUse = new ActivityMonitor();
IMonitor correctUse = new ActivityMonitor();

In the above sample both statements are valid but there is one key difference. The first statement instantiates an ActivityMonitor Object directly which would work but if you try to assign a different implementation of IMonitor to the object it will fail in the compilation step.

var wrongUse = new ActivityMonitor();
wrongUse= new OtherMonitor(); // error
IMonitor correctUse = new ActivityMonitor();
correctUse = new OtherMonitor(); // works

This is because if you want the polymorphism to occur you have to use interface type when you declare an instance.

Know your defaults

In C# value types are not null be it an int or DateTime or any other types which inherits struct. On other hand a reference can always be null.

Value types can be made null only if you declared  it as an nullable(?) type explicitly. For example

static List<int> alist;
static DateTime startTime;
private static void Main(string[] args)
{
Console.WriteLine(alist == null); //Prints true
Console.WriteLine(startTime == null);//Prints false
}

if you want to know the default of some type please use default() keyword.

Reference types vs Value Types

If you don’t know the type that you are using is a value type or reference type you will run to issues constantly. Because when you assign one value to other it makes a copies the value. But reference types copies the reference alone any changes made to the newer value will change both the variables

Point point1 = new Point(10, 20);
Point point2 = point1;
point2.X = 50;
Console.WriteLine(point1.X); // 10 (does this surprise you?)
Console.WriteLine(point2.X); // 50

Pen pen1 = new Pen(Color.Black);
Pen pen2 = pen1;
pen2.Color = Color.Blue;
Console.WriteLine(pen1.Color); // Blue (or does this surprise you?)
Console.WriteLine(pen2.Color); // Blue

The answer is always look at the type of the variable if it is a struct then its a value type, if its a class then its a reference type.

Start Loving LINQ

C#3.0 was introduced on almost a decade ago on 2007. One of the most important feature on that release is LINQ(Language integrated query). It has fundamentally changed how we manipulate collections on C# with its SQL like syntax. But for some reasons lot of beginners find it difficult to get a grasp of LINQ because of its strange syntax.

Lot of programmers also think that its only use is in code that queries database. Even though database querying is one of primary uses of LINQ, it can work with any collection which implements IEnumerable. So far example, if you had an array of Activities, instead of writing

var sum = 0;
foreach (var activity in activities)
{
if (activity.IsRun)
sum += activity.Count;
}

you could just write

var sum = (from account in activities
where account.IsRun
select account.Count).Sum();

I know this is a simple example but with the power of LINQ you can easily replace dozens of statements with a single LINQ statement in an iterative loop in your code. There are also few things that you need to be aware of there could be trade off in certain scenarios when it comes to performance. I would suggest use LINQ when you can predict amount of data that you will iterate through. And always do a performance comparison with normal for loop and LINQ.

Stop nesting exceptions

I’m guilty of doing this in the initial part of my career in the name of exception of handling. beginners tend to add try catch to every single method that they right and most of these exception block will have throw at the catch block. In some cases they may add a log there too. but if all you are going to do is throw back to the caller why catch it in the first place?

public class DeliveryComponent
{
public void Order()
{
try
{
Pay();
}
catch (Exception ex)
{

}
}
private void Pay()
{
try
{
DoTransaction();
}
catch (Exception ex)
{
throw;
}
}
private void DoTransaction()
{
try
{
//some code
}
catch (Exception ex)
{
throw;
}
}
}

More than that this will add a performance overhead to the program. Most of the time it is enough to put the try-catch in the upper level of the function like below.

You will want to handle exception in the lower only if you want to explicitly handle the exception and do some operations like retrying, logging etc.

public class DeliveryComponent
{
public void Order()
{
try
{
Pay();
}
catch (Exception)
{
// ignored
}
}
private void Pay()
{
DoTransaction();
}
private void DoTransaction()
{
//some code
}
}

Use using statement whenever possible

Memory management is a huge problem in programming When you are using resources like Sql connection, File streams, Network socket etc. C# provides a convenient way to call the Dispose method whenever you are finished with an object and avoid memory leaks.

In efficient way

var con = new SqlConnection("");
con.Open();
//some operation
con.Close();

Efficient way

using (var con = new SqlConnection(""))
{
con.Open();
}

Use constraints on your generics

Generics are one of the coolest features of C#. Some beginner level devs may use generics but have no idea about how to put constraints on the generics so that your generic type will not be misused.

For example consider the below example

public interface IActivityRepository<T>
{
bool Insert(T activity);
bool Update(T activity);
T Get(int id);
bool Delete(int id);
}

From the code you can see IActivityRepository accepts any type and tries to insert them in database. But its obvious that IActivityRepository expects a reference type. but some developer may try to do the following

public class ValueActivityRepository : IActivityRepository<int>
{
public bool Insert(int activity)
{
//some code
}

public bool Update(int activity)
{
//some code
}

public int Get(int id)
{
//some code
}

public bool Delete(int id)
{
//some code
}
}

As you can see this is not the intended purpose of that interface. So you can add constraints to make sure the right kind of type is substituted as T.

public interface IActivityRepository<T>
where T : class, IActivity, new()
{
bool Insert(T activity);
bool Update(T activity);
T Get(int id);
bool Delete(int id);
}

The above code makes sure that the generic type T must be a implementation of IActivity

Exceptions: Let’s not push it under the rug. Be Explicit

C# is a statically typed language. This allows C# to pin point errors during the compilation step itself where a faulty type conversion will be detected much quickly. when you are doing explicit type conversion in C# you have two ways to follow. One will throw an exception on error and one will not.

Lets see them

object account = new CurrentAccount();
//METHOD 1
SavingsAccount account2 =(SavingsAccount)account;

//METHOD 2
SavingsAccount account3 = account as SavingsAccount;

In the above example the first method will throw an exception immediately. But the second will just assign account3 with null and which can possibly be consumed by some other method and will throw a NullReferenceException which possible surface at much later time which makes is difficult to track down.

Conclusion

C# is a very powerful and flexible language which supports multiple paradigm of programming. With any tool or language which is as powerful as C# there is always going to be caveats. The best thing that we can do is to learn from your mistakes and avoiding these common problems will make you a better programmer.

Please comment below if you want to add any other common mistakes that C# devs do and also check out my other articles on C#