The MadMan is back.

 

More Madness

 

When I read my last post I must say I really must have had a boost of optimism and god-like energy over me back then, eh?

Books to be written, 3D art to be made, Allegorithmic products to review, Python programming, Blenders Game Engine, Modo stuff.. Phew!

Well. I have actually done something. I won’t present anything here today, but some things stayed and some things did not.

I’m still into writing the book series, that’s one. (But it will be a bit postponed)

What I’m still into is off course Blender! It has gotten a LOT of updates the last seven months.
I’m still willing to create a tutorial series on the BGE (Blender Game Engine) which we all know is programmed with Python. Actually A LOT of Blender is programmed with Python now. It’s just the low level bits and pieces left in hard core C / C++ (?asm?).

I was planning on starting on this in a couple of weeks and off course post it on this blog, if it is any interest for it.

As some of you know, I’m a software developer, mainly using C# and C++, but I’ve also been using some Python through the years.

Lately I’ve been keen on learning to program GAMES. What kind of games? No idea. Just games. Fun programs that can include gaming elements, like “gamification” or something.

So I’ve downloaded the latest Unreal 4 Engine, but that was totally overkill for me. I need to know the basics first.

Even Unity seems a bit cryptic to me sometimes. But that’s mostly because of the horrific implementation of C# / .Net they’ve chosen. Goes against all good usage patterns.

So now I’m going to test out Blenders Game Engine, to see if I can make any sense of that.

Along the way will be some tutorials and some Python code for those interested.

 

Also I’ll look into some rendering with Cycles, Thea Render and LuxRender…

 

Until next time…

Blender Logo

 

 

 

 

 

Follow-Up: Reasons to NOT only learn .Net / C#

A modern developer should master at least 3 languages!

A while ago I was all exited about finally “getting it” in sense of the more advanced parts of C#, like Generics, Events, Delegates, and Lambdas. So I wrote a post about 18 reasons you should use C# / .Net / Mono.  it kinda sounded like I’d found a salvation in it or something.

Well, I did not. I was just “on a roll”!

I’m often on these rolls. This is what gets me ahead of the competition and it’s what get’s developers with master degrees in Computer Science and programming  asking ME for help while at work from time to time. That’s wicked cool, but it has its downsides.   logoLispSpillerGlossy

I seldom sleep more than 4 hours per night, because I just HAVE to finish up a stupid project so I can understand a concept in a programming language I probably never going to use, like Cobol or something. But who knows right?

This brings me to the main theme of this blog post:

I’ve been interested in programming for a long time. Long before I started to work as a developer, I snuck in some developer tasks when working as a network and security administrator, and when working as a server and system administrator.
I managed to convince my boss that I really needed this and that book  or course so we could do this and that so much better. It worked all the time.

My job back then was basically sitting in my office with a self-made monitoring system that alerted me if some fiber link was down or a router to a branch office went bananas, something that didn’t happen very frequently. So, my responsibility was to make sure the systems ran 24/7, and that was that.
I make it sound a bit easier than it was from time to time, but the point is that I had a lot of time to study programming!

I started learning the C Language from the ubiquitous K&R book that I still keep in sight for the kicks emacs6-256of it. This was the first standardized version of C, so it had some quirks to put it mildly, but when you understood it, nothing could beat it! At least not in terms of efficiency and performance… Time to market is a nother story.
I was doing a good deal of scripting as part of my job, for automating mass installations, upgrades log-on scripts and those things, but that was in VBScript! Can you believe it? VBScript! I hated it.

So I started to replace many of the scripts with Perl and Python using the Win32 libraries, something that was way cooler.

Then I got interested in cross-platform programming in Java, because I missed a real GUI from the stripped out Tk I managed to get out of Perl and Python, and it had to run on both Windows, Linux and FreeBSD servers. While I probably would get a compile-time error writing “Hello World” in Java today I thought it was fun while it lasted.

Then the real shit hit the fan. I decided to change jobs and work as a full-time developer. My responsibilities where diverse and involved working with Legacy Borland C++ code and some C# 2.0 written in C++ style. So I had to get really serious about learning C++ and C#. The first months I went to work, did what I could, then I wrote down everything I didn’t know how to do.
When I got home I thought it to myself through books, tutorials and Googling so I could fix it the next day.

This went on for almost a year, and I was totally up to my neck in deadlines and trouble.
It wasn’t really my vision of having a job doing what I liked most.
But somehow I managed to get by and new projects came along that demanded the same level of commitment.
That’s the price to pay for adding a bit extra on the resume…the-ruby-mine-logo

Today I feel a lot safer about my skills in the “major languages” C, C++ and C#, but I’ve realized that I’ve only been doing one kind of paradigm type programming and that is the Object Oriented type of programming.

So I’ve now started to learn two new languages (or three actually), namely Common Lisp, Erlang and TCL/Tk.
What I discover each day is that the new things I learn about the more functional approach to programming could have saved my ass plenty of times if I’d known some of the same techniques when working with the projects I barely got through earlier,
I could have done many things so much simpler and more elegantly.

So my advice to all aspiring or maybe even more to all the mounted old dogs that refuses to learn new tricks is to actually do just that. Learn at least one purely functional language, one strictly OOP and one that blends it all together, like C++ or Objective-C if that’s your cup of tea. Plus I’d invest in learning one strictly interpreted language like Python or Ruby, as it makes prototyping and easier tasks, well, so much easier!.

