It can be tempting to create "convenience back doors" in code to make short cuts. "Isn't it easier to skip all these layers and just call layer 1 from layer 3?". Yes, it is easier in the short run, but later on it will make refactoring very hard. I think the word "just" in any code-related conversation should set off alarms in your head. Layers are there for a reason.
I am currently involved in replacing a DAL for an existing legacy application. The existing DAL creates SQL statements and passes them to a handler that executes the statements with a connection from a connection pool. My job is to remove the SQL statements and replace them by service calls. Luckily the statements are very simple, because the application once used DBase, which isn't a relational database. So most of the SQL statements don't even use joins. This is of course very inefficient, but in this case my luck.
I started bottom-up, so I abstracted the SQL handler first to be able to monitor what is going through. Secondly i started looking for calls to the SQL handler class. It is a Vb6 application, but there is actually a layered architecture, so I figured that too wouldn't be too hard.
Until i found a function in the application layer. The function was there for "convenience". With the function anyone can throw any SQL statement at the SQL handler. And did i mention ... the function is part of the COM component's public interface.
The point of making classes and functions or, more generally, layers, is to abstract implementation details away from its users.
So much for abstraction.
22 September 2009
17 September 2009
Fetchmode.Eager, but still a proxy
I just had an issue where i could not cast a property of type Proxy_blah_blah_blah to the type that it really should have been ("Patient", in this case). The issue was still there after i explicitly set the fetchmode to "Eager": NHibernate seemed to ignore the fetchmode.
In a way, it actually did ignore the fetchmode. I am not sure if this is by design. I did not look this up, but blog the cause for future reference.
The problem was that the objects which were to be loaded eagerly, were already fetched by a previous query which had fetchmode lazy for these objects. So the objects were already present in the current session as proxies. Apparently NHibernate does not re-evaluate the lazyness of the objects if another query wants these objects to be fetched eagerly. I can understand this, because it is impossible to change existing objects to another type + nhibernate obviously wants to return the same objects from the same session.
To summerize the problem:
1. a query loads objects which have a reference to Patient which has a lazy initialized property Person
2. The property Person is now a proxy, in my case the proxy of a base class of Person
3. a second query loads objects that also have a reference to Patient. The query has fetchmodes for associationpath 'Patient' = Fetchmode.Eager and for associationpath 'Patient.Person' = Fetchmode.Eager.
4. Nhibernate now in a way ignores the fetchmode properties: in nhprof the query looks good, but the returned object tree has the previously created proxy objects instead of the resolved objects.
5. Using the Person property as a Person object now fails, because it is a proxy of a base class of Person.
A solution would be to use multiple sessions. The solution i chose in this setup is to eagerly load the objects in the first query, which made sense anyway.
In a way, it actually did ignore the fetchmode. I am not sure if this is by design. I did not look this up, but blog the cause for future reference.
The problem was that the objects which were to be loaded eagerly, were already fetched by a previous query which had fetchmode lazy for these objects. So the objects were already present in the current session as proxies. Apparently NHibernate does not re-evaluate the lazyness of the objects if another query wants these objects to be fetched eagerly. I can understand this, because it is impossible to change existing objects to another type + nhibernate obviously wants to return the same objects from the same session.
To summerize the problem:
1. a query loads objects which have a reference to Patient which has a lazy initialized property Person
2. The property Person is now a proxy, in my case the proxy of a base class of Person
3. a second query loads objects that also have a reference to Patient. The query has fetchmodes for associationpath 'Patient' = Fetchmode.Eager and for associationpath 'Patient.Person' = Fetchmode.Eager.
4. Nhibernate now in a way ignores the fetchmode properties: in nhprof the query looks good, but the returned object tree has the previously created proxy objects instead of the resolved objects.
5. Using the Person property as a Person object now fails, because it is a proxy of a base class of Person.
A solution would be to use multiple sessions. The solution i chose in this setup is to eagerly load the objects in the first query, which made sense anyway.
01 September 2009
Throw it away liberally
The code we developers write is like a child. It shouldn't be. The problem with being protective about your own code is that there is a barrier to change it. But the code is bad, contains lots of bugs, or worse, does not fulfill any requirement that it should.
That's why we should be very liberal to throw away code if we need to, especially if it is our own code.
That's why we should be very liberal to throw away code if we need to, especially if it is our own code.
Sourcegear does not add files after having worked online
If you're using the Sourcegear Vault client plugin for Visual Studio and use the feature "work offline", you have probably encountered this problem. If you added file while being offline, the added files won't be added to Vault after you "Go Online". The vault client thus silently corrupts your codebase.
I learned the hard way that i have to open each and every folder where i could possibly have added new files, to manually add them to vault after going online. But lately I discovered a good workaround. You can right-click on the project file, and choose "Add to vault" on the project root. All files that have not been added yet, will be added by that action.
I learned the hard way that i have to open each and every folder where i could possibly have added new files, to manually add them to vault after going online. But lately I discovered a good workaround. You can right-click on the project file, and choose "Add to vault" on the project root. All files that have not been added yet, will be added by that action.
How to get a working Back-button for an AJAX app in Internet Explorer
In an AJAX application, the back-button will take you to the wrong place because in AJAX applications the actions don't update the browser location. There are some solutions that change the hash of the Location (the part behind the #), but those solutions do not work in Internet Explorer.
The problem with IE is that a change in the hash will not lead to an entry in the history, so the back button will take you back to the previous page anyway. IE does react if you change the src attribute of an IFRAME, so most solutions for IE are in the direction of using a hidden IFRAME.
Since I did not find any actually working code, I post it here for future reference. The code works in IE, FF and Chrome.
The main page contains an iframe. The source of the iframe is an actual document on the server (iframe.html). The only thing the iframe document does is echo it's source url back into the parent page location hash:
The parent page redirects anchor clicks to update the src attribute of the iframe. Because the iframe will be reloaded, IE will update the history of the browser. The iframe writes the src url back into the parent page location. The parent page monitors the location and loads the appropriate url in the appropriate div:
1.html:
2.html:
3.html:
This example loads all links with class "ajaxLink" into the "siteContainer" div. In real life, you would probably want more control over in which div the link will be loaded. This can be accomplished by including the "target" in the class somehow and parse the class of the link to find this target.
The problem with IE is that a change in the hash will not lead to an entry in the history, so the back button will take you back to the previous page anyway. IE does react if you change the src attribute of an IFRAME, so most solutions for IE are in the direction of using a hidden IFRAME.
Since I did not find any actually working code, I post it here for future reference. The code works in IE, FF and Chrome.
The main page contains an iframe. The source of the iframe is an actual document on the server (iframe.html). The only thing the iframe document does is echo it's source url back into the parent page location hash:
<html>
<body>
<script type="text/javascript">
function parseLocation() {
var src = location.href;
var pos = src.indexOf('href=');
if (pos === -1)
src = '' //home
else
src = src.substring(pos + 5);
top.location.href = top.location.pathname + '#' + src;
}
parseLocation();
</script>
</body>
</html>The parent page redirects anchor clicks to update the src attribute of the iframe. Because the iframe will be reloaded, IE will update the history of the browser. The iframe writes the src url back into the parent page location. The parent page monitors the location and loads the appropriate url in the appropriate div:
<html>
<head>
<title>Ajax test</title>
</head>
<body>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<script type="text/javascript">
var baseHref = location.protocol + '//' + location.host;
$(document).ready(function() {
createAjaxLinks();
setInterval(checkLocationHash, 50);
$('#trackingframe').get(0).src = iframeSrc + '?href='
+ location.hash.substring(1);
});
function createAjaxLinks() {
$('a.' + ajaxLinkClass)
.removeClass(ajaxLinkClass)
.click(function(){
var href = this.href;
if (href.indexOf(baseHref) > -1)
href = href.slice(baseHref.length);
$('#trackingframe').get(0).src = iframeSrc
+ '?href=' + href;
return false;
});
}
function checkLocationHash() {
if (location.hash !== this.lastHash) {
var url = null;
if (location.hash === '' || location.hash === '#') {
//home
url = initialDocument;
}
else {
var url = location.hash.substring(1);
}
if (url !== null) {
url = baseHref + url;
$('#siteContainer').load(url, null, createAjaxLinks);
}
this.lastHash = location.hash;
}
}
var initialDocument = '/1.html'; //absolute url
var iframeSrc = '/iframe.html'; //absolute url
var ajaxLinkClass = 'ajaxLink'; //class for ajax links
</script>
<div id="siteContainer">
</div>
<iframe id="trackingframe" style="display:none">
</iframe>
</body>
</html>For this example I created three test documents, named 1.html, 2.html and 3.html, which will in turn be loaded in the siteContainer div.1.html:
<a href="2.html" class="ajaxLink">Load the second document</a>
2.html:
<div> This is the second document. <a href="3.html" class="ajaxLink">Load the third</a> </div>
3.html:
<div>Third document</div>
This example loads all links with class "ajaxLink" into the "siteContainer" div. In real life, you would probably want more control over in which div the link will be loaded. This can be accomplished by including the "target" in the class somehow and parse the class of the link to find this target.
Subscribe to:
Posts (Atom)