Friday, December 4, 2015

Debugging return values in C#

Hi,

I found an extremly helpful feature in Visual Studio 2013: showing the value which is returned by a function.
Example:


1
2
3
4
public bool CheckExists(string itemId)
{
   return ItemList.Exists(item => item.ItemId == itemId);
} // <- breakpoint

With the debugger it is hard to find whether the itemId is or is not in the list (if the list has thousands of entries), BUT as shown in the link it is possible to get this value through the "Autos" window or the immediate window in visual studio while having a breakpoint on the "}" in line 4. The good news is that it even works with "$ReturnValue" in the watch window, but in contrary it seems not to work properly for any use case (with all above introduced solutions). I'm pretty sure that this is reasonable if we have a look on the disassambly, but that is out of scope here.
kind regards,
Daniel

Wednesday, December 2, 2015

javascript functions in html elements

Hi,

I was wondering today about the following possibility to declare functions in html:

<html><body>
<div id="divid">content</div>
 

<a href="#" onClick="document.getElementById('divid').setAttribute('style', 'font-weight: bold; color: red; font-size:150%;');" >attack 1</a>
 
<a href="#" onClick="
var x = function() {
 document.getElementById('divid').setAttribute('style', 'font-weight: bold; color: red; font-size:150%;');
};
x();
" >attack 2</a>
 
</body>
</html>

both versions of the link are really scary... (tested in chrome).

So in server generated code even here HTMLEncoding must be considered to prevent XSS-attacks.

kind regards,
Daniel

Thursday, November 26, 2015

Book-Review: Responsive Web Design with AngularJS (Sandeep Kumar Patel)

The book is a low level beginner’s guide to AngularJS and some of its responsive capabilities if you are using AngularJS pure (without twitter - bootstrap). A responsive portfolio-application is built as a show-case to introduce the reader to common problems of the field. Other technologies like SASS and JSON are mentioned and partly shown. The book ends with tool advises like the Chrome-F12-tools and Batarang.
AngularJS topics:
-          Templates and DataBinding (MVW)
-          Partials
-          Router
-          Modules / Controller / Scopes
-          Directives (built-in and custom)
-          Custom Services
-          Events / $Watch
-          Publish-Subscribe with $emit, $broadcast - $on
It was easy to read and with good background information, but I would say that after reading it you are not 100% able to build an AngularJS application responsively (and even not responsively) your own. Further investigations are definitely necessary...

kind regards,
Daniel

Wednesday, November 11, 2015

Helpful error handling in JavaScript

Hi,

today I tried to develop an error handling in javascript (more exact: angularjs, but this is as good as meaningless here) that makes sense and is helpful. The following solution is built and tested in Chrome.

First of all: javascript support try/catch blocks we can use by throwing errors


1
2
3
4
5
try {
  throw new Error('Whoops!');
} catch (e) {
  alert(e.name + ': ' + e.message);
}

(see: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Error )
... but this is not the thing I meant...

I wanted to create a central function (error callback, error output method) that can be used to gather further information during development time (e.g. as an error callback for a bad web service rest request; as a usable function to call if an internal object state is unknown => a situation happens that should never happen).

... so I did the following:

1
2
3
4
5
6
7
8
            $scope.writeError = function (response) {
                if (true) { // DEBUG
                    console.error('error', response);
                    console.trace();
                    debugger;
                    throw new Error('error');
                } 
            };

writeError has an if (line 2) which disables the javascript-console information fast. In angularjs it is easily possible to build this "if" depending on angularjs-constants that can be configured central in a application configuration section.

