Tuesday, September 28, 2021

Change detection in EF

Entity Framework change tracking is often something that doesn't require too much thought from the app developer. However, change tracking and the DetectChanges method are a part of the stack where there is a tension between ease-of-use and performance. For this reason it can be useful to have an idea of what is actually going on such you can make an informed decision on what to do if the default behavior is not right for your app.

Part 1: What does DetectChanges do?
http://blog.oneunicorn.com/2012/03/10/secrets-of-detectchanges-part-1-what-does-detectchanges-do/

Part 2: When is DetectChanges called automatically?
http://blog.oneunicorn.com/2012/03/11/secrets-of-detectchanges-part-2-when-is-detectchanges-called-automatically/

Part 3: Switching off automatic DetectChanges
http://blog.oneunicorn.com/2012/03/12/secrets-of-detectchanges-part-3-switching-off-automatic-detectchanges/

Part 4: Binary properties and complex types
http://blog.oneunicorn.com/2012/03/13/secrets-of-detectchanges-part-4-binary-properties-and-complex-types/

Monday, September 20, 2021

Lambda and anonymous functions

The point here is that whether a function has a name or not is not particularly important compared to whether or not the function is used as data. JavaScript supports named and unnamed lambdas, and it's useful to understand that a named function can be anonymous. If you don't consider a named function to be a lambda, you're missing the point of lambdas. TL;DR - Lambda means "function expression used as data".

Tuesday, October 27, 2020

Entity Framework .Remove() vs. .DeleteObject()

If the relationship is required (the FK doesn't allow NULL values) and the relationship is not identifying (which means that the foreign key is not part of the child's (composite) primary key) you have to either add the child to another parent or you have to explicitly delete the child (with DeleteObject then). If you don't do any of these a referential constraint is violated and EF will throw an exception when you call SaveChanges - the infamous "The relationship could not be changed because one or more of the foreign-key properties is non-nullable" exception or similar.

Entity Framework .Remove() vs. .DeleteObject()

Thursday, December 13, 2018

Get raw query from Elasticsearch NEST client

https://stackoverflow.com/questions/28939022/get-raw-query-from-nest-client
var json = elasticClient.RequestResponseSerializer.SerializeToString(request);
Great for debugging and finding out what json you are sending.

Wednesday, December 12, 2018

DateTime and DateTimeOffset

Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo

DateTime.ToLocalTime

Utc This instance of DateTime is converted to local time.
Local No conversion is performed.
Unspecified This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.

DateTime.ToUniversalTime

Utc No conversion is performed.
Local The current DateTime object is converted to UTC.
Unspecified The current DateTime object is assumed to be a local time, and the conversion is performed as if Kind were Local.

Tuesday, June 5, 2018

ServiceBus Serialization Scenarios

ServiceBus Serialization Scenarios.
Use BrokeredMessage with explicit json to understand whats going on.
E.g. for using in Azure functions
class MyData
{
 public string name {get;set;}
}

// Sender
[FunctionName("Send")]
public static void Send(..., [ServiceBus("%Topic%")] out BrokeredMessage brokeredMessage, ...)
{
 ...
 MyData myData = new MyData();
 string bodyJson = JsonConvert.SerializeObject(myData);
 brokeredMessage = new BrokeredMessage(bodyJson);
 brokeredMessage.Label = "My routing info";
 ...
}

// Receiver
[FunctionName("Receive")]
public static void Receive([ServiceBusTrigger("%Topic%", "%Subscription%")] BrokeredMessage brokeredMessage, ...)
{
 ...
 string bodyJson = brokeredMessage.GetBody<string>();
 MyData myData = JsonConvert.DeserializeObject<MyData>(bodyJson);
 ...
}

If you want to send a test message using Service Bus Explorer in the send message dialog just paste the json e.g. {"name":"Andreas"} into Message -> Message Text and then set Sender -> Body Type to String (i.e. not Stream). Set the Label as well if you want to route the message.
ServiceBus Serialization Scenarios
Messages, payloads, and serialization
Azure Service Bus bindings for Azure Functions

MVC json DTO

If you got a DTO and want camelCase json when returning data.

Is there any benifit in using DataContract and DataMember attributes while working with Asp.net Web API?

Is there a way to alias response model properties in ASP.Net Web API

JSON and XML Serialization in ASP.NET Web API

Thursday, February 25, 2016

Javascript (KnokcoutJS) code that for some reason work in Chrome but not IE?

Had some problems at work where KnockoutJS code worked fine in Chrome but not at all on IE. The problem was ajax caching being done in IE for GET requests.

Observables does not update values

AJAX requests not executing or updating in Internet Explorer? Here's a solution

Wednesday, November 18, 2015

Great article about javascript function declarations vs function expressions

Function Declarations vs. Function Expressions
https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

And there are other reasons to favour Function Expressions?
How did you guess?
a) Function Declarations feel like they were intended to mimic Java style method declarations but Java methods are very different animals. In JavaScript functions are living objects with values. Java methods are just metadata storage. Both the following snippets define functions but only the Function Expression suggests that we are creating an object.

//Function Declaration
function add(a,b) {return a + b};
//Function Expression
var add = function(a,b) {return a + b};


Compare to C# Anonymous functions:

Anonymous Functions (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/bb882516.aspx

Tuesday, December 9, 2014

CQRS

Martin Fowler

CommandQuerySeparation (Martin Fowler)
http://martinfowler.com/bliki/CommandQuerySeparation.html

CQRS (Martin Fowler)
http://martinfowler.com/bliki/CQRS.html

CQRS, Task Based UIs, Event Sourcing agh!
http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/

Microsoft

Reference 1: CQRS in Context
http://msdn.microsoft.com/en-us/library/jj591560

Reference 2: Introducing the Command Query Responsibility Segregation Pattern
http://msdn.microsoft.com/en-us/library/jj591573

Reference 3: Introducing Event Sourcing
http://msdn.microsoft.com/en-us/library/jj591559

Reference 4: A CQRS and ES Deep Dive
http://msdn.microsoft.com/en-us/library/jj591577

CQRS Journey
http://msdn.microsoft.com/en-us/library/jj554200.aspx

Command and Query Responsibility Segregation (CQRS) Pattern
http://msdn.microsoft.com/en-us/library/dn568103.aspx

Event Sourcing Pattern
http://msdn.microsoft.com/en-us/library/dn589792.aspx

CRUD, Only When You Can Afford It
http://msdn.microsoft.com/en-us/library/ms978509.aspx

Greg Young

Stereotypical Architecture
https://cqrs.wordpress.com/documents/a-stereotypical-architecture/

CQRS Introduction
https://cqrs.wordpress.com/documents/cqrs-introduction/

CQRS and Event Sourcing
https://cqrs.wordpress.com/documents/cqrs-and-event-sourcing-synergy/

Task-Based UI
https://cqrs.wordpress.com/documents/task-based-ui/

CQRS provided by Edument

http://cqrs.nu/

http://cqrs.nu/tutorial/cs/01-design http://cqrs.nu/tutorial/cs/02-domain-logic http://cqrs.nu/tutorial/cs/03-read-models http://cqrs.nu/tutorial/cs/04-web-app-integration http://cqrs.nu/tutorial/cs/05-other-matters