erlang

Put a NoSQL database like MongoDB on top of this and you have an incredible toolbox for handling almost any programming task thrown at you. Word.

PS! Don’t forget those that  work hard for free to bring us some fantastic tools to develop cool things with! 

Get a  Member Sponsorship at the Free Software Foundation Today! 

fsfButton     <– LINK!

These guys are Funding the GNU Project.

Until next time, The MadMan.

Using LINQ and EntityFramework, Measuring and analyzing the LINQ effectiveness

Some Entity Framework Components

When you use something like the extensive “Entity Framework” or any other ORM (Object Relational Mapping) tool, the process of writing sophisticated database driven applications becomes a lot easier.
It eliminates most of the data access code used to be required to create object-based data applications, and contains tools that makes it possible to create object-oriented data applications from an existing database, by using .edmx files that describes the CLR (Common Language Run-time) objects and these can be “extracted” from any existing relational database that the Entity Framework has a driver for.

This enables developers to program directly against data types defined at the conceptual level, presented as regular object instances.

But.
The responsibility to create clean queries and dispose the resources that’s being used, are just as important as it was in more “old fashioned” database connected applications that does all the SQL manually.

Many people assume that, as long as the application compiles and runs without complaining loudly, everything is probably done correctly, right?
This is .Net we’re talking about, so if something was wrong I would probably get a warning or something, right?

Sadly life is not that simple in this case.

The risk of introducing data-access “anti-patterns” in your code that may decrease the performance of your application and greatly slow down your organization’s database server is highly possible, and  actually quite likely to happen if you’re new to ORM and the Entity Framework in particular.

How Can You Measure the LINQ query’s Efficiency?

To reduce the chances of creating inefficient ORM based applications, we need some way to easily (or it won’t be used) analyze exactly what goes on in the EF brain when it executes our code.

This post focuses on a product called LINQ Insight by DevArt, that addresses this problem (and a lot of other LINQ related problems) and may be downloaded for free from the DevArt site:

Download LINQ Insight Standard

NB! The FULL functionality is free for 30 days, before it changes to the more restricted, but highly useful Free Edition.

Data access anti-patterns decreases performance greatly and must be avoided.

When you write applications using for example LINQ to Entities with the Entity Framework, what goes on behind the scenes is something like:

1) The Entity Framework “Mapped” Objects are created with a ObjectQuery() method, run against an existing ObjectContext instance.

The ObjectContext instance is a container for in-memory objects and metadata extracted from the relational database.
It manages the object identities and states, keeps a pointer to original and current values of the objects properties, and tracks changes made to each object in its cache store.

The ObjectContext provides the connection and metadata information that is required to compose and execute queries, and should therefore be set up in advance.

2) Entity Framework components like the ObjectQuery class are compiled behind the scenes, and generate “regular” SQL statements that does the “actual” query against the database.

The queried data is returned as entity objects, most often as anonymous class instances, and the application works with these objects instead of directly querying the SQL database.
This is an important point to understand.
Using LINQ in this way, to offload the database server is a fundamental aspect of ORM frameworks.

Constructing an ObjectQuery Instance

The ObjectQuery returns a collection of zero or more typed entities.  The ObjectQuery generic class implements the IQueryable generic interface, whose methods enable LINQ queries to be incrementally built. You can also let the compiler infer the type of entities by using the C# var keyword, which is something that simplifies specific query situations considerably.

Instances of the ObjectQuery generic class, which implements the generic IQueryable interface, serve as the data source for LINQ to Entities queries. In a query, you specify exactly the information that you want to retrieve from the data source. A query can also specify how that information should be sorted, grouped, and shaped before it is returned. In LINQ, a query is stored in a variable. This query variable takes no action and returns no data; it only stores the query information. After you create a query you must execute that query to retrieve any data.

// Call ctor with query for "persons" and the named ObjectContext
var personsQuery = new ObjectQuery("Persons", context);

3) You can get access to the entities via the reference and collection properties, and this in turn produces additional SQL queries to get the necessary data.

4) When your application finishes working out what entities are needed, the “mapping” process of EF is more or less done.

BUT: if there are any changes to the queried entities, the application will have to save them to database somehow.
It does this by transforming your LINQ, like SubmitChanges() to “regular” SQL statements, like SELECT, UPDATE etc.

All of this happens without you being able to “see” exactly what’s going on at a “lower” level. That’s kinda the idea, to abstract away the unnecessary details from the user, and let you think about designing the flow of the application, not the technicalities used to make things work.

Though all these actions are performed by Entity Framework run-time, and are transparent for the application that works with entities, they may be complex and affect application performance greatly. That’s why you probably need a good Entity Framework profiler to create efficient applications using Entity Framework for data access. Entity Framework profiler in LINQ Insight really profiles ORM events and allows you to see how much time LINQ statement or SubmitChanges call takes to execute as a whole even if it generated multiple SQL statements.

Example Application

Let’s show how to profile Entity Framework applications on a simple example. We have used the widely used NorthwindEF sample database for our example, and generated the model for it with EDM wizard using default settings. The application is a simple console application with the following code: