My Software Notes

Useful things I discover

Dispatching and Binding

leave a comment »

This is just a quick note on definitions:

Binding – determining which signature (method name + parameters) should be called.

Dispatching – Figuring out which implementation of a signature to call.

Example:

    public interface IFoo
    {
        void MyMethod(int id);
    }

    public class Foo : IFoo
    {
        public virtual void MyMethod(int id)
        {
            //whatever
        }
    }

    public class Bar : Foo
    {
        public override void MyMethod(int id)
        {
            //whatever else
        }
    }

    public class Tester
    {
        public void TestMe()
        {

            var array = new IFoo[] { new Foo(), new Bar() };

            foreach(var item in array)
            {
                item.MyMethod(42);
            }
        }
    }

How do you determine which MyMethod in the foreach loop is  to be called?

Binding – match the signatures – MyMethod(int)

Dispatching – figure out which implementation (Foo.MyMethod or Bar.MyMethod) to call

Static Binding or Dispatching – figure it out at compile time.

Dynamic Binding or Dispatching – figure it out at run time.

 

Written by gsdwriter

November 2, 2016 at 7:42 am

git push hangs for no apparent reason

leave a comment »

Out of the blue, I got a hang when I did a push to a git repo.  I’ve no idea what caused it because it had never happened before and, of course, this time I was in a hurry.

The good news: I found the solution. Thanks to Pedro Liska who hit the same problem and found a solution:  git push hangs when windows git client pushes to TeamFoundation Git Server

The most recent version of git, as I write this, is 2.5. So, what are you waiting for? Go install it: git Downloads

Written by gsdwriter

August 27, 2015 at 10:38 am

Posted in git

Encrypting and Decrypting web.config sections

leave a comment »

Another thing I do infrequently is encrypt and decrypt web.config sections. I end up going back to a script I wrote ages ago to find the command lines.

So just to save time, here they are:

Encrypt a web.config section

“c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe” -pef sectionName sitePath

E.g.,

“c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe” -pef “connectionStrings” “c:\websites\mysite”

Decrypt a web.config section

“c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe” -pdf sectionName sitePath

E.g.,
“c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe” -pdf “connectionStrings” “c:\websites\mysite”

These command lines must be run on the web server and must be run in a console with admin privileges.

Written by gsdwriter

February 19, 2015 at 11:55 pm

Posted in IIS

Covariance and Contravariance. What do they mean again?

with one comment

I don’t have to deal with covariance and contravariance very often, so every time I do I forget which is which. So, just for my own benefit, here is a note describing them:

Covariance and contravariance are terms that refer to the ability to use a less derived or more derived type than originally specified.

Covariance – Enables you to use a more specific type than originally specified

E.g., in generics

We have a base type called “Feline” and we have a derived type called “HouseCat”.

IEnumerable<HouseCat> cats = new List<HouseCat>();
IEnumerable<Feline> gods = cats;
//Note: felines have a very high opinion of themselves :)


Contravariance – Enables you to use a less derived type than originally specified.

E.g., in generics

Using the same base and derived types as above.

IEnumerable<Feline> gods = new List<Feiline>();
IEnumerable<HouseCat> cats = gods;


References:

Covariance and Contravariance (C# and Visual Basic)

Covariance and Contravariance in Generics

 

Written by gsdwriter

January 19, 2015 at 12:05 pm

Posted in .NET, Languages

Entity Framework: Contains() (aka ‘Like’) is case insensitive for SQL Server queries

with one comment

Just a note to self.

The Contains method on String when used in an Entity Framework “where” clause is case insensitive.

Example:

Assume the “NoteContent” column on the table “Notes” is a varchar column. If I run the following Linq query against the database where db is a DbContext then I get the same result every time.

var num = db.Notes.Where(n => n.NoteContent.Contains(“NoTe”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“NOTE”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“note”)).Count();

All produce the same value for “num”.

I’m making this note to self because I keep forgetting and because I keep seeing people do this:

var someString = “whatever”;

var num = db.Notes.Where(n => n.NoteContent.ToUpper().Contains(someString.ToUpper())).Count();

And, to quote Led Zepplin, “It makes me wonder.”

Written by gsdwriter

December 2, 2014 at 9:53 am

Posted in .NET, Database, LINQ

Forcing Visual Studio to produce an XML Serializers DLL

leave a comment »

A friend needed the Xml Serializers dll, the one named something like “ClassLibrary1.XmlSerializers.dll”, so he went into the project properties in VS and set “Build > Generate serialization assembly” to “On”.  But nothing happened.  No XmlSerializers dll was created.

We hunted round for an answer and came across this: Generating an Xml Serialization assembly as part of my build.

Quick answer:

After setting the above project property you must go into the project file in your favorite text editor and add this:

<SGenUseProxyTypes>false</SGenUseProxyTypes>

 

just after the line:

<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>

 

You should end up with something like this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
    <SGenUseProxyTypes>false</SGenUseProxyTypes>
  </PropertyGroup>

 

Do that for every build configuration where you need the serializer.

Hope that helps.

Written by gsdwriter

July 28, 2014 at 11:39 am

Entity Framework Generates Weird SQL

leave a comment »

If you ever see Entity Framework code that looks something like this:

SELECT
1 AS [C1],
CAST(NULL AS int) AS [C2],
CAST(NULL AS int) AS [C3],
CAST(NULL AS varchar(1)) AS [C4]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
WHERE 1 = 0

and your EF LINQ query was nothing like it (and who’s would be?), then you probably have a

somelist.Contains(x.Value)

in your query and the collection “somelist” is empty.

I hit this today and was scratching my head until I found this:  

LINQ to Entity Framework submits meaningless query to SQL Server for Contains(empty array)

Hope that helps someone.

 

Written by gsdwriter

July 17, 2014 at 5:14 pm