Friday, June 10, 2016

Dependency Injection with Windsor and WPF

Hi,

this month I started reading about dependency injection and found out that castle.dynamicProxy (already mentioned in earlier posts) works great with castle.windsor. This might not be surprising but nevertheless after my first project using both nuget-packages I can definitely say that using these has changed my way of work. In this project my classes are shorter, better testable and my interfaces are more proper. (About a year ago I read the book about Clean Code Development by Robert C. Martin. The book is an advertisement for TDD with some pointers about coding style and development mind sets).

One thing to mention about my project using castle.windsor: it supports even more separation of Views and ViewModels of WPF. I used a custom Activator of generic type T (derived from DefaultComponentActivator) to activate my window and asked the windsor container to resolve a singleton viewModel of type T which can be set as the datacontext (override of CreateInstance). I stored the container as a static member of a centrally available class.

So:

  • WPF-Activator<ViewModel>
  • Still consider to set the DesignViewModel in the XAML code directly
  • create a DIProvider 
  • use windsor - installers
  • remove startup-uri in app.xaml and start mainwindow with showdialog
  • prefer dependency injection over service locator pattern
  • use interfaces instead of implementations


kr, D

Sunday, May 29, 2016

Book-Review: AOP in .NET (Matthew D. Groves)

The book is very good to start with Aspect Oriented Programming in .NET (as the title says it). It starts with a general overview about aspects, advice and point-cut to conquer cross-cutting concerns (as it would be described in a book for aspectJ) and adds understandable examples to show where all this stuff makes sense in combination with TDD.

Later it is shown which kind of aspects there are (those: for methods and those: for properties) and on which levels and ways these can be applied. The most important asset of the book is that it really makes it clear that there are different paradigms to make AOP. The first way described is "weaving" which actually changes the il-code in a post-compile step and "dynamic proxies" which create reflection-assemblies on the fly at runtime (the pros and cons are - in my opinion - the most important part of the book).

After reading the book i'm definitely able to write AOP code in .NET, but there are many references to IoC and DI so I would recommend to read a book about these first.

Kr, Daniel

Thursday, May 19, 2016

Castle DynamicProxy

Hi,

I am currently reading a book about AOP in .NET (book review-post will follow soon). I really fell in love with Castle DynamicProxy and it probably works even better in combination with ninject (see: https://www.nuget.org/packages/Ninject.Extensions.Interception.DynamicProxy/ --> my next topic to dive into). The good thing here is that no weaving process is needed as post-build action. This means that if you debug, you debug the actual code and not manipulated code which might not match (or prevent debugging at all).

Crosscutting-concerns will be tackled by method-interception using a proxy:

var service = new ProxyGenerator().CreateClassProxy<Class1>(new Aspect());

  • Aspect must implement IInterceptor. 
  • Class1 should have decorated its functions as virtual to create the appropriate proxy.

Often implemented aspects: logging, security, caching, threading (Invoke), lazy loading, INPC-implementation, exception handling, defensive programming and argument handling, validation, auditing, monitoring, fault tolerance.

kr, d

Backup

Today i wanted to backup my PC. There are 1000 possible ways to. My favorite solution would be: having 5 Dropbox accounts (1 for each device) with unlimited storage... 

Unfortunately this is only possible with a paid team or business license... So... No... Also any other cloud solution costs...

A possible opportunity was owncloud ... an on-premise solution with support for mobile devices. After spending hours installing a wamp-server and owncloud i found out that this can not be hosted on windows (since a couple of version ... Attention there are youtube tutorials for windows -> legacy! No windows support)

I decided to use copy jobs like robocopy or freefilesync for my PC (all directories except: windows and "program files"). This works great...

Last improvement i found is btsync (bit torrent synchronization), which copies directories over the internet. The sync-app is implemented for a lot of devices, so finally I found a way to backup my mobile phones too! yes!

Friday, May 13, 2016

Webserver cassini

I didn't know that the logic of the IIS is part of .net's fcl... Here a project using this: cassini

See: https://msdn.microsoft.com/de-de/library/bb979483.aspx

King regards,
Daniel

Tuesday, March 22, 2016

comma separated values in a single cell (T-SQL)

Hi,

I reviewed my solution back from 2015-05-27 ( http://itados.blogspot.co.at/2015/05/sql-recursive-functions-linked-lists-in.html ). I thought it would be easy to explain a colleague how to put multiple values into a cell based on this article, but it took me over 5 minutes of staring at the code to remember how this all works...

Here an easier example:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
declare @x as table (id int, val int)

insert into @x values (1,1)
insert into @x values (1,2)
insert into @x values (1,3)
insert into @x values (2,1)
insert into @x values (2,2)
insert into @x values (2,4)

select 
  o.id, 
  stuff(
    (
      select ', ' + cast(i.val as nvarchar) 
      from @x i
      where o.id = i.id
      for xml path('')
    ), 1, 2, ''
  )
from @x o 
group by id

kind regards,
Daniel

Wednesday, March 9, 2016

C# switch and double assigned enum values

Hi,

today I wanted to trick C#'s switch-case statement, but failed :-)

I tried to create an enum with 2 values pointing to the same value (yes this do is possible).

    public enum TestEnum : uint
    {
      a = 1,
      b = 1,
    }

Main:
    switch (variable)
    {
      case TestEnum.a:
        Console.WriteLine("a");
        break;
      case TestEnum.b:
        Console.WriteLine("b");
        break;
      default:
        break;
    }
... looking at the second part (only) this code makes perfect sense (as a test-case), but in combination it must be considered that the switch-case statement must find a unique case as a target and resolves the values behind the enumeration.

Compiler Error:

Error CS0152: The switch statement contains multiple cases with the label value '1'

... even if i failed to trick the compiler it is good news that c# is consistent here.

Kind regards,
Daniel