Interview Questions Headline Animator

Wednesday, July 29, 2009

Singleton Vs. Static Class

Well I was asked this question once. “What is a singleton design pattern and why should I use it? Why not use a static class instead?”

And it was quite surprising how it went. I said that Singleton is used when you need to maintain state. Static classes are used when you want to club together a bunch of stateless methods that do something irrespective of which instance calls these methods. Math class is a good example.

So he asked me, can I not maintain state in a static class? So I said if you want to instantiate something in a static class how would imageyou do it? And surprisingly he told me that he would do it in a static constructor. I asked him, is it possible to instantiate anything in the static constructor or even declare an instance type in a static class? And he asked me is it not possible? I said it is not as far as I know and he asked me if I am sure.

Now the way the questions were asked about this subject, I was quite surprised. The interviewer looked quite convinced that instance types can be declared in a static class. I am surprised how he was allowed to take the interview in the first place. Or was he checking my confidence?

I wonder.

Monday, July 27, 2009

To query the local object context

When faced with the dilemma that what if I wanted to insert something and before I actually saved the changes into a persistent storage (database) by calling SaveAllChanges() method on the data context, I wanted to do a select, and also I wanted to make sure that this select is done on the just inserted objects, what should I be doing?

Well generally we write a generic method that gets all the records for a generic object. it looks like this:

IQueryable<T> result = null;

string type = typeof(T).Name;

//ObjectQuery query = entities.CreateQuery<T>(type);

ObjectQuery query = Context.CreateQuery<T>(type);

result = (

IQueryable<T>)query;

return result;

This will only query into the database. So if you have just recently inserted some records, but haven't yet committed them, they won’t appear in the results.

However, if we provide another method (an overload perhaps) that handles local objects, this would be possible. When I say local objects, I mean objects that have been inserted into the Object Context and yet not been persisted into the database. this method would look something like this:

IQueryable

<T> result = null;

string type = typeof(T).Name;

return from stateEntry in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added |

EntityState.Modified |

EntityState.Unchanged)

where stateEntry.Entity != null && stateEntry.EntitySet.Name == type

select stateEntry.Entity as T;

This way we give the option to the user of the API to choose which kind of select he wants to fire. A local one or one on the database.

Tuesday, July 14, 2009

Entity Framework, Business Objects and Beyond…

Back during the RMI days, we used to generate skeleton (henceforth referred to as skel or skels) and stubs for all long range service communications… Yes… WCF has its roots there and perhaps further into the past…! Details available here.

Looking at it from an architectural perspective, the data contracts were designed in such a way that the changes are minimal because every change in the stub would have to trickle down all the way to the client.

We have come a long now and Service Oriented Architecture is something that must be considered in any well behaved and decent enterprise application. In an environment such as this, I wouldn’t ever suggest having client dependencies on any 7 letter word starting with an ‘s’ ending with an ‘e’ and having ‘ervic’ in between.

We should think about providing all client apps independence so that any server / database / service changes do not affect the client… to a great extent…

Now there is a big discussion going on across the Internet about whether business objects should be separately created or should we treat EF entities as business objects. After all that’s what they are. Business Objects carry data from the client to the server and back and that’s what EF entities do… really really well… with all those amazing features such as state management, change tracking, yadda yadda… “straight out of the box” as they call it…

“Well couldn’t I agree more. But wait… entities are created from the database right…? using the EF Designer…?”

“Yes… and you could create in a number of other ways…”

“Well… doesn’t that mean that the design of my business objects would be dictated by the database design?”

“Of course not… Hell no… it is not necessary that you have to import all the entities from the database straight… of course you have the choice to customize them… manually…”

“Hmm… That’s nice… so now I can create my business objects with inheritance and everything just as I want them to and then map it to my database… True Object Relational Mapping…”

“Umm… Wait… did you say Inheritance… well you have that exception… you see the entities are already inherited from ObjectContext. And since there is no true multiple inheritance… you have that constraint… At least not in Framework 3.5… I don’t know if that would be available in Framework 4.0 either…”

“Ok… so what if the structure of the entities change…”

“Well all you have to do is regenerate the proxies and you are done…”

“Umm… new proxies…? Do you mean a change in the data contract…? what if I am not consuming the change… Like I have first name and last name in one of my screen and I don’t need the middle name although, i need it somewhere in the server… Do I still have to break the data contract…?”

“Err… Yes…”

Precisely why you should not be using Custom Entities or EF entities as business objects. You might be able to solve all your problems using partial classes and all code customizations… But end of the day, you are breaking the dependency rule.

There’s a reason why they call it a Data Contract.

The solution would be to create a separate business objects layer and then map your business objects with the EF entities. There are a couple of really smart implementations available. One of them could be implementing the Observer Design Pattern so any change could be tracked and reacted to.

Speaking of which I just discovered a good video in which Eric Meijer talks about the Reactive Framework. Definitely worth a watch.

Tuesday, July 07, 2009

“Open in new tab” hangs IE 8

I have been facing this problem for some time, and to tell you the truth, it gets a little embarrassing when if you are trying to show something to a larger group.

All you need to do these days, is Bing it and I got this. But this is with IE 8. Heck I thought why not give it a shot. And what do you know. It works with IE 8 too.