Introduction to ASP.NET Core part 12: data annotation of view models

Introduction

In the previous post we went through how to insert a new Book entity using a form. We used a view model for this purpose. Two different Create action methods take care of the insertion process in the Books controller. One accepts HTTP GET requests and only presents an empty form with the various book insertion view model properties. The corresponding view has a form that’s built using a Html helper which takes care of setting up the form with the correct naming conventions so that the view model properties can be correctly populated. We also extended our rudimentary layered architecture to simulate that the Book entity is saved in a data store. Finally we also mentioned the usage of the anti-forgery token attribute which guards against a cross site request forgery attack.

In this post we’ll look at how attributes on our attributes can help us refine our view models and how they are rendered in the view.

Read more of this post

Data type conversion in F#

F# has much the same primitive data types as C#. Here are some examples:

let integer = 123
let floating = 23.
let dec = 24M
let bool = true
let text = "This is some string"
let character = 'c'

Occasionally we may need to convert one primitive type to another.

Read more of this post

Infix operators in F#

F# makes the distinction between “prefix” and “infix” functions. The word prefix means that something comes before something else. Prepositions in many Indo-European languages, like “in”, “for” and “on” in English are examples of prefixes, they are placed in front of another word, most often a noun: in the house, at the station, for that purpose.

Prefix functions are how we most often define and call functions in F#. The function name comes first which is then followed by the function arguments.

Here’s a function that adds two numbers:

let addTwoNumbers x y = x + y

Unsurprisingly we call the above function as follows:

Read more of this post

An overview of grouping collections with LINQ in .NET

Introduction

The LINQ GroupBy operator is one of the most powerful ones among all LINQ operators. It helps us group elements in a collection in various ways and lets us control the element and result selection process. However, with its many overloads and Func parameters the GroupBy operator can be a bit difficult to understand at first. At least it is more complex than say the Min() and Max() LINQ operators.

This post will go through the GroupBy operator and many of its overloads with a number of examples.

Read more of this post

Introduction to ASP.NET Core part 11: inserting a new Book in a form

Introduction

In the previous post we continued our exploration of MVC in ASP.NET Core by building a details page for the books. We can now click a link for each Book view model in the table and view some more details about the selected book. We also rearranged the code and introduced rudimentary layers of a controller, a service, a domain and a repository for our Book entity. Then we exposed ourselves to the first example of a HTML helper in the Index and Details Razor views with the anchor tag builder. HTML helpers are very useful tools in Razor that help us build dynamic HTML. If you used Razor views before then this is nothing new to you.

In this post we’ll continue on the same path as in the previous part. We’ll build a view to insert a new book entity. We’ll see a couple more HTML helpers to make model binding easier.

Read more of this post

Randomly rearrange a string in .NET C#

Say you’d like to randomly rearrange the contents of a string, i.e. put the constituent characters into new, random positions. The following method can help you with that using a combination of LINQ and Random:

private static string RearrangeString(string startingPoint)
{
	Random num = new Random();
	string rand = new string(startingPoint.
		OrderBy(s => (num.Next(2) % 2) == 0).ToArray());
	return rand;
}

Let’s test it with my name:

string myName = "Andras Nemes";
string myNewName = RearrangeString(myName);

So my new name can be something like “drsNeeAna ms” or “AdaNmesnrs e” or even “ArsNmenda es”, I haven’t decided yet.

Filip Ekberg drew my attention to the following alternative solution:

string rand = new string(startingPoint.OrderBy(x => Guid.NewGuid()).ToArray());

View the list of posts on LINQ here.

Convert a dynamic type to a concrete object in .NET C#

Dynamic objects in C# let us work with objects without compile time error checking in .NET. They are instead checked during execution. As a consequence we can’t use IntelliSense while writing the code to see what properties and functions an object has.

Consider the following Dog class:

public class Dog
{
	public string Name { get; }
	public string Type { get;}
	public int Age { get; }

	public Dog(string name, string type, int age)
	{
		Name = name;
		Type = type;
		Age = age;
	}
}

Read more of this post

Introduction to ASP.NET Core part 10: the details page and more on view models

Introduction

In the previous post we looked more closely at routing in .NET Core MVC. There are two ways two declare the routing rules: imperatively via Startup.cs or declaratively in the controllers using attributes. The routing rules can be distributed with this solution. General routing rules like the Default route can be placed in Startup.cs, whereas controller and action method specific rules can be declared in the controllers directly.

In this post we’ll continue building on our demo application. We’ll add a details page for the books and we’ll also see our first Razor HTML helper method in action.

Read more of this post

Embedded functions in F#

F# allows us to declare a function within a function. The embedded function will only be visible within the encasing function. Indentation is important in this case as the embedded function can also span multiple rows.

Consider the following function:

let calculate x = 
    printfn "Your input parameter was %i" x
    let embedded y z = 
        printfn "%s" "Running the embedded function"
        let a = (x + y) * 2
        x + y + a
    let finalRet = embedded 5 4
    printfn "The result is %i" (finalRet)
    finalRet

let calculateRes = calculate 5

Read more of this post

Function composition in F#

F# lets us build new functions that consists of other functions using the composition operators >> and <<. The direction of the arrows determines how the individual constituent functions are linked.

Consider the following simple functions:

let incrementByOne x = x + 1
let triple x = x * 3
let halve x = x / 2
let double x = x * 2

A common feature of each function is that each accepts a single parameter and has a return value. The input and return types are also identical. These functions are good candidates for function composition.

Here’s how to do it:

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.