Wednesday, November 27, 2019

Parallax Scrolling

This seems to be old but gold: Parallax Scrolling.
It is an effect where you push content into different layers and scroll or move them in different speed.

You can see a demo here on w3schools: https://www.w3schools.com/howto/howto_css_parallax.asp


The things I found out during research (and that are really new to me) are:

- different speed means also zero speed (static background images are counted as parallax)
- it is not forbidden to create effects on scrolling (so eg: one image can be shown in different situations while the content in the background represents the scenes) AND this is also counted as parallax.
- parallax is still cool :-)



Saturday, April 13, 2019

Over Engineering

Over-Engineering is something most of the developers I know struggle with. Nevertheless, the term is also very subjective. What is over-engineered for developer A might be a perfect strategic step in the right direction for developer B. (in the following section the italic sentences are direct quotes from hackernoon here)

Over-Engineering is subjective and the damage of its subjectiveness increase as the requirements fail to present the full picture of the problem

So to define the term:

Over-Engineering is when someone decides to build more than what is really necessary based on speculation

So if we assume that speculation is the root cause then the following quote is the logical consequence:

To reduce the chances of considering something as Over-Engineering we need to understand the problem and to understand the problem we need to clarify the requirements as much as possible by asking "why"?

But there is more than that... Further Reasons (as described in cloud-foundry on youtube here):

  • consider the data
    • like:
      • how often are things used
      • how often are things changed
      • how long does a process take
      • is the process responsive
      • ...
    • we don't have it (green field, bad data gathering)
      • it might be too early to make a decision for further engineering (refactoring)...
      • will we have the data somewhen?
      • can we ask someone?
        • do we really trust this guy?
    • do we ignore the data and might make bad decisions
    • even if something is crap: 
      • is it worth changing it?
        • return of invest?
      • do we have use-cases that work out better by improving the software
  • unnecessary complexity because of pride
Further reasons (as described in build stuff-youtube here):
  • we are not listening to what we need to deliver (bicycle -> spaceship)
  • frameworks don't deliver value (even if funnier to solve any problem then one specific)
  • business guys are responsive to risk and not preventative so not implementing stuff might be ok for your product owner
  • don't focus on all the things that might go wrong, but recognize that you're still on the happy path (that nothing went wrong right now)

Friday, August 17, 2018

cs-script or compile

Hi,

today I evaluated how easy it is to compile code in the command line and wanted to compare it to C# interactive window.

This is my generic script to compile

@echo off
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /t:exe /out:output.exe *.cs >nul 2>nul
.\output.exe
del output.exe
pause;

Consider, I need a fully blows c# file to write hello world to the console:

Something like
// A Hello World! program in C#. using System; namespace HelloWorld { class Hello { static void Main() { Console.WriteLine("Hello World!"); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } }

(see: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/hello-world-your-first-program )


This is a bit much in comparison to (started in developer command prompt):

command: csi csitest.csx
content:     Console.WriteLine("Hello World!");

We definitely have a winner here... 

kr,
Daniel

Friday, June 15, 2018

using angular-cli for vanilla javascript projects

it is very handy to use the angular CLI using "ng serve --open" especially in combination with vs code... the usage of web-pack under the hood (and many more state of the art stuff) are very nice and implement all needed build steps with no effort at all.

For HTML projects consider to use this template too, but reduce it to the following:

* index.html: remove app-root
* main.ts: remove AppModule import and usage and remove app-folder at all
* use style.css as style sheet file
* add a js file and add it to the scripts part in the angular.json (like src/logic.js)


Monday, May 28, 2018

c# string concatenation beyond StringBuilder

I compared in a race with 1 million iterations the execution times of

  1. StringBuilder with an Action call to "Append" a single character (as string)
  2. StringBuilder with a function call to "Append" a single character (as string)
  3. direct string = string + character (as string)
  4. StringBuilder with an Action called to "Append" a single character (as string) but initialized with 1M+1 size
  5. StringBuilder with a direct call to "Append" a single character (as string) initialized
  6. like 5 but a single character is added as a character.

Here the results in ms:


                V5 13
                V4 20
                V6 29
                V2 35
                V1 57
                V3 839343

As we see in V4/5/6 the initialization of a string builder without the need of reallocation is the absolute performance gain by far. The indirection using an action I would consider to be a cheap operation (see 4 vs 5). 

Using a StringBuilder, in this case, is a MUST as we can see it on the scoreboard. 

Interesting is that adding a character instead of a string doubles the execution time.

Comparing 1 and 4 makes it obvious how expensive the allocation of further storage is. 

In V3 garbage collection is called constantly.

While these results differ heavily (especially the numbers of the execution time) from run to run even on the same machine, they differ even more on different machines. Nevertheless, the statements derived are valid.

windows services - installation / deinstallation toggle script

Tried to find a script that installs a windows service if it is not installed and is capable to deinstall it if needed. Finally, I created myself the following:

sc query SERVICENAMEif %errorlevel% equ 0 (                echo uninstall                goto uninstall) else (                echo install                goto install)

Check if it is installed: if so uninstall, if not install. 

in :install call:

  • sc create SERVICENAME binpath=%cd%\Service.exe displayname="the service" start=demand 
    • if you need to set credentials for the startup then set: obj and password
    • %cd% is the current directory
  • sc start SERVICENAME


in :uninstall call:
  • sc stop SERVICENAME
  • sc delete SERVICENAME
    • delete can already be called even if the service is still in "stopping" state so in a script it works the right way even with some timing issues

Saturday, May 26, 2018

Printing web pages to pdf

As an extremely lazy person I started automating a lot of tasks... so many that I started writing the results (successful execution / failed execution) into the database to query for errors. Then I started writing the logs and execution times into db. It became so huge that we wrote a dashboard application to check state.

Today I want to forward this dashboard to some colleagues without granting them access to the platform... solution: mailing a pdf. I asked myself if there is a way to convert the html page into a pdf easily and found the following solution with chrome. The following snippets shows a batch script for windows.

@echo  %time%
@"C:\Users\{user}\AppData\Local\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --print-to-pdf=c:\{outputFolder}\output.pdf file:///{pathToIndex}/index.html
@echo  %time%
@pause


Keep an eye on the fact that the screenshot is taken after onload is executed which can take several seconds.


Saturday, May 5, 2018

minimal .NET core web api

Hi,

today I tried to create a minimal .NET core web API application which returns a result as a foundation for a micro-service approach...

Here the code that works (pasted into main):


            WebHost.CreateDefaultBuilder(args)
                .Configure(app =>
                {
                    app.Run(async context =>
                    {
                        using (StreamWriter writer = new StreamWriter(context.Response.Body))
                        {
                            await writer.WriteAsync(await Task.FromResult("this works..."));
                        }
                    });
                })
                .Build()
                .Run();



Thursday, May 3, 2018

sql server local time and utc time

Hi,

there is a nice way to make a local DateTime value to a UTC DateTime value. First, I thought I can add a fixed amount of hours, but this is unfortunately not ok because change summer- to winter-time made the calculation invalid. So I found a very nice trick on StackOverflow...

http://sqlfiddle.com -> SQL Server

select getdate() as localtime, getutcdate() as utctime, datediff(hour, getdate(), getutcdate())

We can check the local time and the UTC time and add the delta as the offset to the local time. This is unfortunately only true if the local time value is in the same timezone as you are currently, but for my case it worked...

kr,
Daniel

Saturday, February 3, 2018

changing command prompt in windows 10

Hi,

today I tried something nerdy and wanted to improve my cmd - ux in windows (using windows 10).

First of all the useless stuff:

color a0

introducing a nice matrix look and feel.

Probably you remember the quote "follow the white rabbit", so I added a bunny in the prompt:

prompt $_       _$_      (\\                                  $_       \$B$B$_     __(_";$_    /    \$_   {}___)\)_                                             $_$P>

with cls (clear screen) you see the bunny up there and it appears after any command entered... funny for about 2 minutes... then really annoying... removed it...

sticking to the original output and add date and time turned out to be more useful

prompt $D $T$_$P$G

to much space, to few info...

prompt $D $T $C%USERDOMAIN%\%USERNAME%$F$_$P$G

more info!

ipconfig|for /f "tokens=12 delims=: " %G in ('findstr /i ipv4') do ( echo %G ) > ip.txt
set /p ip=<ip.txt
prompt $D $T $C%USERDOMAIN%\%USERNAME%$F IP: %ip%$_$P$G

linux / bash style:
prompt %username%@%userdomain%: $P $T$$

but best experience with simple stuff

prompt $P $T$G

kr,
Daniel

Tuesday, December 19, 2017

T4 Generation Templates VS 2017

Hi,

It took me a lot of time to find good resources about T4 (even if there are no real alternatives built-in for visual studio). Here a small template to create multiple files easily...

I used following resources exists using archive.org:

Further resources:


kr,
Daniel

<#@
template debug="false" hostspecific="true" language="C#" #><#@

assembly name="System.Core" #><#@

import namespace="System.Linq" #><#@
import namespace="System.Text" #><#@
import namespace="System.IO" #><#@
import namespace="System.Collections.Generic" #><#@

output extension=".log" #><#
CleanOutputFolder();

string propertyName = "Prop";
           
Enumerable.Range(1,3).ToList().ForEach(id => {

StringBuilder builder = new StringBuilder();
builder.AppendLine("public class Data" + id.ToString("00"));
builder.AppendLine(" {");
Enumerable.Range(1,3).ToList().ForEach(propId => {
builder.AppendLine(" public int "+propertyName+propId+" { get; set; }");
});
builder.AppendLine(" }");

CreateClass("Data"+id.ToString("00")+".cs", builder.ToString());
});


#><#+
public void CleanOutputFolder()
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFolder = Path.Combine(templateDirectory, "output/");
foreach(var file in Directory.GetFiles(outputFolder))
{
File.Delete(file);
}
}

public void SaveOutput(string outputFileName)
{
      string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
      string outputFilePath = Path.Combine(templateDirectory, "output/", outputFileName);
      File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());

      this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}

public void CreateClass(string fileName, string content)
{
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
<#= content #>
}

<#+
SaveOutput(fileName);
}
#>


Monday, December 18, 2017

Dumping objects in c#

Hi,

I had a look at different possibilities to .net objects. First I was like: "just serialize that stuff and everything will be fine", but the more I thought about it, the more I realized that I needed more...

First of all: LinqPad did a great job with its functionality to dump objects, which was very inspiring to me...

see:



... so what might be a good solution to embed into our code...?

Good fitting solution seemed to be ObjectDumper (unfortunately you can find different solutions with the same name in google):

ObjectDumper (version: stone-age)



ObjectDumper (version: old)



ObjectDumper (not a single-file-solution anymore, Options to ignore fields)



ObjectDumper (Finally the current version without a single commit in the last 2 years... same version as CodePlex?)



So I started further testing with the github version:


  • installation: copy Dumper.cs and DumperOptions
  • DumperOptions can be removed easily... only 1 line must be patched (the rest is just handover of the options object into the recursive call)


IEnumerable<FieldInfo> fields = XY ? Enumerable.Empty<FieldInfo>() : type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

you can replace XY by a constant or a static field, so even the github version can be a single file solution again.

  • alternatively nuget is available (as already mentioned)
  • its a simple recursive solution based on GetFields and GetProperties
  • can read private data using reflection but can not handle static members
  • Every reference type is pushed into an object of class ObjectIDGenerator generating making it easy to make cross-references of objects (e.g.: backing field and property)
    • not a feature I really need, so I tried to delete it, but found out that it is a perfect solution to remove circular stepping into the code, so it is not only for printing purpose.
For me it works perfectly.

The following Test shows a first impression:

        public class Data
        {
            private int secret = 15;
            private string secondSecret = "15";
            public static string staticData = "123";
            public int Public => secret * 2 + 3;
            public string Something => "something";
            public int StoredData { get; set; }
            public string StoredData2 { get; set; }
            public Data()
            {
                this.StoredData = 1;
                this.StoredData2 = "1";
            }
            public Data2 data2 = new Data2();
            public class Data2
            {
                public bool IsData { get; set; } = false;
            }
        }

Dumped:

#1: data [DumpTester.Program+Data]
{
   properties {
      Public = 33 [System.Int32]
      #2: Something = "something" [System.String]
      StoredData = 1 [System.Int32]
      #3: StoredData2 = "1" [System.String]
   }
   fields {
      secret = 15 [System.Int32]
      #4: secondSecret = "15" [System.String]
      <StoredData>k__BackingField = 1 [System.Int32]
      <StoredData2>k__BackingField = "1" [System.String] (see #3)
      #5: data2 [DumpTester.Program+Data+Data2]
      {
         properties {
            IsData = False [System.Boolean]
         }
         fields {
            <IsData>k__BackingField = False [System.Boolean]
         }
      }
   }
}
you see:
  • no static info
  • AutoProperties with Backing-Fields
  • For reference-auto-properties you see the linkage between Prop and Field (see #3)
  • ValueTypes have no reference
  • Types in the system namespace are not dumped


kr,
Daniel

Thursday, November 9, 2017

benchmarking

Found a cool benchmarking nuget-package. It is called nbench and unfortunately I am not sure if the project is dead or not, but nevertheless the current state can add to the functional unit test the non-functional performance test which is quite cool.

It can be used for .net and .net core projects with a variety of possibilities. This project fills one of the gaps of a nightly compile run and might be helpful in many cases to keep quality high.

see: https://github.com/petabridge/NBench

IIS development and exceptions

In a production environment it is often a good idea to hide some errors of e.g.: a web-page to be able to work on the root cause of the error in the background that finally the customer will not identify some strange behavior as a bug.

Exactly the opposite is true for developers and testers. Find bugs! Don't cover them with any UI candy or stuff... and exactly in this trap I fell into...

see customErrors on msdn: https://msdn.microsoft.com/de-at/library/h0hfz6fc(v=vs.110).aspx

and for web api 2 see: https://msdn.microsoft.com/en-us/library/system.web.http.filters.exceptionfilterattribute(v=vs.118).aspx
https://docs.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling

see also AppDomain.UnhandledException, Application.ThreadException / Application.SetUnhandledExceptionMode (for WinForms), DispatcherUnhandledException (for WPF), Application_Error (IIS Global asax)

Be careful to disable stuff that makes it hard to find bugs during testing :-) 

Tuesday, October 31, 2017

mstest with test cases

I am still wondering why there is not better support in mstest for test-cases as it is in nunit like described in http://nunit.org/docs/2.5/testCase.html ... nevertheless I wrote a code snippet which makes life bit easier... I assemble the data to test into the test-name which is not that bad as it might sound, because in some cases e.g.: testing mathematical functions you want to see the input data which is used directly and so you would write it into the name anyway... so we can use the name of the test and reflect over it like:

         public static int[] GetIntArrayFromName()
        {
            StackTrace t = new StackTrace(skipFrames: 1);
            var frames = t.GetFrames();
            string name = frames.First().GetMethod().Name;
            return name.Split('_').Skip(1).Select(x => int.Parse(x)).ToArray();
        }

so if you call this function inside a test-method which is called something like Test_1_2_3 you will get an array like new[]{1,2,3} which might fit quite well.

            [TestMethod] public void Add_1() => AddMethod(GetIntArrayFromName());
            [TestMethod] public void Add_1_2() => AddMethod(GetIntArrayFromName());
            [TestMethod] public void Add_5_4_6_3_1() => AddMethod(GetIntArrayFromName());
            [TestMethod] public void Add_7_5_1_3_4() => AddMethod(GetIntArrayFromName());
           
the rest is copy / pasting which is easy...

just to mention it, there do is some kind of support using the data source attribute in mstest... see: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.datasourceattribute.aspx and https://stackoverflow.com/questions/21608462/how-to-run-unit-test-with-multiple-datasource

Saturday, October 21, 2017

swagger integration into webapi project (Part 2 - .net core)

While trying to setup a test web-api solution in .net core I was wondering whether the swagger integration even works for .net core with the swashbuckle nuget and yes... it does work!

I used swashbuckle.aspnetcore (with .swagger / .swaggergen / .swaggerui)

the only things I had to add in startup.cs were:

ConfigureService:

  • addmvc
  • addmvccore
  • addapiexplorer
  • addswaggergen
    • swaggerdoc
    • includexmlcomments
Configure:
  • usemvc
  • useswagger
  • useswaggerUI
    • SwaggerEndpoint
done.

Every created controller will from now on be listed in swagger UI.

Friday, October 13, 2017

generic execution of stored procedures in c# accessing sql server

for generic execution of stored procedures I found some helpful links to generate code:

https://stackoverflow.com/questions/20115881/how-to-get-stored-procedure-parameters-details
https://raresql.com/2014/01/18/sql-server-how-to-retrieve-the-metadata-of-a-stored-procedure/
https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-describe-first-result-set-transact-sql

... but in fact I want to pass in generic data without much of validation before hand, because I want to keep it RAD (rapid application development) and test it with integration tests.

Nevertheless with sqlfiddle.com we can validate that:
  create procedure t1(@x int) as
    select 1 as resultValue
  go

can be executed using (e.g.):
  exec dbo.t1 3;

parameters can be queried using
select 
   'Parameter_name' = name, 
   'Type'   = type_name(user_type_id),
   'Nullable' = is_nullable,
   'DirectionOut' = is_output,
   'Length'   = max_length, 
   'Prec'   = case when type_name(system_type_id) = 'uniqueidentifier'
              then precision 
              else OdbcPrec(system_type_id, max_length, precision) end, 
   'Scale'   = OdbcScale(system_type_id, scale), 
   'Param_order'  = parameter_id, 
   'Collation'   = convert(sysname,
                   case when system_type_id in (35, 99, 167, 175, 231, 239) 
                   then ServerProperty('collation') end)  ,
   system_type_id, user_type_id
  from sys.parameters
  where object_id = object_id('dbo.t1')
 
  order by param_order

(first) result record set meta data can be queried using
   SELECT * FROM sys.dm_exec_describe_first_result_set ('exec dbo.t1 3', NULL, 0) ;  


So execute a stored procedure and retrieve a datatable can be achieved using the code from:
https://stackoverflow.com/questions/25121021/generic-execution-of-stored-procedure-in-csharp

    public DataTable RunSP_ReturnDT(string procedureName, List<SqlParameter> parameters, string connectionString)
    {
        DataTable dtData = new DataTable();
        using (SqlConnection sqlConn = new SqlConnection(connectionString))
        {
            using (SqlCommand sqlCommand = new SqlCommand(procedureName, sqlConn))
            {
                sqlCommand.CommandType = CommandType.StoredProcedure;
                if (parameters != null)
                {
                    sqlCommand.Parameters.AddRange(parameters.ToArray());
                }
                using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand))
                {
                    sqlDataAdapter.Fill(dtData);
                }
            }
        }
        return dtData;
    }

this link shows an easy way to map datatables and datarows to objects

https://www.exceptionnotfound.net/mapping-datatables-and-datarows-to-objects-in-csharp-and-net-using-reflection/

(things dapper is doing for us in general).

So this opens up a lot of opportunities for strongly typed argument objects (or an on-the-fly generated instance from a json-string) and output handling with a list of strongly typed instances mapped by datarows. There only needs to be a mapping between class and procedure name AND a mapping between argument fields and parameter names.


swagger integration into webapi project

In the (let's say) "early days" of .net's webapi the controllers and its operations could be listed (with the option to try the operation) using the nuget https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/ which is currently out of maintenance (I believe) because the last update was in february 2015 which is 2,5 years ago.

During some research I found a perfect alternative which seems to be the more or less official successor: https://www.nuget.org/packages/Swashbuckle .

There is a perfect tutorial from redgate related to swashbuckle at: https://www.red-gate.com/simple-talk/dotnet/net-development/visual-studio-2017-swagger-building-documenting-web-apis/

It works in 5 minutes and allows to generate REST API Clients which means perfect fit between client and server.

Things I changed (after installing the nuget):

  • c.DocumentTitle
  • c.IgnoreObsoleteActions
  • c.IgnoreObsoleteProperties
  • c.IncludeXmlComments

    with the function from the redgate blog-entry (add xml documentation in properties)

    protected static string GetXmlCommentsPath()
    {
                return System.String.Format(@"{0}\bin\webDemo.XML",
                    System.AppDomain.CurrentDomain.BaseDirectory);
    }

I don't needed to adapt the global.asax file from the root folder (and which does not work in a sub-folder which is very logical afterwards, but took me an 1 hour of research to find the bug).

In the global.asax (application_start) I still have:

  • simple-injector init (see: http://simpleinjector.readthedocs.io/en/latest/webapiintegration.html
  • GlobalConfiguration.Configure((config) =>
    {
      ((HttpConfiguration)config).MapHttpAttributeRoutes();
    });



kr,
Daniel