Gang of Four Design Patterns

The Gang of Four are the authors of the book, “Design Patterns: Elements of Reusable Object-Oriented Software“. These patterns are grouped as follows

Creational Patterns

Structural Patterns

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy

Behavioural Patterns

  • Chain of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor

References

Upload File MVC Core

.Net Core 2x

Upload and save some file.

FileUploadViewModel

1
2
3
4
public string Foo { get; set; }

[Display(Name = "Some File")]
public List<IFormFile> FormFiles { get; set; }

FileUploadController

1
2
3
4
5
6
7
8
9
10
11
12
13
// POST: FileUpload/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("PackageType,FormFiles")] FileUploadViewModel fileUploadViewModel)
{
if (ModelState.IsValid)
{
var postedFile = fileUploadViewModel.FormFiles[0];

using (var memoryStream = new MemoryStream())
{
postedFile.CopyTo(memoryStream);
...

View Create.cshtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form asp-action="Create" enctype="multipart/form-data">
<div class="form-group">
<label asp-for="Foo" class="control-label"></label>
<input asp-for="Foo" class="form-control" />
<span asp-validation-for="PackageType" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FormFiles" class="control-label"></label>
<input asp-for="FormFiles" type="file" multiple>
<span asp-validation-for="FormFiles" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Upload" class="btn btn-primary" />
</div>
</form>

References

Colab

Colab Notebooks. Colaboratory is a Google research project created to help disseminate machine learning education and research. It’s a Jupyter notebook environment that requires no setup to use and runs entirely in the cloud.

Shortcuts

List of most useful commands :

RUNNING

  • Ctrl + ENTER : Run current cell
  • Shift + ENTER : Run current cell & select next cell or create new one if last
  • Alt + ENTER : Run current cell & create new cell
  • Ctrl + F9 : Run all cells in notebook

HANDLING CELLS

  • Ctrl + M + A : Create new cell above current cell
  • Ctrl + M + B : Create new cell below current cell
  • Ctrl + M + M : Transform current Code cell into Text cell
  • Ctrl + M + Y : Transform current Text cell into Code cell
  • Ctrl + M + P : Go to previous cell

References

JSON Serializers

System.Text.Json

Ships with .net core 3.0 - new hotness <3

1
2
3
4
public string ToJson(object obj)
{
return JsonSerializer.Serialize(obj);
}

Json.NET

Was integrated into ASP.NET even though it was 3rd party, I think its called Middleware. This lib is pretty much the de facto standard for JSON Serialization (Probably won awards, free beer & stuff!)

1
2
3
4
public string ToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}

System.Runtime.Serialization.Json

Class: DataContractJsonSerializer

An older, Microsoft-developed serializer that was integrated in previous ASP.NET versions until Newtonsoft.Json replaced it. Needs more code and sucks, don’t use it :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public string ToJson<T>(object obj)
{
var json = "[]";
var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
using (var memoryStream = new MemoryStream())
{
dataContractJsonSerializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
using (var streamReader = new StreamReader(memoryStream))
{
json = streamReader.ReadToEnd();
streamReader.Close();
}
memoryStream.Close();
}
return json;
}

System.Web.Script.Serialization

I remember using this ages ago in Web Forms and AjaxServer.asmx.cs files - I stopped using it after somebody showed me Json.NET

1
2
3
4
5
6
var list = new List<string>();
list.Add("some string value");

// slower built in 'JavaScriptSerializer'
var json = new JavaScriptSerializer().Serialize(list);
return json;

References