line 5 sets a coded breakpoint in the javascript console (see: http://stackoverflow.com/questions/10050465/set-a-javascript-breakpoint-in-code-in-chrome / I adapted the jsfiddle to http://jsfiddle.net/hBCH5/70/ ); 3 and 4 writes the data (and all its properties) to the console; line 6 throws an exception that can be caught in code and shows similar information to the console.

Attention: the removal of the throw statement can change the application execution logic if try/catch blocks were used outside.

kind regards,
Daniel

// Update

I should have mentioned 2 things:
- there is window.onerror where you can assign an event handler
- there are frameworks like jsnlog ( jsnlog.com ) that sends the error to a server where they can be monitored centrally (angularjs compatible)

Friday, October 30, 2015

Reporting solutions

Hi,

today I had a look on reporting software... of course I did not consider the hardcore solutions for big corporations like Cognos, tibco, microStrategy,...

After some research I decided to pick the following solutions to dig deeper:
- SAP: Crystal Reports
- Microsoft: SSRS
- Eclipse: BIRT
- tibco: JasperReports
- jsreport
- sealreport
- Pentaho

The free solutions looked mature enough to kick the commercial solutions out of my evaluation.
jsreport seems to have currently fewer features than the others (even if the concept looks quite straight-forward for web-solutions), so I kicked it.
seal-reports is open source, but the full power of the solution will not be unleashed until buying a license (when I understood it right), so I kicked it.

- Eclipse: BIRT
- tibco: JasperReports
- Pentaho

These are exactly the solutions as described in http://opensource.com/business/14/6/three-open-source-business-tools ... Because I want to stay focused on reporting and not on BI I removed Pentaho from the list ( http://www.pentaho.com/ seemed to stress the data integration and the setup of a cube) and choose BIRT because of a lot of opinions on the web like  http://stackoverflow.com/questions/2513534/birt-vs-jasper-reports .

A good comment of the opensource.com-link above introduced the page www.reportserver.net which seems to be a good extension to the BIRT technology.

must criterias for the reporting solution:
- db interface
- pdf output
- web output
- IDE for power-users
- active development / community
- command line support

kind regards,
Daniel

Thursday, October 29, 2015

bootstrap editors

Hi,

after a small break I am back reincarnated as a web developer.

Today I played around with bootstrap trying to change the style of my web-page.

Following resources helped me much
- http://getbootstrap.com/
http://www.bootply.com/
- http://fortawesome.github.io/Font-Awesome/
http://glyphicons.com/

but it didn't feel right to work online (and to collaborate without possibly wanting it).

I found other resources to consider
http://www.layoutit.com/
http://brix.io/

... again web, but there are still the stand-alone, cloud-less, good-old tools like:

commercial desktop products:
http://pinegrow.com/

free desktop products:
http://pingendo.com/


What I did not found are true alternatives to bootstrap itself and in fact I don't even need any. Bootstrap is a really good designed, good working framework which does create good looking pages.

kind regards,
Daniel

Wednesday, June 3, 2015

.NET Crawler Harvest

Alexander Nyquist ( http://nyqui.st/ ) made a cool crawler-component I used once... the solution worked quite good for me...

http://nyqui.st/harvest-a-c-multithreaded-web-crawler
https://github.com/alexandernyquist/Harvest/

in my solution I wrote the page to the filesystem using the OnPageDownloaded and wrote some code for filtering items which redirect to a login page. Here a decision has to be made about using a white-list or a black-list approach. For full-text search purpose media content can be filtered too.

cheers,
Daniel

Wednesday, May 27, 2015

dynamic dispatch using "as dynamic" in c#

I found a new entry for our internal coding guidelines: "don't use dynamic dispatching if not absolutely necessary!".

wikipedia:
...
In computer sciencedynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. Dynamic dispatch contrasts with static dispatch in which the implementation of a polymorphic operation is selected at compile-time. The purpose of dynamic dispatch is to support cases where the appropriate implementation of a polymorphic operation can't be determined at compile time because it depends on the runtime type of one or more actual parameters to the operation.
...
Dynamic dispatch is often used in object-oriented languages when different classes contain different implementations of the same method due to common inheritance. 
...


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dynamic
{
    public interface IInt
    {
    }

    public class A : IInt
    {
    }

    public class B : A
    {

    }

    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            IInt x = new A();
            p.Print(x);
            p.Print((A)x);
            p.Print(x as B);
            p.Print(123);
            p.Print(x as dynamic);

            Debugger.Break();
        }

        public void Print(IInt p)
        {
            Console.WriteLine("IInt");
            //if (p is B)
            //{
            //    Print(p as B);
            //} 
            //else if(p is A)
            //{
            //    Print(p as A);
            //}
        }

        public void Print(A p)
        {
            Console.WriteLine("A");
        }

        public void Print(B p)
        {
            Console.WriteLine("B");
        }
        /*
        public void Print(object p)
        {
            Console.WriteLine("obj");
        }*/

        public void Print(dynamic p)
        {
            Console.WriteLine("dynamic");
        }
    }
}

Output:
 IInt
