Tuesday, October 8, 2013

Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'.

I am using MVC4 for while. Together with NuGet it works very well. Last week I updated my libraries for my solution. Running my code and BAM! Exception was thrown:

Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'.

Method not found? So I start searching the internet. Tried multiple solutions but nothing seems to work. In my steps to reproduce, I removed some references, like EntityFramework and OAuth.

To keep the solution short, NuGet seems to be the problem. In my steps to reproduce the problem I downloaded once the latest libraries. However, when NuGet finds out you have the library already, the solution won't be updated. So the version of my DLL was just 4.0.1 instead my latest library had version 5.0.6. After changing my projectfile to use the version of 5.0.6 everything works fine!

Ps. I updated WebGrease and Newtonsoft.Json

Happy coding!

Friday, September 20, 2013

Disable Password Expiry in Windows Server 2008 r2

We have some servers for testing purpose. We don't want to change the password all the time. So we disabled the Password Expiry policy.

How to disable the Password Expiry Policy?
- Go to Start -> Run -> gpedit.msc, this will load the Group Policy Editor
- Expand sections to Password Policy
      = Computer Configuration --> Windows Settings --> Security Settings --> Account Policies --> Password Policy
- Set 'Maximum password age' to 0 to totally disable expiry

Now your password expiry policy is disabled

Wednesday, September 4, 2013

OOP with JavaScript

Object Oriented Programming, short called OOP, can be done on different levels. JavaScript can be made OOP too. Normally you write based on the function what you need. For example:

$(".btn").click(function(){
    alert("Button is clicked");
});

For creating a new class with JavaScript you need to start with this:

var MessageBox = function() {
   //Methods and stuff
};

Now we have created a new class. The name of the class is MessageBox. Now we want to add a public method to the class. Lets call the method AlertDefault. The AlertDefault method will call the alert with a static text: "I am a default message"
For defining a public method you need to add this to the method name. So this will be:

this.AlertDefault = function() {
   alert("I am a default message");
};

But maybe we want to have our custom message to give to the alert. So we want to add an argument to our second method. Lets call this method AlertMessage. The implementation will be almost the same, however the argument is put in the alert.

this.AlertMessage = function(message) {
   alert(message);
};

The complete code will be:

var MessageBox = function() {
   this.AlertDefault = function() {
       alert("I am a default message");
   };
 
   this.AlertMessage = function(message) {
       alert(message);
   };
};

Now we have created our class we want to check if everything is working fine.

First we need to create a instance of the MessageBox. Then we can call the AlertDefault and the AlertMessage.

var messageBox = new MessageBox();
messageBox.AlertDefault();
messageBox.AlertMessage("I am a custom message");

Monday, July 15, 2013

What makes a framework a good framework?

We are developing in different languages. On working days in C# .NET, in spare time in C, PHP, jQuery / javascript or Java. Each language has its own benefits and disadvantages. For each language there are also numerous frameworks. But what makes a framework a good framework?

The definition of a framework is:
Broad overview, outline, or skeleton of interlinked items which supports a particular approach to a specific objective, and serves as a guide that can be modified as required by adding or deleting items.
We as developers don't want to create everything from scratch. Therefor we use frameworks. Boundaries of the application are defined, and can be filled with everything. The developer is responsible for the implementation. You can compare it with an house. The carpenter constructs the skeleton of the house. You as client can choose the stones, the color and fill the rooms.

Why use a framework?

Most frameworks have already solved software problems and coding horrors, have great implementations of patterns and make live easier.
An advantage of using a framework is that you can develop faster. You don't need to think about the setup and can deploy faster.

Well known frameworks are made by companies like: Telerik or DevExpress. They deliver a lot of components to use. Mostly its enough to drag, configure and run. How easy can it be?
We use eXpressApp Framework. An powerful framework which is described by DevExpress:
The eXpressApp Framework (XAF) is a modern and flexible application framework that allows you to create powerful line-of- business applications that simultaneously target Windows and the Web. XAF's scaffolding of the database and UI allows you to concentrate on business rules without the many distractions and tedious tasks normally associated with Windows and Web development. XAF's modular design facilitates a plug and play approach to common business requirements such as security and reporting.
XAF's advantages when compared with a more traditional approach to app development are profound. See for yourself and learn why XAF can radically increase productivity and help you bring solutions to market faster than you've ever thought possible.

Accept with the rules

Because you are using an framework, you need to work within the boundaries of the framework. So when you are searching for a good framework, keep in mind: "Is the framework flexible enough for me?". Each framework has its own implementation and behaviour.
My advise is to separate the business logic from the behaviour.

