Posts Tagged ‘dotnet’

Scope_Identity doesn’t return an Int in sqlserver but a Decimal

December 5th, 2011

This is an old item.  Scope_Identity in Sqlserver doesn’t return an Int but a Decimal.  So in Dapper the call is

                var id = conn.Query<Decimal>(...

Honour those who should and here.

VB.Net and lambda

November 30th, 2011

Using the terse lambda syntax used in C# also in VB.Net is also (almost) possible.

Here is a short example:

var nameList =
 myFacade.GetData().
 where( x => x.ID = 42 ).
 select( x => x.Name ).
 ToList();

which in VBNet becomes:

Dim nameList =
 myFacade.GetData().
 Where( Function(x) x.ID = 42 ).
 Select( Function(x) x.Name ).
 ToList()

Note that the periods have to be at the end of the line, otherwise the multi line capability of Vbnet doesn’t work.  This can be remedied by the old trailing underscore of VB.

If you need to set the type of x it is also doable:

Dim nameList =
 myFacade.GetData().
 Where( Function(x As MyClass) x.ID = 42 ).
 Select( Function(x As MyClass) x.Name ).
 ToList()

Singleton in dotnet

November 15th, 2011

Singleton is often considered the simplest pattern but I digress. Except for the simplest cases writing a singleton requires thorough knowledge about the language at hand; one really has to know the locks and ifs and constructors to make a zyzygy.

Below is linked a good article. I don’t consider it as wordy since it really explains the subject in depth in a simple way.

There are 6 examples where 4 can be used. At the bottom of the article is a short explanation if you don’t want to grok it.

http://csharpindepth.com/Articles/General/Singleton.aspx

CompulsoryCat version 1.2

June 30th, 2011

Updated CompulsoryCat to version 1.2

The first big news is the Assemblyname functionality for retrieving a tree of assembly references for an application.

The other big news is that I genereated help files and uploaded too.

http://code.google.com/p/compulsorycat/

Assert.Inconclusive and my present almost-TDD compromise

April 1st, 2011

I am trying out Visual studio unit testing framework, the Microsoft alternative to, for instance, nUnit which was my old preferred testing framework (and possibly still is).

At the same time I am not in a project devoted to Test driven development so I can’t use its religion to write tests first and refuse to show any UI progress to the customer.  Right or wrong – that is how I perceive the reality around me right now.  But I do want to have tests and waiting for them I make sure I have place holders.

My compromise is to set the tests to Inconclusive  (Ignore in nUnit talk).  I guess it will come back to bite me…

What I also do is to make sure I have a test to go with every method.

        [TestMethod]
        public void AddOrUpdateTest()
        {
            Assert.Inconclusive();
            Elvis.Common.Meta.GetMethodName<BLF.Sample>(x => x.AddOrUpdate(null, null));
        }

I used the code from

http://stackoverflow.com/questions/1524290/linq-is-it-possible-to-get-a-method-name-without-a-return-type-via-linq-expressio

but I could probably use CompulsoryCat just as well.

I belive that even adhering TDD this technique is good.  There are so many times I don’t know what the classes and methods will look like before having done a simple implementation.  To write exhaustive tests for a method that will be deleted is a waste.  Better then to make sure the simple code runs and not forget to write the tests by having a yellow Inconclusive flag.

Include other files in config files in dotnet

November 19th, 2010

A seldom used feature of dotnet config files is the possibility to link to another file.  Use configSource=”theotherfile” or file=”theotherfile” as attribute.  An example and comments are found here.

This is old news indeed but every time I use it I tend to google for a while since I cannot remember the keywords.

Example of use:

I have an ordinary app.config file.  It links to connectionstrings.config which keeps my <drumroll/> development connection string.  For creating an installation file I have a normal setup/installation project.  This project takes the file connectionstrings.prod.config and incorporates it in the MSI while renaming it to connectionstrings.config.

Threading in C# by Albahari gratis

November 7th, 2010

Advise: before attempting parallel programming – read up on the subject!

Normally we program by intellisense and the assumption that the name of the method does what we suppose it does.  It can be thought of as lazy, that we don’t grok the framework before we start using it.  But is does seem to work.

This is not the case with parallel programming.  Do it without understanding it and you’re down the slippery slope.

Download the Threading chapter of C#4 in a nutshell by Albahari.  It is gratis.
Read up.  The text is easy to read and well written.

I have read one other part (looking for interface and inheritance and virtual) of the first release and it was also good.  If the rest of the book is as well written I suggest everyone to get a copy.

Exception: Only parameterless constructors and initializers are supported in LINQ to Entities.

October 30th, 2010

I am not sure of the *exact* meaning and reason for this but I found something that can be good to know.  Or not.

return context.NuggetSet.Where(n => n.Name == name).Select(x => new MyClass(x)).ToList();

threw a

Only parameterless constructors and initializers are supported in LINQ to Entities.

When inserting a ToList it worked as suspected/wanted.

return context.NuggetSet.Where(n => n.Name == name).ToList().Select(x => new MyClass(x)).ToList();

My guess is that by calling ToList we losen EF’s grip of the entities and can do what we want with it.

CompulsoryCat 1.1

October 21st, 2010

Just released version 1.1 of CompulsoryCat on google code.

It is a helper lib for getting the names of methods, classes, properties and other stuff out of the methods, classes etc.  It uses lambda or reflection to be as safe as possible.

It is unit tested and has a small exe to show a few examples of its use.  There is also a help file.

Exception: Error 1083: The executable program that this service is configured to run in does not implement the service.

September 30th, 2010

In short: verify that the service name is correct.  Totally correct.

Longer:

Debugging windows services written in dotnet can be hard.  They have no GUI and have to be installed by a semi secret process.  Then they have to be started by a system call.  And if the start fails it is not necessarily possible to uninstall the service without rebooting.  The CL.exe doesn’t work and hacking the registry doesn’t work.

Today I spent hours tracking down a bug that surfaced with a “Error 1083: The executable program that this service is configured to run in does not implement the service.” when starting the service.  In the end I found out that the ServiceName was incorrect.

To be honest I got some clues, in one place it said MyMailer instead of MyMailerService.  I couldn’t be sure if this was correct or not since I had inherited the project and my search turned out nothing so I dropped it.

By (almost) chance I checked into the automagically created MyMail.designer.cs file and noticed this.ServiceName=”MyMailer”.  Which, it turned out, was the culprit.

I learned a trick on the way.  The service has 30 (I believe) seconds to start.  If it doesn’t fulfill it is shot down by Windows.  When I started the process it failed and I got a question whether I wanted to debug it.  Unfortunately it takes more thatn 30 s to open my VS2010.  So I started VS as administrator (necessary in Win7) and loaded the project.  When I then started the service I got the question whether to attach and I did.  Two seconds of work and I got a highlighted row at the exact point.