A
B
dynamic
A

 The reason why line 61 to 65 is commented out is that it is technically not possible to keep both functions in code ... the one with the object parameter and the one with the dynamic (will be translated to object later). At first sight it seems to be elegant to call line 30 and get the interface function and then call line 34 and get the "right" function, but don't forget the difference... you put compiler decisions to run-time, so you trick the compiler in some ways. Don't do that!

 kind regards,
Daniel

sql recursive functions (linked lists in sql)

In modern programming languages it makes sense to work with lists, stacks, dictionaries and other collections. This can be crucial when you want to persist the data into the database, because some considerations have to be made about sorting and stuff like that.

Now implementing a linked list in a table is easy:

table: mylist

  • ID: int (not null)
  • data: nvarchar(50)
  • nextID: int (null)
... more tricky is to walk through the list and find e.g.: the leaf of a corresponding root (seeing a list as a special sort of tree). 

the solution is described at: 

http://stackoverflow.com/questions/16749095/sql-recursive-function-that-gets-all-ancestors-of-an-item

...but the other way round using parent_id instead of a next_id

data_table:
ID       parent_id   name
---------------------
1        2            first 
2        4            second
3        3            third
4        5            fourth
5        -            fifth

the solution looks as follows:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
with parents as 
(
  select ID, parent_ID
  from data_table t
  where parent_ID is not null
  union all 
  select p.ID, t.parent_ID
  from parents p
    inner join data_table t on p.parent_ID = t.ID
      and t.parent_ID is not null
      and t.ID <> t.parent_ID
)
select *
  , parents = '(' + stuff
    (
      (
        select ', ' + cast(p.parent_ID as varchar(100))
        from parents p 
        where t.ID = p.ID
        for xml path('')
      ), 1, 2, ''
    ) + ')'
from data_table t
order by ID

  • line 1 gives the inner statement a name to call it e.g.: more often like in
    with y as (select * from x where id > 12) select * from y, y
    ... here you have a smaller set of data to cross join
  • line 3 to 5 makes a select as a starting point for the recursion (the original records)
  • union all in line 6 adds to the starting point a data set defined below
  • line 7 to 11 was at first sight simply magical to me
    • line 7 selects the columns corresponding to line 2 (required by the union and makes sense with the recursive algorithm) ... the own id and the parent of parent
    • line 8 requests data from the data table itself (the recursion using the name), but
    • line 9 joins parent data to the original data
  • line 13 to 24 uses the "expanded" data evaluated and shows it nicely in an own column using "for xml path".
the expanded result is:
| ID | parent_ID |
|----|-----------|
|  1 |         2 |
|  2 |         4 |
|  3 |         3 |
|  4 |         5 |
|  2 |         5 |
|  1 |         4 |
|  1 |         5 |

this will be transformed to

| ID | parent_id |   name |   parents |
|----|-----------|--------|-----------|
|  1 |         2 |  first | (2, 4, 5) |
|  2 |         4 | second |    (4, 5) |
|  3 |         3 |  third |       (3) |
|  4 |         5 | fourth |       (5) |
|  5 |    (null) |  fifth |    (null) |

...

There is an addition for select statements to overwrite the max recursion depth (in sql server it is default set to 100) to work with "deep" lists or trees.

    OPTION (MAXRECURSION 0)

kind regards,
Daniel

Monday, May 11, 2015

Tool for load tests and performance tests

I needed to make some load and performance tests ... I thought about using the visual studio components, but I realized that I have no studio installed on the machine where I wanted to make my testing ... I needed another tool! Test Studio of Telerik costs 80$ a month ... #dislike

Finally I thought about using an open source tool. After some google search I found some opportunities ( http://blazemeter.com/blog/open-source-load-testing-tools-which-one-should-you-use ), but JMeter seems to be outstanding...

... and it worked for me! The following tutorial with 20 minutes was enough to setup my first easy use case...


kind regards,
Daniel

Wednesday, May 6, 2015

C# Debug-Info

Hi,

a few days ago I found a quite cool feature:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestProject
{
    class Program
    {
        [DebuggerDisplay("Person: '{Name}'")]
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            var persons = new List<Person>(new Person[] { 
                new Person() { Name = "Johnny Galecki", Age = 40},
                new Person() { Name = "Kaley Cuoco-Sweeting", Age = 30},
            });

            Debugger.Break();

            if (Debugger.IsLogging())
            {
                persons.ForEach(x => Debugger.Log(0, "output", x.Name + Environment.NewLine));
            }
        }
    }
}

