Programming

Building a self-hosted API with OWIN

Building a self-hosted API with OWIN

So it’s Friday and I haven’t published anything this week yet.  Figured I’d push out a short and quick tutorial on OWIN and give everyone something to play with over the weekend!

What is Owin?

OWIN is an acronym that stands for Open Web Interface for .NET and is meant to define a standard interface between web servers and applications. The goal of OWIN is to decouple servers and applications.  Due to this it encourages the development of simple modules for .NET web development.  Because of this OWIN is great for building small, simple, self-hosted applications.  It’s ability really shines when it comes to the creation of micro-service APIs.  Within this tutorial I will take you through the basic process of creating a small self-hosted API.  We will go from inception all the way to implementation and discuss possible use cases.

Continue reading →

Posted by DCCoder in Tutorials, 0 comments
Hello C# 8, goodbye null reference!

Hello C# 8, goodbye null reference!

Say hello to C# 8.0 and goodbye to those nasty little null-reference exceptions!   That’s right, Microsoft is getting ready to release yet another major version of the language!  This has been common knowledge for a little while now, so I may be slightly behind.  Behind or not, I still wanted to bring it up.  I am excited about all of the changes and features coming in the new C#.  I don’t have the time or the space here to cover all of them but I will touch on some of the most drastic and useful features being added in.

 

Null references

Brace Yourself, NullRef Exception incoming

Let’s face it, we’ve all been there.  Everything compiles, we run our program eagerly awaiting it’s output.  Then BAM!  A big nope screen is thrown in your face, saying something about a NullReferenceException.  Believe it or not Null References were suppose to be a thing of the past a long time ago.  Thankfully the C# designers have finally gotten around to trying to get rid of them.   Currently by default all reference types as well as variable types are nullable, this is all about to change.

Non-nullable by default

Starting with C# 8.0 reference types, by default, will be non-nullable.  Now this isn’t to say that you can’t make them that if you so choose, but again this is by default.  The C# compiler is also going to help you on this quest by throwing some helpful warnings if you forget to check for nulls or forget to make them nullable.   Take a look at the example below:

 

1
2
3
4
5
ISomeType notNull;
ISomeType? mayBeNull;

notNull = null; //This will throw a compile warning
mayBeNull = null; //This won't

Another nice aspect about this is now it will also throw a warning if you forget to check if a nullable is actually null. This is a feature I believe is going to come in very handy. Take a look below:

1
2
3
4
5
6
7
8
9
ISomeType notNull = GetSomeType();
ISomeType? mayBeNull; = GetSomeType();

mayBeNull.Execute(); //This will throw a warning (we didn't make sure it wasn't null!)
notNull.Execute(); //This will run fine

if(mayBeNull !=null){
  mayBeNull.Execute(); //This won't throw a warning (because we checked)
}

 

Records

I’m sure most of us have worked with POCOs, creating numerous classes that are simply just going to be used to define a data structure and hold it.  Traditionally this meant writing out a whole new class and defining it’s properties.  Thanks to C# 8.0 we now have records!  With records you can easily and quickly create these “container classes” with one line of code!  For example, instead of having to type of this:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Person: IEquatable<Person>{
  public string FirstName { get; }
  public string LastName { get; }

  public Person(string firstName, string lastName){
    this.FirstName = firstName;
    this.LastName = lastName;
  }

  public bool Equals (Person other){
    return Equals(FirstName, other.FirstName) && Equals(LastName, other.LastName);
  }

  public override bool Equals(object obj){
    return (obj as Person)?.Equals(this) == true;
  }

  public override int GetHashCode(){
    return FirstName.GetHashCode() + LastName.GetHashCode();
  }

  public void Deconstruct(out string FirstName, out string LastName){
    FirstName = this.FirstName;
    LastName = this.LastName;
  }
}

 

Now you can just do this and get the same results!

1
public class Person(string FirstName, string LastName);

Now, I don’t know about you but this is definitely one of the more helpful features that I’ve seen.

 

And many more!

 

These are just two examples that I decided to speak on simply because I find them fascinating but there are many other features in C# 8.0!  Check out the links below to find out more!

