EF bloating
Posted: June 25, 2009 Filed under: WCF | Tags: entity framework, rant, WCF Leave a comment »I just read a post from Cibrax aka Pablo M. Cibraro and remembered my old post on the “LINQ to SQL is dead” news.
Especially the quote:
They made a good work teaching us about how evil Datasets were for interoperability with other platforms, and now they came up with a solution like this, no way.
Priceless…
On the topic from Cibrax’ post, I could not agree more, I am a strong believer in the explicitness of service design and the WCF “opt-in” approach. How else would you know what is on the wire!? Similar to how you would choose ASP.NET MVC over classic ASP.NET WebForms if you want complete control of your HTML. Similar even to WCF vs. ASMX (on a high level).
Getting an absolute URL from ASP.NET Webforms
Posted: January 15, 2009 Filed under: ASP.NET | Tags: ASP.NET, url Leave a comment »Related to the post Getting an absolute URL from ASP.NET MVC here is the corresponding ASP.NET Webforms version (not as extension methods):
public static Uri GetBaseUrl(HttpRequest request)
{
Uri contextUri = new Uri(request.Url, request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = request.ApplicationPath, Query = null, Fragment = null };
return realmUri.Uri;
}
public static string GetAbsoluteUrl(HttpRequest request, string relativeUrl)
{
return new Uri(GetBaseUrl(request), VirtualPathUtility.ToAbsolute(relativeUrl)).AbsoluteUri;
}
Getting an absolute URL from ASP.NET MVC
Posted: January 13, 2009 Filed under: ASP.NET MVC | Tags: ASP.NET, ASP.NET MVC, url 4 Comments »I have been in situations where I need to generate an absolute URL to a specific action in an ASP.NET MVC application. The main problem mostly lies in getting the “domain” part of the URL. The only way to do this is to examine the request URL. I came across a very nice method of doing this during an encounter with the dotnetopenid.
public static Uri GetBaseUrl(this UrlHelper url)
{
Uri contextUri = new Uri(url.RequestContext.HttpContext.Request.Url, url.RequestContext.HttpContext.Request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = url.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
return realmUri.Uri;
}
public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName)
{
return new Uri(GetBaseUrl(url), url.Action(actionName, controllerName)).AbsoluteUri;
}
jQuery and AJAX Tooltips
Posted: January 1, 2009 Filed under: ASP.NET MVC, jQuery | Tags: javascript jquery tooltip 2 Comments »So the day has dawned, very symbolic that this day happens to be the first of a new year… enough of that. The day has finally come where I post a JavaScript entry! Who would have thought that???
Over the last couple of months I have had a very passionate relationship with a new hottie I discovered. She is named jQuery and OMG can she do tricks… OK, stop this talk right now
Basically I have had my head in AJAX for ASP.NET doing what others before already realized probably wasn’t the best approach. Finally I got around to have a look at jQuery and I must admit that (especially for my ASP.NET MVC project(s)) I haven’t looked back!!!
Today I had a little feature I wanted to solve with some AJAX:
I have a list of objects displayed in a certain view. One property on these objects is calculated and involved some semi-heavy database and calculation stuff and therefore requires both time and system resources to get. This value is not in particular important to the current view, but is a nice-to-have information. This means that the value as such does not need to appear in the view after rendering. I therefore figured it could be nice to have in a dynamic tooltip, i.e. “on tooltip hover, calculate/retrieve the value and display it as a tooltip”.
Having used other of Jörn Zafferer jQuery plug-in’s previously (with great success) I turned my attention to his tooltip plugin.
Having read the documentation it is apparent that, while you can specify a callback function (namely the “bodyHandler” property) to determine which “value” to display in the tooltip, there is an issue with this: it is synchronous! And by definition AJAX is asynchronous. The problem here is that if you do a $.get() call you the return value of the bodyHandler callback is “out of scope”, since it is first available once the AJAX call is completed, i.e.:
$(".tooltip-element").tooltip({
bodyHandler: function() {
$.get("/MyController/MyAction", { id : "myid" }, function(data) { /* do what with "data"? */; }, "html");
},
showURL: false
});
My solution to this was the following:
$(".tooltip-element").tooltip({
bodyHandler: function() {
var result = $("<span/>").html("some loading indicator");
$.get("/MyController/MyAction", { id: "myid" }, function(data) { result.html(data); }, "html");
return result;
},
showURL: false
});
I think that is pretty nifty! Of course the “some loading indicator” can be tweaked (e.g. use a AJAX load indicator). And this is of course not specific to ASP.NET MVC!
UPDATE:
There is a problem with this approach! The jQuery tooltip plugin used seems to invoke the bodyHandler callback directly “on mouse over”, ie. not after the delay. This means that although the tooltip displays nicely after the specified delay, the Ajax call was performed at T=0. What this effectively means is that if you eg. have a table where all values in column 1 have this Ajax-tooltip-thingy. If you then move the mouse in over a couple of the values each time you the cursor is over the value an Ajax call is executed. Not so good unless you want to utilize all resources in your server
Back to the drawing board…
ASP.NET MVC HtmlHelper methods and CSS class
Posted: November 19, 2008 Filed under: ASP.NET MVC | Tags: ASP.NET, ASP.NET MVC, reference 4 Comments »If you in ASP.NET MVC use a Html helper method to render out e.g. an input box:
<%= Html.TextBox("name") %>
Sometimes you might need to add an CSS class attribute to your input box (this should be a fairly standard thing). For this there exist overload methods that takes a “htmlAttributes” parameter of type object. Now my experience with this is somewhat similar to Ayende Rahien experience – although I have not begun banging my head against the wall out of frustration. This is not a rant, I love ASP.NET MVC, I just had an experience with something simple that was hard to figure out because of some of the design decisions – not that they are bad, they can just be confusing – especially with the lack of documentation at the current stage (yes, I know it is just a beta). So if you try this:
<%= Html.TextBox("name", null, new { class = "required"}) %>
You will (of course) get a compiler error, since “class” is a keyword and cannot be used like this. So how do you specify the class? Well, like this:
<%= Html.TextBox("name", null, new { @class = "required"}) %>
Just for future reference
ADO.NET Entitity Framework vs. LINQ to SQL
Posted: November 1, 2008 Filed under: .NET General | Tags: ado.net, entity framework, linq to entities, linq to sql 1 Comment »I have never been a huge fan of all the ADO.NET stuff – one exception: the DataReader. Everything else seems to me just to be bloated functionality around what I really need. How ADO.NET handles connections and commands are in my opinion overly "complex" – not that they are complex in any way, they could just be done way simpler. Whenever I have been writing code that access a database, I tend to rely on my trusted companion: the Enterprise Library. It provides such a nice and clean abstraction to the unneeded (IMHO) overhead posed by Connection, Command and Parameter handling.
Compare:
Database db = DatabaseFactory.CreateDatabase("my connectionstring");
using (IDataReader reader = db.ExecuteReader("my stored procedure", "my parameter 1", myParameter2, etc))
{
// do my thang
}
To:
using (SqlConnection conn = new SqlConnection("my connectionstring"))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "my stored procedure";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = cmd.CreateParameter();
param.ParameterName = "@param1";
param.DbType = DbType.String;
param.Value = "my parameter 1";
cmd.Parameters.Add(param);
param = cmd.CreateParameter();
param.ParameterName = "@param2";
param.DbType = DbType.Int32;
param.Value = myParameter2;
cmd.Parameters.Add(param);
param = cmd.CreateParameter();
param.ParameterName = "@param3";
param.DbType = DbType.Int32;
param.Value = etc;
cmd.Parameters.Add(param);
using (IDataReader reader = cmd.ExecuteReader())
{
// do your thang
}
}
And don’t get me started on the DataSet…
Now, MS has implied that LINQ to SQL will be deprecated and focus will be moved to the ADO.NET Entity Framework and LINQ to Entities. This has already stirred up the .NET community: David Hayden, Ian Cooper, Ayende Rahien to name a few.
I have never really considered using LINQ to SQL in big applications, mainly because I think LINQ to SQL imposes some constraints on my applications – one simple thing like the “low coupling” principle is not something that comes out of the box, it has to be tweaked and twisted to even give just a hint of low coupling. However I see LINQ to SQL as a very, very nice low-level method to create a basic SQL based data foundation.
For example, I am privately playing around with a blog engine based on ASP.NET MVC and LINQ, both XML and SQL. In this case LINQ to SQL is near-perfect for my needs. It provides sufficient abstraction from the database without imposing too much overhead, by providing flexibility in all future scenarios if the database suddenly need to be converted to an Oracle DB or refactored/de-normalized due to poor performance or whatever. It allows me to write SQL like-statements that (to a large extent) is verified at compile time. Sure I could do the same in LINQ to Entities, but it feels like firing up a super-tanker for something that really just need to be according to the KISS principle. LINQ to SQL is to a large extent very much KISS’ish, and fits perfectly in to the use I required for my playground project here.
I like to throw in this favorite quote of mine:
"Perfection is not when there’s nothing to add, but when there’s nothing to take away."
- Antoine de Saint-Exupéry
Or Occam’s Razor for that matter:
“All other things being equal, the simplest solution is the best”
Another thing about LINQ to Entities (and this is just based on a my brief experience with the ADO.NET Entity Framework), the designers within Visual Studio are not very good pretty bad very bad.
When creating a new “ADO.NET Entity Data Model” it does allow a reverse engineering possibility for an existing database. The model you end of with is simply a 1:1 mapping between entities and tables, i.e. the Active Record pattern. This is fine, but LINQ to SQL does the same – again “supertanker”. The Entity Framework is designed to be an abstraction of your database not “just” an Active Record pattern, i.e. your data model does not need to reflect your database schema in a 1:1 mapping. This is pretty neat, and can be useful, especially on larger projects where you are not creating the database schema yourself, or you are working on top of an existing SQL database. BUT if I create a very simple SQL database for a very specific scenario, I don’t really need this.
Another thing that is very bad in the designers is that once your model has been reverse engineered and you want to begin modifying/abstracting it, things get very, very tedious. The tools in the designer are not helping very much, i.e. apa
rt from initial reverse engineering the database, utilizing the schema meta data to create properties etc. is not possible. The model is (granted) fairly intuitive, but the task of adding properties that map to columns, etc. is just overly “complex” (again complex in as it could be done way simpler).
The key feature of the Entity Framework to work on-top of any database has also on my first shot failed. Maybe it is poor database design, I’m not really a DBA so I’ll let others be the judge of that. But I have a scenario where I have a n:n relation between two tables – Person and Group. But instead of having the foreign keys in my cross reference table being the primary key hereof, I have an explicit primary key:
When creating the ADO.NET Entity Data Model in Visual Studio (by reverse engineering).
That is of course fair enough, so I remove the PersonGroup entity and create a many-many association between Person and Group with the following setup:
I get this error:
Error 3025: Problem in Mapping Fragment starting at line 115: Must specify mapping for all key properties (PersonGroup.Id) of table PersonGroup.
You also have to map ALL columns in the database to properties in the entity model, maybe I don’t what that! What if my entity model is really built on top of an already running database, and my job is to create a another user interface for e.g. a mobile device which requires just a subset of the database schema. I don’t want to map the entire shebang, for one because it is very tedious and second because the rest of the schema might contain sensitive information that should not be exposed on the mobile device (or whatever reason).
What is it with that homemade Entity-SQL stuff, that really smells too much like reinventing the wheel – we already have SQL (a standard) which on top of that has T-SQL and then we have LINQ in our managed code – this is really enough query languages for one application! Do we really need yet another proprietary query language (although it has a lot of similarity with SQL – but so does T-SQL and LINQ)? I don’t think so!
Not to mention that for demoing LINQ to SQL is just perfect – however I don’t (sadly) think that could justify MS investments in the LINQ to SQL framework alone.
This is just my gut feeling, maybe it has no substance when it comes to reality! But one thing I know is: I like LINQ to SQL for what it is, but equally much for what it isn’t.
Please prove me wrong!
Cool upcoming .NET stuff from Microsoft
Posted: September 13, 2008 Filed under: .NET General, ASP.NET | Tags: ASP.NET, MVC, On .NET, Oslo, Silverlight, upcoming, Velocity, Zermatt 1 Comment »Silverlight 2.0
Version 2.0 of the ubercool Silverlight, Microsoft’ pendant to Adobe Flash, is drawing nigh. Currently available as beta 2, release in june ’08. Silverlight 2.0 allows embedded code client-side, by some magic, that does not even require having the .NET framework installed on the client.
ASP.NET MVC Framework
Quoting the ASP.NET MVC page:
ASP.NET MVC enables you to build Model View Controller (MVC) applications by using the ASP.NET framework. ASP.NET MVC is an alternative, not a replacement, for ASP.NET Web Forms that offers the following benefits:
- Clear separation of concerns
- Testability – support for Test-Driven Development
- Fine-grained control over HTML and JavaScript
- Intuitive URLs
References: Official page, Scott Guthrie MVC Preview 5 Release post
Zermatt
Quoting Zermatt home page:
Zermatt is a framework for implementing claims-based identity in your applications. By using it, you’ll more easily reap the benefits of the claims-based identity model described in this paper.
References: Official page, Vittorio Bertocci blog, Keith Brown blog
Velocity
Quoting Velocity Blog
(…) a distributed caching product to provide the .NET application platform support for developing highly performant, scalable, and highly available applications.
References: Velocity blog
Oslo
Quoting the official Oslo page:
”Oslo” is the codename for Microsoft’s forthcoming modeling platform. Modeling is used across a wide range of domains and allows more people to participate in application design and allows developers to write applications at a much higher level of abstraction. “Oslo” consists of:
- A tool that helps people define and interact with models in a rich and visual manner
- A language that helps people create and use textual domain-specific languages and data models
- A relational repository that makes models available to both tools and platform components
References: Official page, Douglas Purdy – "What is Oslo?", Don Box blog, Various MS SOA articles (incl. non Oslo stuff)