In line 12 DebuggerDisplay tells the Debugger to show the information "person" and then the person's name. E.g.: if you are running this example application the debugger will break (like setting a break-point) on line 25. When you check the variable using a "watch"-window or "local" you will see that the output is now something helpful instead of the full qualified class name.

Other important features related to Debug-Info:
  • DebuggerStepThrough-Attribute
  • DebuggerBrowsable-Attribute
kind regards,
Daniel

EDIT: check also DebuggerTypeProxy: https://msdn.microsoft.com/en-us/library/d8eyd8zc.aspx 

Tuesday, April 21, 2015

DOS error levels

Today I was in the mood to quit my job and become a potato farmer...
 
I tried to work with batch scripts to create an "easy" install script (which is currently growing to a quite sophisticated all-in-one solution). (The post is related to http://itados.blogspot.co.at/2014/12/windows-start-script-for-applications.html )
 
The "easy" concept was to...
* delete and re-create a hard coded folder on the c: - drive (making always a clean "check-out" from the source path)
* connect to a UNC path on a server
* xcopy the files locally
 
My first problem was that connecting to the UNC path which is shared for the group "everyone", but which was not reachable by "everyone", because "everyone" seems to be "everyone" inside of the domain scope and not "everyone" as I would expect it. There are some related problems which can be solved by tweaking the windows security policy which can be found in secpol.msc at "Local Policies > Security Settings > Network Access: *", but I think it is a bad idea to change these (the infrastructure experts will definitely have an eye on these settings (= problems for me) and this means to open a new gate into your system and be open for new threats).
 
So I created a user on the system locally and gave him a secure password what leads me to my second problem, which was that if the "net use" command has credentials inserted and the credentials contain special characters then an error about "password or user incorrect" shows up. Attention: I copy/paste-d the command to my normal cmd-box and it worked properly (and created completely different output in windows event log on the server)! I used the same statement in a batch script... it failed. Now some magic: using "abc" as a password worked in both cases properly... (I really hate that...).
 
The net use command stores the credentials of this local account. In case these have changed or just to work proper I deleted them before setting clean new ones... in case it is the first time to connect, nothing can be deleted so we get an error message saying "hey, this can't be executed, nothing found to delete". So I printed the user (as an information only) that "something happened here, but don't worry everything is still fine"... using "if %errorlevel% NEQ 0" ( ... ATTENTION: there is an undocumented feature on /DELETE ... the /YES forces the delete of the connections even if the connection is currently open... it do is undocumented in /? of net use!
 
Next problem: the parenthesis of the if has to be on the same line as the if statement in the batch script to identify the following statement as a block which should be executed when the if condition is true. In case this would have made serious troubles a nice goto would make you jump out of the critical section probably to the end of the script (what equals to a "return" in common programming languages).
 
Back to step 1: To delete the folder is an easy call using "rd" or "rmdir" (i believe these are synonyms). Here I have troubles getting an error code and in fact the BUILT-IN COMMANDS have troubles setting the error code right!
http://stackoverflow.com/questions/6500314/do-ms-dos-built-in-commands-return-an-error-exit-code
the answer of stackoverflow user "dbenham" brought me to solution that works: "rd testdir || rem" ... again... mind blowing! you have to pipe the result to a comment to get the errorcode of the original statement set up. W00t?
 
And now to my favorite part... setting and resetting the error level. To copy the application binaries I use "taskkill" to close any running applications (=processes). Resetting the errorcode (because some users really close their application before installing a new version leading to the error "process not found" = code 128) is a common problem and could be accomplished in many different ways like
* verify >nul
* ver >nul
* cmd /c "exit /b 0"
* (call )
* (call)
 
attention: Don't use "set %errorlevel% = 0", because this creates a local variable with the same name, what disables further error handling possibilities, because then the local variable and not the system variable will be evaluated for further checks ( http://stackoverflow.com/questions/1113727/what-is-the-easiest-way-to-reset-errorlevel-to-zero )
 
but none of them work inside of an if statement...
 
taskkill /f /im doesnotexist.exe
echo after taskkill %errorlevel%
if 0==0 (
 cmd /c "exit /b 0"
 echo cmd %errorlevel%
)
echo after if %errorlevel%
 
output:
 
FEHLER: Der Prozess "doesnotexist.exe" wurde nicht gefunden. (german for process not found)
after taskkill 128
cmd 128
after if 0
 
 
meaning ... if you have something like:
kill my process => errorlevel become 128
if folder exists (
  delete folder => errorlevel should become 0
  check error code (HERE THE ERRORCODE STAYS 128 AND THE ERRORCODE OF DELETE FOLDER WILL BE IGNORED)
)
 
the solution here is to reset the errorcode after killing the process and to make an "if folder exists" after deleting the folder and to get the information here... another valid solution is to jump to code parts outside of any ifs using goto.
 
The effect is explained in http://blogs.msdn.com/b/oldnewthing/archive/2006/08/23/714650.aspx who says that an if is executed as a single command what means that something like a preprocessor pushes the values as textreplace into the placeholders. So it does not count what happens inside the if because the text-replace was already done. 

Following example from the link shows it quite good:

C:\>set VAR=before
C:\>set VAR=after & echo %VAR%
before

There seems to be a solution for it with "SETLOCAL ENABLEDELAYEDEXPANSION" (please check the link for further information).

In the end i am not even sure if i am using the errorlevel right, because i am not sure if the %-signs are necessary (both ways seem valid)
 
cmd /c "exit /b 123"
if errorlevel == 123 (
 echo errorlevel 123 %errorlevel%
) else (
 echo else %errorlevel%
)
 
cmd /c "exit /b 123"
if %errorlevel% == 123 (
 echo errorlevel 123 %errorlevel%
) else (
 echo else %errorlevel%
)
 
for this example the next trap came across ... closing parenthesis in echo also close blocks!
 
cmd /c "exit /b 123"
if %errorlevel% == 123 (
 echo errorlevel 123 (%errorlevel%)
) else (
 echo else (%errorlevel%)
)
 
output:
errorlevel 123 (123
else (123)
 
 
now I know why everyone loves powershell (and also for small scripts) and also why install projects or ClickOnce-Deployment have their right to be... (nevertheless why it is cool to build websites without installing stuff)

kind regards,
Daniel

Null propagation in C# 6.0

Today I read ( http://www.volatileread.com/Wiki?id=2104 ) about the null propagation Operator ?. ... a C# 6.0 feature what enables you the possibility to make things like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
string invalidResult = string.Empty;
 
if(this.CurrentUser != null)
{
    if(this.CurrentUser.Name != null)
    {
        return this.CurrentUser.Name;
    }
    else
    {
        return invalidResult;
    }
}
else
{
    return invalidResult;
}

in only one simple statement:


1
return this.CurrentUser?.Name ?? string.Empty;

... semantically "?." means "if not null evaluate property, else return null". 


cool stuff...

kind regards,
Daniel

Tuesday, April 7, 2015

performant vs clean code

Today I read a really good article about development performance vs simple-clean development.
 
http://arne-mertz.de/2015/03/simple-and-clean-code-vs-performance/
 
The article (and its comments) highlights some very good points I definitely agree with.
 
In short:
 
simple is better than performant:
  • before: performance (how fast to do something) != efficiency (how long to do it)
    • if you run 2 miles you can be more performant than a guy walking a single mile,
    • but if you reach the same goal by walking only one mile you are much more effective
  • try to write efficient code before you write performant code because your intention will still be understandable and the code keeps being readable
  • check whether the code to optimize is needed to be optimized (or called rarely, so it doesn't contribute to total run-time)
  • optimization needs tooling to find painful parts in the code
  • optimize the data store first
  • check for other bottlenecks (e.g.: hardware, user input,...) 
  • check for best practices, libraries and patterns
  • use caching instead of recalculating the hard stuff

kind regards,
Daniel

Saturday, March 28, 2015

DB diff tools

made some research on database tools for comparing databases about changes and differences...

free

non-free


The solution of simego seems to be one of these hidden heroes in the universe nobody knows but saves life all day... I really really like this product... 

it supports the following features in the current version/release 3.4.000 (copy from the original page):

  • Fast access to your database without the bloat
  • Edit Table Data
  • Execute Ad-Hoc SQL Queries
  • Create and Edit Stored Procedures, Views, Functions and Triggers
  • Compare Database Schema and Synchronise the Changes or generate a Change Script.
  • Compare SQL Data and Synchronise the Changes or generate a Change Script.
  • Export Data to Xml, CSV and Excel File Formats
  • Automatic Code Generation from SQL Schema
  • Multi Window Layout
  • Cached database registration for fast start up time
  • 64 Bit and 32 Bit versions
  • Export data (CSV File, Excel File, XML File and SQL Script)
  • import
  • compare schema
  • compare data
  • generate code (VB.NET, C#)
  • automation abilities (NANT Build process integration)
kind regards, 
Daniel

Friday, February 6, 2015

find non-printable characters in nvarchar string (T-SQL)

Yesterday I needed to find all characters in an nvarchar which are non-alpha-numeric... long story short... I found no better way than writing my own stored function and walk over the string char by char.

Good example is shown here: http://jamesveitch.com/t-sql-function-replace-non-printable-ascii-chars/

other solutions based on PatIndex: http://stackoverflow.com/questions/1007697/how-to-strip-all-non-alphabetic-characters-from-string-in-sql-server

... the only adaption needed to the function of the first link is in the if clause.

First you don't have to write hard coded numbers... there is a tsql-function called "ascii" which can be used instead (e.g.: ascii('A') => 65) AND is even used in the function to convert the current char to a number... a second function "unicode" does the same, but for unicode (n)chars.

Second: use begin and end statement and check explicitly for null

Third: adapt solution for language (e.g.: german öäüß,...) ...

kr Daniel

Tuesday, February 3, 2015

sql server complex update

Today, I was asked how complex update statements can be executed; so I built the following sample:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
declare @t1 TABLE (a int, b int)
declare @t2 TABLE (a int, b int)

insert into @t1 values (1,2)
insert into @t1 values (2,3)

insert into @t2 values (1,200)
insert into @t2 values (2,300)

update t2 set t2.b = t1.b
from @t1 as t1
inner join @t2 as t2
on t1.a = t2.a

select * from @t1
select * from @t2

in line 1,2 I create a temporary table variable and add some values in the following lines. In line 10 it starts become interesting. Here we update the value b from table @t2 aliased as t2 and overwrite the values with the joined values of table @t1 aliased as t1. This syntax with froms and joins is much more common in select statements, but can be used in update statements as well.

The much worse solution is to create a cursor over @t1 and overwrite @t2 line by line. This solution is officially bad.

kr, Daniel

MongoDB

The last few days I had a look on MongoDB (using windows)...

MongoDB was developed by a company called 10gen and was open-sourced in 2009. The reason they started building a database was simply the lack of usable NoSql databases for their own service. 2013 they renamed their company to MongoDB Inc. to express their new focus. A key player here is Dwight Merriman.

MongoDB itself is a C++ open source NoSQL schemaless (but can be used with a schema) document store (IaaS) storing BSON (Binary JSON) documents with JavaScript as procedure language and good scaling behavior (CAP theorem: partial consistency).

Installation process is well documented and worked for me without any troubles. Best thing is that it can be skipped using mongoDB portable (e.g.: ZWAMP is a portable windows web-stack with mongo). The only thing to do is to create a config file or set --dbpath to a folder with the data and start mongod (daemon). Another nice feature is the ootb http-interface (can be enabled by argument) and the --rest switch which allows basic data querying.

Tools:

  • php MongoDB admin
  • MMS: Mongo Management Service
  • Mongo Shell (ootb)
  • RoboMongo
  • MongoVUE
  • Query Translator ( http://www.querymongo.com/ )
    e.g.: translates 'select * from x where _id <= "3"' to '
    db['x'].find({"_id": { "$lte" : "3" }});'
The following code (php) shows my first tries with mongoDB (I used dBug http://dbug.ospinto.com/ http://sourceforge.net/projects/php-dbug/ for visualization)



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php 
 header('Pragma: no-cache');
 include_once('dbug\dbug.php'); // its open source and a single file to copy http://dbug.ospinto.com/
 if (!class_exists('Mongo')) die("Mongo support required. Install mongo pecl extension with 'pecl install mongo; echo \"extension=mongo.so\" >> php.ini'");

 // returns a connection object to work with
 function Connect()
 {
  try
  {
     return new Mongo('mongodb://localhost:27017', array('connect' => true));
  }
  catch (MongoConnectionException $ex)
  {
     error_log($ex->getMessage());
     die("Failed to connect to MongoDB");
  }
 }

 function ShowDBs()
 {
  $dbs = Connect()->listDBs();
  new dBug($dbs);
  echo "<hr />";
 }

 function CreateDB($name)
 {
  $mongo = Connect();
  $mongo->selectDB($name)->createCollection('__tmp_collection_');
  $mongo->selectDB($name)->dropCollection('__tmp_collection_');
   }
   
 function DropDB($name)
 {
  Connect()
   ->selectDB($name)
   ->drop ();
 }

 function CreateCollection($db, $col)
 {
  Connect()
   ->selectDB($db)
   ->createCollection($col);
 }

 function AddDocument($db, $col, $doc)
 {
  Connect()
   ->selectDB($db)
   ->selectCollection($col)
   ->save($doc);
 }

 function ShowDocuments($db, $col)
 {
  $cur = Connect()
   ->selectDB($db)
   ->selectCollection($col)
   ->find();
  
  foreach($cur as $data) 
  {
   echo "\n<div style='float:left;margin-right:10px'>\n";
   new dBug($data);
   echo "\n</div>\n";
  }
 }

 function CreateSPToAddNumbers($db,$col)
 {
  Connect()
   ->selectDB($db)
   ->selectCollection('system.js')
   ->save(array(
    '_id'   =>                        'addNumbers',
    'value' => new MongoCode('function addNumbers(x, y) { return x + y; }')
   ));
   
 }

 function CallSPToAddNumbers($db, $col)
 {
  echo "<div style='clear:left'/><div style='padding-top:40px'>Calculation: 30 + 12 = ";
  var_dump(Connect()->selectDB($db)->execute(
    'function(x, y) { return addNumbers(x,y); }', 
    array(30, 12)));
  echo "</div>";
 }

?>

<html>
<head><title>mongo test</title></head>
<body>
<?php

 //ShowDBs();
 DropDB('CC');

 //ShowDBs();
 CreateDB('CC');
 // die();
 
 //ShowDBs();
 CreateCollection('CC','x');
 // die();
 
 AddDocument('CC','x', array("_id" => "1", "name" => "John",     "details" => array("born" => "1986", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "2", "name" => "Lenny",    "details" => array("born" => "1981", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "3", "name" => "Frank",    "details" => array(                  "status" => "tired")));
 AddDocument('CC','x', array("_id" => "4", "name" => "Ryan",     "details" => array("born" => "1988", "status" => "tired")));
 AddDocument('CC','x', array("_id" => "5", "name" => "Will",     "details" => array("born" => "1990", "status" => "tired")));

 ShowDocuments('CC','x');

 CreateSPToAddNumbers('CC','x');
 CallSPToAddNumbers  ('CC','x');
?>
</body>
</html>

... the code drops and then creates a db 'CC' (cloud computing) with a collection x with 5 documents then shows the document and the result of an example function.

kr, Daniel

Monday, February 2, 2015

Prototyping (Part 1)

I am currently doing some research on "prototyping". My interests concentrate on desktop applications and how requirements can be validated. In some cases a lack of understanding costs days, weeks, months or years of time. The earlier errors are found or not made the less costs arise to get back on track. So I think some additional costs and time spending is nothing compared to a completely misunderstood requirement (the more important the more expensive).

In this post I concentrate on software found in an - as good as infinite - list on http://c2.com/cgi/wiki?GuiPrototypingTools ...

What I am looking for is a software which:

  • shows screens
  • offers walk-through-prototyping 
  • is optimized for web pages and windows desktop applications
  • can handle code extensions to enrich the prototype
  • uses .NET or JavaScript for enrichment
  • optional: can use photography (e.g.: paper prototypes) as base of work
before creating a list of possible matching tools here a tool for android which inspired me: POP https://popapp.in/ ... it is really amazing. I would recommend to try it if you build mobile apps! ... but I don't, so I keep on looking for an alternative. (See also: http://alternativeto.net/software/pop--prototyping/ )

My top 10 tools from the link list above are (with focus to the mentioned requirements / price-schemas: commercial, single-user license):
unmentioned till here: Big 3rd party vendors like telerik or infragistics built their own prototyping software. It makes sense to use their products when you use their libraries, because the prototypes are nearer to the final result (less imagination needed by the customer).

The prototyping market is currently focusing on app development and web development (clear trend). Enterprise applications (what I think often means desktop applications) are only supported by good established, bigger products. My requirements are not met by any product (found nothing after hours of research and even after looking tons of other listing pages, which are mostly subsets of the mentioned list). 

Some smart guys on the net advised to use paper only, others talk about their experience with pure drawing programs, but in my opinion such prototyping is not close enough to the final product to validate customer needs. Other opinions are to use lightswitch or other RAD tools, but I think they aren't the right way to validate the requirements either. I think this is also a bad idea because you turn from "no code" to "code it all", but it brings us to an important question:

Who should build a prototype and why? 

For validating business requirements I advice a requirements engineer, project manager or management guy. Validating technical questions like: "we want an interface, can we technically get it up and running" will be done by a technical engineer and is completely different to the other situation. I think I will dive deeper into this question in a following post.

Anyway, I think that this time there is a chance to build a product which has unique features on the market and I am looking forward to do something in this area...

kr, Daniel

Friday, January 9, 2015

WPF Master-Detail sync

today I dived into WPF data binding and checked how to create an easy master-detail view. This following sample show my findings (description below):

XAML:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<Window x:Class="MasterDetail.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525" 
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="118" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <ListView Grid.Column="0"
                  Grid.Row="0"
                  Margin="5"
                  ItemsSource="{Binding PersonList}" IsSynchronizedWithCurrentItem="True"
                  SelectedIndex="0">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding Name}"
                                    Width="Auto" />
                </GridView> 
            </ListView.View>
        </ListView>
        <ListView Grid.Column="1"
                  Grid.Row="0"
                  ItemsSource="{Binding PersonList/OrderList}"
                  IsSynchronizedWithCurrentItem="True"
                  Margin="5">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"
                                    DisplayMemberBinding="{Binding Name}"
                                    Width="Auto" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button Grid.Column="1"
                Grid.Row="1"
                Width="100"
                Height="25"
                HorizontalAlignment="Right"
                Margin="0,5,5,5">Close</Button>
    </Grid>
</Window>

Code behind:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MasterDetail
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class Person : INotifyPropertyChanged //,IEditableObject
        {
            #region Name

            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                    RaisePropertyChanged();
                }
            }
            
            #endregion

            public ObservableCollection<Order> OrderList { get; set; }
            public Person(string name, Order[] list)
            {
                this.Name = name;
                this.OrderList = new ObservableCollection<Order>();
                list.ToList().ForEach(x => this.OrderList.Add(x));
            }

            public event PropertyChangedEventHandler PropertyChanged;
            private void RaisePropertyChanged([CallerMemberName]string prop = null)
            {
                if(this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
                }
            }
        }

        public class Order
        {
            public string Name { get; set; }
            public Order(string name)
            {
                this.Name = name;
            }
        }

        public ObservableCollection<Person> PersonList { get; set; }
        public MainWindow()
        {
            PersonList = new ObservableCollection<Person>();
            
            #region set data
            
            PersonList.Add(new Person("Peter Parker", 
                           new Order[]{
                                new Order("spiderman costume"),
                                new Order("comics"),
                                new Order("science books")
                           }));

            PersonList.Add(new Person("Tony Stark",
                           new Order[]{
                                new Order("screw driver"),
                                new Order("tie"),
                                new Order("headset"),
                                new Order("Mobile phone")
                           }));

            PersonList.Add(new Person("Bruce Benner",
                           new Order[]{
                               new Order("shorts")
                           }));

            #endregion

            InitializeComponent();
        }
    }
}

Findings:

  • Setting the datacontext to the codebehind is a bit tricky see line 7 in XAML
  • IsSynchronizedWithCurrentItem makes sense if you use more than 1 control to display a collection. The control is not directly using the list, but it wraps it using a "default" listcollectionview, which has a current item. This property can be used to sync the positions of the different controls. A problem here is, that listcollectionview is a wpf class, which should not be used inside the viewmodel so getting the current item can be a bit tricky and possibly needs to be done in a view-service, which returns a reference to the current item.
  • line 32 is the highlight in this post. First I tried the same with a dot ("."), but there the output window of visual studio told me (eligible) that there is no property of the given name for the observablecollection. What does the trick is to use the current item, which will be requested by using the slash ("/"). So binding to /OrderList binds to the order list of the current item. It is different behavior to a collection-view which automatically binds to the current item.
kind regards, Daniel