In the beginning you follow the way of the framework. When the application is growing you will probably get questions for functional changes. Most of the changes would fit within the boundaries of a framework, but after some the more and more changes won't fit and will go over the set boundaries. To be able to go over those boundaries you can use extension methods. But these implementations are not well written code, I call it workarounds.

Are there no disadvantages?

Off course there are disadvantages!
When there is a bug in your application, it could be very hard to find, especially when the bug is actually inside the framework.
You need to keep the framework up to date, and you need to know what the changes are inside the framework. It would not be the first time that you rely on some behaviour in the framework which is changed in a newer version, do you know the impact of changes in the framework you use?

Look to the future!

Ok, I know you can watch what the future will bring. But you be able to take some time before creating your application. You can ask for yourself:
  • What kind of features will / can I implement in the future?
    If you look at the features what you want to create now, the framework fits. But if new features bring your application to an higher level, is this also easy in the framework?
  • Can I easily make changes on components?
    You will definitely use editors of the framework, probably you also will create  your own editors to make your own behaviour, but can you change the behaviour easily?
  • Do I have an escape plan?
    What if the framework you use doesn't fit anymore? Are you be able to move your functionality to a new framework?
  • Does the framework have enough support by community or from the company?
    It is terrible to use an open source framework or a paid framework which is not supported. You won't be able to ask people for help and all your energy is moving from developing to searching.

Experience

We have experience with the benefits and the disadvantages of a framework. If you need advise? Just comment.

Monday, June 10, 2013

Test Driven Development without the tests

For me, designing software without Test Driven Development is just unthinkable. TDD helps me in a lot of ways. It helps me to split large tasks into smaller pieces, it helps me to focus on a small part of the software and the task I am currently working on. It also helps me to keep my code as simple as possible.

TDD recap

A big part of Test Driven Development is writing tests to automatically test the code you have to write. But in order to write code we mostly follow the following steps:
  1. Write a test and see it fail.
  2. Write a little code and see the test pass.
  3. Refactor.
  4. Go back to step 1 to write more code...
By writing the tests first we can concentrate on the task at hand and keep the code as functional as possible. Basically we start using the code before the code exists and we make it possible to make the code easy to use.

By writing just enough code to pass the test, we keep the code small, clean and easy to maintain.

Refactoring is the part where we cleanup the code. We remove duplicate code, make code easier to read and understand by renaming variables to make their intend clear, extract methods to get single responsibility, etc, etc, etc...

TDD without tests

But what when it is not possible to write tests? Sometimes it is hard to think that it is not possible to write tests for some peace of code, but there are quite a lot situations where automated tests are not possible.
A few examples when you can see tests aren't worth writing:
  • During the setup of the test you have to instantiate half the application.
    Most of the times this is an issue when the application or a library you use depends on a lot of configuration and on some huge god classes.
  • You need access to hardware / hardware api's
    This could be anything actually, simply painting to the screen, play audio, send a document to the printer, get data from a scanner..
The first point is very difficult to handle and personally I think the best solution is to try and use an other library, or to decouple as much as possible from this library.
For hardware (api) access it is also possible to create wrappers to hide the real implementation, but still you can't test that the image is correctly displayed by the api. Still I believe it is possible to apply the principles of TDD in these situations.

Lets take a look at the steps we take when performing TDD and how we can apply them when we want to show some character images on the screen.
  1. Write a test and see it fail.
  2. Write a little code and see the test pass.
  3. Refactor.
  4. Go back to step 1 to write more code...
"Writing a test and see it fail" is the most difficult step. First of all let's define what we want to test. Start small and say for example "I want to show a single character bitmap on the top left corner of the screen".
Of course this is only a small task and easy to define whether this test will (visually) pass or fail, but for more complex situations it would be a good idea to draw some mockups.

"Write a little code and see the test pass" is not too difficult, but it could be harder than you think. It is difficult to restrict yourself to write as little code as possible.

The difficult thing about refactoring this kind of code is that you have to run the application time after time to make sure you don't break anything. With bigger applications this can be time consuming, but by creating smaller test applications that show only what you want to test this tasks will be doable.

Conclusion

TDD is a very useful concept and to some extend it could also be applied without tests. The key to success is taking small steps, just like you would do when writing tests.

Get classes based on Interface with Linq

This weekend I was searching for an easy way to get all classes which implement an given interface.
After searching I found an easy way to get the classes by given type. Using Linq getting the following code:
var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
.ToList()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface);

Wednesday, June 5, 2013

First blogpost :)

We, David and Wim, are friends and working together as programmers for the same company. We are .NET developers. We have experience with C, PHP, Java, jQuery / Javascript and always searching for new ideas and frameworks.

In our blog posts we want to share our experience while developing. Have fun and keep our blog in mind ;-)