3 New C# 8 Features We Are Excited About

Posted by DCCoder in News, Programming, 0 comments
Automated UI testing with Selenium [Part 2]

Automated UI testing with Selenium [Part 2]

In Part 1 we set up Selenium and created our first test showing a browser being opened and then immediately closing.  While that was interesting, I don’t know many people that would be super exciting by that alone.  Since the whole point of these tutorials is UI testing, why don’t we actually test a UI?  In this article we’re going to walk through creating a few more tests to actually log in to a website and verify that only a user with the right credentials can log in, we don’t want just anyone logging in now do we?

Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
Automated UI testing with Selenium

Automated UI testing with Selenium

It seems everyone is making the move towards automated testing these days, and why shouldn’t they? How many times have you been working on a web project and had to constantly retest the same thing over and over simply to verify that it works? Or maybe you’re an analyst on the surface but a code monkey at heart and would like to blend the two together? Enter Selenium.

What is Selenium? Selenium is a browser automation framework. I was exposed to it a few years ago when I was still heaving into web scraping. Selenium is perfect for UI testing as it can easily mimic an actual browser with human input. You can even change the type of browser you want! Ok, so hopefully by this point you’re interested (or maybe not) and want to get started.

Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
C# for Beginners [Part 2]

C# for Beginners [Part 2]

In the previous tutorial we covered some basic structure of a C# program and what it looks like.  I would like to build upon that knowledge and cover some of the basic syntax of C#, some of this may be slightly repetitive from the previous tutorial but it is important to get this basic syntax down. Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
C# for Beginners [Part 1]

C# for Beginners [Part 1]

Ok so yesterday I received a request to do a C# tutorial covering the basics of the C# language. This will be a short multi-part tutorial on the basics of C#. In Part 1 we’re going to cover some basic structure and syntax.  I do not intend to get too in-depth but cover just enough to allow others to be able to start writing simple programs and get comfortable with the language syntax.

Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
SQLite in C#? [Part 2]

SQLite in C#? [Part 2]

Building off the previous tutorial  SQLite in C# [Part 1] we are going to get a little more in detail with SQLite and populate our database with some information that we can pull back out later.

Lets start off with inserting information into our database.  We will do this in a similar fashion as we did creating our database table.  By creating a query string, passing it to a SQLiteCommand, and then executing the command.

Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
SQLite in C#? [Part 1]

SQLite in C#? [Part 1]

I can’t tell you how many times I am working on an application that I want to store data in an organized fashion but I don’t want the dependency of an external server.  Maybe I want to be able to use the application offline?  Or maybe the device I am running it on doesn’t have the ability to hold a database server.

Enter the realm of SQLite.  I have used it extensively in the past with simple Python and PHP applications, but now I figured I’d give a good overview of using it with C# and .NET.

Continue reading →

Posted by DCCoder in Programming, Tutorials, 0 comments
C# 7.1 Update

C# 7.1 Update

Last month Microsoft released Visual Studio 2017.3 and with it C#’s latest minor upgrade C# 7.1!  C#’s latest update includes four new (very useful) features that I am honestly very excited about.

  • async Main method
  • default literal expressions
  • inferred tuple names

Below I’ll go into some detail on each of the new features and show some examples of each, along with how to update your current project to the new C#7.1 update.

Continue reading →

Posted by DCCoder in Programming, 0 comments

Storing Sessions in a database

Most common applications and “home-brew” sites use sessions for storing temporary data as well as authentication. However,
sometimes a developer may want the session to span over multiple domains and or servers, and some may just be very security
conscience. It is common knowledge that session data is stored in a text file on the webserver, however if you are using
a shared server (as most cannot afford dedicated hosting or VPS) then any user on that server may see your session files.
To prevent this, and allow your user’s sessions to span over multiple domains the answer is easy: store the session data in a
MySQL database! Most of you may not be sure how to do this, or may have even been unaway that this is possible, however it is
very easy. When storing sessions in a database PHP makes the work easy for us with the use of a function called
session_set_save_handler(), this function can control the way that sessions are stored, retrieved, destroyed, etc. Continue reading →

Posted by DCCoder in Posts from old site, Security, Tutorials, 0 comments