Implementing a C# interface with an F# object expression

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

Read more of this post

Implementing a C# interface with an F# type

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

We can implement this interface in an F# type as follows:

Read more of this post

Using the Redis NoSql database with .NET Part 18: Redis as a cache engine in .NET

Introduction

In the previous post we looked briefly at data persistence in Redis. We know that the principal storage for Redis is the memory. It also offers two modes of persistence. The first option is called snapshotting which means that the database is saved to disk periodically. The periodicity can be configured in the Redis config file and we can define multiple options depending on the number of new items in memory. The drawback of snapshotting is that if the Redis service abruptly goes down then some unsaved data in memory will be lost. If you cannot afford any data loss then the append-only persistence mode can be for you. It saves the commands in an append-only file which is processed when Redis starts up to restore the database based on the saved commands. Note that append-only will make write operations slower.

In this post we’ll look at caching in the ServiceStack.Redis client. This post also concludes the series on Redis with .NET.

Read more of this post

Dynamically invoking a method with Reflection in .NET C#

Say you do not have access to a .NET assembly at compile time but you want to run code in it. It’s possible to dynamically load an assembly and run code in it without early access.

Here we’ll see how to invoke a method of a type in a referenced assembly.

Open Visual Studio and create a new C# class library project called Domain. Add the following Customer class to it:

Read more of this post

Using the Redis NoSql database with .NET Part 17: persistence

Introduction

In the previous post we looked at the security features of Redis. We saw that Redis doesn’t come with the highly granular security features of a full-fledged relational database like SQL Server or Oracle. It offers password passed authentication where the client needs to authenticate with a password before issuing commands. There’s no separate username and we cannot set granular rules like userA can only access this and this resource. Another security feature is command obfuscation where we can change how a command is called. The CONFIG command can be a “dangerous” one that we don’t want just any user to be able to call. We can instead provide another name, possibly a cryptic one, for it so that CONFIG must be called by that name instead.

In this post we’ll discuss the persistence options in Redis.

Read more of this post

Dynamically invoking a constructor with Reflection in .NET C#

Say you do not have access to a .NET assembly at compile time but you want to run code in it. It’s possible to dynamically load an assembly and run code in it without early access.

Here we’ll see how to invoke a constructor of a type in a referenced assembly.

Normally, if you have a direct reference to an assembly then you can simply initialise new objects using the ‘new’ keyword. In the absence of a direct reference this is not possible.

Open Visual Studio 2012/2013 and create a new C# class library project called Domain. Add the following Customer class to it:

Read more of this post

Using the Redis NoSql database with .NET Part 16: security

Introduction

In the previous post we looked at how to log slow queries in Redis. We can declare the limit using the slowlog-log-slower-than flag that accepts an integer. If a query whose execution time exceeds this limit expressed in microseconds is logged to the slow log. The slow log is persisted only in memory and we can set an upper limit to the number of commands stored there. If this limit is reached then the oldest stored command is removed.

In this post we’ll see what Redis has to offer by way of security.

Read more of this post

Consuming a C# class in F#

F# and C# can work together pretty easily. Say that your domain classes are contained in a C# class library called Project.Domains. Let’s take the following Product class as an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Domains
{
	public class Product
	{
		public Product(string name, int price)
		{
			Name = name;
                        Price = price;
		}

		public string Name { get; }
		public int Price { get; }

		public double CalculateDiscountedPrice(double percentageOff)
		{
			double discount = Price * percentageOff;
			return Price - discount;
		}
	}
}

Read more of this post

Filtering exceptions on exception messages in F#

Here’s an example of pattern matching in a with clause in F#:

let testDotNetExceptions =
    try
        raise (new System.IndexOutOfRangeException("Your list is not large enough"))
    with
        | 😕 System.IndexOutOfRangeException as ex ->
            printfn "Out of range: %s" ex.Message
            0
        | 😕 System.Exception as ex ->
            printfn "Something bad has happened: %s" ex.Message
            0

Read more of this post

Using the Redis NoSql database with .NET Part 15: logging slow queries

Introduction

In the previous post we looked at how to use the Redis messaging feature in the ServiceStack.Redis .NET client. We saw that it was in fact very easy to both send and receive messages with a couple of lines of code. The most important thing to keep in mind is that the subscriber application has a blocking call to the SubscribeToChannels function. It will prevent all other code after it from executing and start watching the declared Redis channel.

In this post we’ll look at how we can log slow queries in Redis.

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.