1 |
$('#modalWindow').find('iframe').contents().find('body').focus().blur() |
Always run Visual Studio as administrator on Windows 10
You might be working with a program that always require administrator privileges, such as a service listening on a port. Up to Windows 10, you just right clicked the devenv exe, and checked “always run as administrator” in the compatibility tab. But in Windows 10, the compatibility tab is missing for the Visual Studio exe. Why? No idea. But here’s how to fix it.
To just run the shortcut as admin : right click on the sortcut > in the “properties” tab, click “advanced”, then check “always run as administrator”. Simple, fast, easy.
If you want to run Visual Studio as administrator also when opening a .sln or .csproj file, it’s not so easy. Here are the logical, coherent, and easy to find steps to fix this: (labels translated to english from french, sorry if it doesn’t match)
- Open the start menu, type “troubleshoot”, and open “check your computer state and solve problems”
- On the bottom left, click “troubleshoot compatibility problems”
- Click on “advanced” on the left then “run as administrator”
- Click again on “advanced”, then uncheck “automatically fix”, then “next”
- Search for Visual Studio in the list, then “next”
- Make sure “fix the program” is checked, then “next”
- Select “troubleshoot the program” (bottom option)
- Check “the program requires additional rights”, then “next”
- Click “test the programe” then “next”
- Select “save the parameters for the program” then “close”
Easy, logical, simple. Thanks Microsoft!
Git-svn and branches
I’m using git-svn to leverage the power of git while the rest of the company prefers the simplicity of svn.
I have created a local branch, intending to merge it in the trunk/master when I’m done, but it turns out it’s much more complicated than expected. So, I want to commit it on svn.
I already have checked out the full repository.
The most simple solution I have found; all other solutions fiddle with the git config files, and I don’t like it:
- Checkout the branch: git checkout mybranch
- Rename the local branch: git branch -m mybranch-temp
- Create a remote branch “mybranch” in SVN using Tortoise SVN
- Fetch the SVN repository to get the new branch: git svn fetch
- Check that the new SVN branch has been fetched: git branch -r (or git branch -avv )
- Get back to the master branch: git checkout master
- Checkout the remote branch: git checkout -b mybranch-svn origin/mybranch
- Rebase my local branch into the local svn branch: git rebase mybranch-temp
- Commit the branch in svn: git svn dcommit ; use --dry-run to check the branch it will commit to, because sometimes it still commits to the trunk (I haven’t found out why).
Unit testing MongoDB queries
When writing unit tests, it’s common to stop at the database access layer.
You might have a “dumb” data access layer that just passes stored procedure names and parameters to the SQL database, for instance.
It’s usually very hard to test this layer, since it requires either that your build server has access to a SQL Server instance, or that a SQL Server is running on the build server.
Plus, we’re getting out of unit tests and are entering integration tests, here.
Using a more advanced tool like Entity Framework, in order to test your complex EF queries, there are usually methods to insert fake data into a fake container, like Test Doubles, InMemory, or Effort.
Using MongoDB, you might encounter the same problem : how do I test that my complex queries are working ?
Here for instance, I get the possible colors of a product; how do I know it works using unit tests?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public IEnumerable<Color> GetColors(string productId) { var coll = db.GetCollection<BsonDocument>("products"); var aggregate = coll.Aggregate(new AggregateOptions { AllowDiskUse = true }) .Match(new BsonDocument { { "ProductId", productId }, { "Color", new BsonDocument("$ne", "") } }) .Unwind(new StringFieldDefinition<BsonDocument>("Color")) .Group(new BsonDocument { { "_id", "$Color" }, { "Count", new BsonDocument("$sum", 1) } }) .Sort(Builders<BsonDocument>.Sort.Descending("Count")) .Limit(100); return aggregate .ToList() .Select(doc => new Color { ColorValue = doc["_id"].AsString, Count = doc["Count"].AsInt32 }); } |
MongoDB has an “inMemory” storage engine, but it’s reserved to the (paid) Enterprise edition. Fortunately, since 3.2, even the Community edition has a not-very-well-documented “ephemeralForTests” storage engine, which loads up an in-memory Mongo instance, and does not store anything on the hard drive (but has poor performances). Exactly what we need!
Before running the data access layer tests, we will need to fire up an in-memory instance of MongoDB.
This instance will be common to all the tests for the layer, otherwise the test runner will fire up new tests faster than the system releases resources (file and ports locks).
You will have to extract the MongoDB binaries in your sources repository somewhere, and copy them besides your binaries on build.
The following wrapper provides a “Query” method that allows us to access the Mongo instance through command-line, bypassing the data access layer, in order to insert test data or query insertion results.
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 |
public class MongoDaemon : IDisposable { public const string ConnexionString = "mongodb://localhost:27017"; public const string DbName = "test"; public const string Host = "localhost"; public const string Port = "27017"; private readonly string assemblyFolder; private readonly string dbFolder; private readonly string mongoFolder; private Process process; public MongoDaemon() { this.assemblyFolder = Path.GetDirectoryName(new Uri(typeof(MongoDaemon).Assembly.CodeBase).LocalPath); this.mongoFolder = Path.Combine(this.assemblyFolder, "mongo"); this.dbFolder = Path.Combine(this.mongoFolder, "temp"); // re-create db folder if it exists if (Directory.Exists(this.dbFolder)) { Directory.Delete(this.dbFolder, true); Directory.CreateDirectory(this.dbFolder); } this.process = new Process(); process.StartInfo.FileName = Path.Combine(this.mongoFolder, "mongod.exe"); process.StartInfo.Arguments = "--dbpath " + this.dbFolder + " --storageEngine ephemeralForTest"; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.Start(); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public string Query(string query) { var output = string.Empty; var procQuery = new Process { StartInfo = new ProcessStartInfo { FileName = Path.Combine(this.mongoFolder, "mongo.exe"), Arguments = string.Format("--host {0} --port {1} --quiet --eval \"{2}\"", Host, Port, query), UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }, }; procQuery.Start(); // read query output while (!procQuery.StandardOutput.EndOfStream) { output += procQuery.StandardOutput.ReadLine() + "\n"; } // wait 2 seconds max before killing it if (!procQuery.WaitForExit(2000)) { procQuery.Kill(); } return output; } protected virtual void Dispose(bool disposing) { if (disposing) { // dispose managed resources } if (process != null && !process.HasExited) { process.Kill(); } } } |
We’re using the
IClassFixture interface of xUnit to fire up a MongoDaemon instance that will be common to all our tests using it.
It means we need to clean up previously inserted test data at each run.
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 |
public class MongoDataStoreTests : IClassFixture<MongoDaemon> { private readonly MongoDaemon daemon; private readonly DataStore sut; public MongoDataStoreTests(MongoDaemon daemon) { this.daemon = daemon; this.sut = new DataStore("localhost", 27017); // cleanup previous tests data this.daemon.Query("db.products.drop()"); } [Fact] public void Colors_are_counted() { // arrange : insert test data var id = "test"; var nb = 5; for (int i = 0; i < nb; i++) { var color = string.Format("{0}{0}{0}", i, i, i); var data = "{ ProductId: '" + id + "', Color: '" + color + "' }"; this.daemon.Query("db.products.insertOne(" + data + ")"); } // act var result = this.sut.GetColors(id); // assert result.Should().HaveCount(nb); } } |
There you have it: a kind-of-easy way to test your Mongo data access layer.
Nested sets references
Just bookmarking a few things here.
- The “base” reference to understand hierarchical sets vs nested sets
- A few practical examples ; there is a part 2 and a GitHub
- A neat query to convert a traditional hierarchy (with a parent id) to a nested set ; found from this post which unfortunately uses the HierarchyID of SQL2008
- Speaking of which, there is a HierarchyID data type in SQL2008. Neat, but makes it (even) harder to switch to another DB.
- In a more front-oriented way, here is a javascript library to handle drag & drop of items in a nested list. It’s based on JQueryUI, but it’s pretty good.
Modify copy/paste format depending on target
Sometimes you want to copy tabular data, and paste it differently depending on the target. For instance, you want CSV when you paste in your text editor, and HTML when you paste it in an email.
Fortunately, some nice fellow has created a helper class to handle all that for you. And it’s even available on GitHub!
To sum it up, the
Clipboard class has a SetDataObject method. A DataObject can get multiple contents (with different DataFormats) through multiple calls of the
SetData(string, object) method.
So basically, you should create a DataObject, then call SetData once with the HTML, and once with text. The trick is that the HTML needs special formatting to work.
Start a WPF application in the notification area with Caliburn.Micro
Sometimes you’re writing a quick utility app that you wish to start directly in the notification area (near the clock), and not display anything on startup.
It’s actually very simple using Caliburn.Micro.
You probably already have customized your application bootstrapper already. All you have to do is modify the OnStartup method from DisplayRootViewFor<T> to displaying a custom tooltip. Here I’m using the Hardcodet.NotifyIcon.Wpf Nuget package:
1 2 3 4 5 6 |
protected override void OnStartup(object sender, StartupEventArgs e) { TaskbarIcon tbi = new TaskbarIcon(); tbi.Icon = Properties.Resources.sql_icon; tbi.ToolTipText = Properties.Resources.ToolTip; } |
It seems obvious in retrospect, but it took me a while to find this, because I don’t modify the app bootstrapper often, so I forgot about this method.
Don’t forget to add behavior to this icon! Double-click, context menu…
I have created a Caliburn.Micro + MahApps template app if you wish a starting point : https://github.com/cosmo0/Caliburn.MahApps.Metro.Template
Mapping DataTables to domain objects
It’s always a pain to work with DataTables and DataSets. You’re always typing these pesky column names, it doesn’t necessarily represents the actual data model (if there are nested data), there is no type safety until runtime, it’s pretty hard to use Linq on your results… the list goes on.
Fortunately, there is a simple solution: AutoMapper. It’s very simple to cast a DataTable to a list of objects:
1 2 3 4 5 6 7 8 |
public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } List<Person> people = AutoMapper.Mapper.DynamicMap<IDataReader, List<Person>>( sourceDataTable.CreateDataReader()); |
Mocking static methods
I’m working on a pretty old application with no unit tests whatsoever, and I’m trying to write some as I add features and fix bugs.
After successively finding out about MS Fakes, then that it’s only included in VS2012+ Premium/Ultimate and very hard to include in a continuous integration chain, I was pretty stumped until I found this, which works incredibly well.
Another option to transform the static method into a static Func or Action. For instance.
Original code:
12345678 class Math{public static int Add(int x, int y){return x + y;}}You want to “mock” the Add method, but you can’t. Change the above code to this:
12345 public static Func<int, int, int> Add = (x, y) =>{return x + y;};Existing client code doesn’t have to change (maybe recompile), but source stays the same.
Now, from the unit-test, to change the behavior of the method, just reassign an in-line function to it:
123456789 [TestMethod]public static void MyTest(){Math.Add = (x, y) =>{return 11;};}Put whatever logic you want in the method, or just return some hard-coded value, depending on what you’re trying to do.
This may not necessarily be something you can do each time, but in practice, I found this technique works just fine.
C, C++ and C++/CLI cheat sheet
No explanation of the basic concepts, just a memory help when you don’t use C/C++ frequently, or if you’re a beginner with some bases.
First question: why pointers? Answers: because functions arguments are passed by value in C and C++. Because arrays and strings are crazy in C.
How to read pointers
Here is a handy way of remembering how pointers syntax works.
In variable declaration
Go from the right to the left of the declaration, from the innermost parenthesis, going back to the right when hitting “)”
- Read the name of the declaration
- * is “a pointer to”
- [] is “an array of”
- () is “a function returning”
- Finally, read the type
int *p[]; reads “p is an array of pointers to int”.
int *(*foo())(); reads “foo is a function returning a pointer to a function returning a pointer to int”.
In variable usage
Variable usage reads from left to right.
- Determine “get” (read variable) or “set” (write variable)
- * is “the value pointed to by”
- & is “the address of”
- [i] is “the element at position i”
- Read the variable name
*p = 5; reads “set the value pointed to by p”.
x = &i; reads “get the address of i”.
printf("%i", *foo[0]); reads “get the value pointed to by the element at position 0 of foo”.
C pointers
Manipulating a variable through a pointer is called dereferencing. It is used through an indirection expression.
Manipulating pointers through variables is called decaying.
Given an integer:
1 |
int i = 42; |
- Create an pointer:
123int* p = &i;// orint *p = &i; // preferred syntax - Get the memory location of the variable:
12345printf("%i", p);// orprintf("%i", &i);// orpritf("%i", &*p); // get the address of the value pointed to by p - Get the value of the variable:
12345printf("%i", i);// orprintf("%i", *p);// orprintf("%i", *&*&*&*p); // if you want to be stupid - Set the value of the variable:
123i = 42;// or*p = 42; - Setting a pointer to null allows to assign it to a variable later:
1234int *p = NULL;if (p) {// use *p}
C arrays
Arrays mostly can’t be manipulated directly, but rather through pointers.
The variable
array is, for most intents and purposes, a pointer to the first element of the array (until it’s incremented).
Given an array of integers:
1 |
int array[] = { 45, 67, 89 }; |
- This doesn’t really creates an array. It creates 3 integers side-by-side, and stores the memory location of the first one.
- array == &array == &array[0]
- When you pass an array as an argument to a function, you really pass a pointer to the array’s first element, because the array decays to a pointer.
- Incrementing a pointer to an array really moves the pointer to the next element of the array (the pointer is incremented by the size of the type of the pointer).
1array++; // now the "array" pointer points to the second element. - The subscript operator (the
[] in
array[0]) has nothing to do with arrays themselves, you can also use it on a pointer:
123int *ar_p = array;printf("%i", ar_p[0]); // outputs the memory location of the first elementprintf("%i", *ar_p[0]); // outputs the value of the first element
C structures
Given a structure:
1 2 3 4 5 6 |
struct foo { char name[10]; int result; }; struct foo f; // note you have to specify "struct" in the declaration |
- Accessing members of the structure through the variable:
12f.result = 42;printf("%i", f.result); // outputs "42" - Accessing the members through a pointer:
1234struct foo *p = f;(*p).result = 42;// orp->result = 42; // using a pointer-to-member operator
C++ references
C++ is much less in love with pointers than C, and prefers using reference variables. They are mostly used in the same way as pointers, but their address can’t change (they always points to the same variable).
The main usage of a reference variable is for function parameters, to pass the variables by reference (by default it’s by value), which allows to not use pointers.
Given an integer:
1 |
int i = 42; |
- Create a reference to a variable with int &r = i; ; this is called binding a reference to an object
- You do not need to dereference a reference to access the variable’s value:
12345678i = 42;// orint &r = i;r = 42;// should generate about the same machine code asint *p = i;*p = 42; - To make sure the arguments are passed by reference in a method:
12345678void foo(int &bar) {bar = 42;}// call it with the variable itselfint i = 0;foo(i);printf("%i", i); // outputs 42 - On the other hand, the same method using pointers must be passed the variables by reference in the call:
123456void foo(int *bar) {*bar = 42;}// call it with the references to the variablefoo(&i);
Managed instances
Instances of managed objects should be used through handles ( ^ , also called “hat”). It can be read “is a handle to the instance of the managed class”. Then, you use the handle as if it was the actual instance.
Note that, even though you don’t have to use managed objects in C++/CLI, you have to declare instances of managed objects with a handle.
1 2 3 4 5 6 7 8 9 |
public ref class Foo { int result; // native member System::String^ name; // managed member }; // while the convention for pointers is to prefix "*" to the pointer name, // the convention for handles is to suffix "^" to the class name Foo^ f = gcnew Foo(); printf("%s", f.name); |
Managed pointers
Because of garbage collection (which changes memory addresses), you can’t just use a regular C/C++ pointer to a managed objects. The managed pointers are called interior pointers. A good overview can be found here.
The syntax for interior pointers is very… declarative:
1 |
interior_ptr<int> p = &f->result; |
Native pointers are automatically converted to interior pointers (the inverse is not true):
1 2 3 4 5 |
void foo(interior_ptr<int> i) { *i = 42; } foo(&f->result); // passing a native pointer to the method |
As with pointers in C++, usage interior pointers is not recommended, and you should rather use managed references.
Managed references
A managed reference is called a tracking reference and uses % instead of & .
1 2 3 4 5 |
void foo(int %i) { i = 42; } foo(f->result); |
Managed pointers and references have some weirdness, for which this SO post goes into further details.