Repository base classes with LINQ to SQL and performance.

July 18, 2008 19:14 by Rob

I read an article on CodeProject about implementing the repository pattern with Linq To SQL.

I tried that approach because it sounded very nice. I did have some problems with performance and after a few debugging sessions (showing the SQL trace or the DataContext log) I found the problem.


By defining the virtual methods of the repository base class like this: public IEnumerable<T> FindAll(Func<T, bool> exp) the Func will not be (what I call) a delayed method, but it will be expanded immediately. I checked the log from the DataContext and it shows that the table is no longer queries using a where clause. Instead, a full table load is performed and the function is evaluated in memory.


However, the solution is very easy, although this mean you'll have to include the Linq assemblies to your testing bed:

protected T Single(System.Linq.Expressions.Expression<Func<T, bool>> exp)
{
    return Items.Single(exp);
}


The trick is to wrap your functions in a System.Linq expression. Client usage is exactly the same. The performance gain on my project was huge, If you consider that tables are no longer loaded completely into memory.

The full code for my repository base class is in the link below.

Repository.cs (4,64 kb)


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Silverlight Declarative Tweening

July 7, 2008 19:29 by Rob

I like the Silverlight animation system. You just define a storyboard somewhere in your code and in that storyboard you describe how you want a property to change over time. Now this is nice and all, but if you want cool ease-in and ease-out animations you'll have to do some very complex Spline-based composition. This gets even harder if you want to reuse those behaviours for different properties or targets.

Koen Zwikstra did some time on the Tweener library targeted at ActionScript 2 and 3 and came up with a Silverlight port. It includes full source code and some tutorials and a demonstration. The library contains most of the tweening animations you can come up with. Very reusable and surpringly little coding.


Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories:
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Dynamic Data and Silverlight?

July 6, 2008 19:00 by Rob

Yesterday I saw a demo on the new Dynamic Data contribution to Asp.net 3.5. It literally blew me away (really) because that was what I've been doing for most of my time over the last couple of years.

The base

Basically, we develop custom data driven web sites with Asp.net. Most of the time this also involves some kind of back-end content management. What our mini-cms actually did was the following:

  • Inspect the database model
  • Enrich that model with metadata such as preferred input method, validation and comments and associations,
  • Generate a whole lot of Asp.Net controls from the aforementioned properties,
  • Inject a lot of role-based security-stuff,
  • Present those forms to the end-user to allow basic site management.

So, what we had to build was a configuration file with all the meta data, the database, and of course the web site that got driven by that data.

Not any more. I'm out of a job.

With Visual Studio 2008, Asp.Net 3.5 framework and Dynamic Data the generic application that we wrote can be thrown out of the window, because with the DataContext designer this is not longer something somebody else will pay us to do. As you can see in the demo, by binding your decorated data model to the starter solution, you have an interface in a matter of minutes.

Fortunately the Internet-facing site cannot be generated yet. And, we can embrace the DynamicData technology to our advantage and create sites even faster.

On to the subject of this post!

Silverlight and Dynamic Data

Now this is all nice, but there is a simple problem. If I wanted to use this technology with Silverlight, In the last couple of weeks, I was working on a Silverlight application for our content management concept, and I was wondering if the object model could be easily extracted with the new LINQ To SQL designer. I would have to transfer all meta data and content through a WCF service, right?

Lucky for me, the DynamicData layer isn't even finished and does not yet generate WCF services. So, in order to build a generic Silverlight application you would have to serialize a lot of unknown types into a Silverlight application that was unaware of those types. That is not really what I wanted. I wanted a pre-compiled application that can be configured with just a single configuration property: the data model.

I decided to give it a go, and here's what I came up with. I will try to explain it one step at a time.

Here's my very basic data model:

image

Just a news item for a particular category that can have zero or more attachments to it. Nothing fancy.

Using the designer, this stuff generates classes for me with the all the properties and data access methods (select, update, delete etc.) while maintaining all the relations.

Here comes the cool bit: Did you know that the DataContext class actually gives us all the meta data without having to use reflection?  Just drill down into DataContext.Mapping and see that you find there:

image

(edit: since GetTables is a method, it should be written as .GetTables())

Silverlight does not have this nice API, So I created my own WCF contract to serialize these properties in a separate class. Here's a sample of the type specification that I use to transfer to Silverlight (I left the part out that maps the MetaDataMember stuff to my own classes on purpose).

image

To serialize an item of a particular type I perform a generic LINQ select on the table, I read all the properties of a data item and put them in a dictionary so that Silverlight can read and display those items:

image

Notice that for this demo I used the Convert.ToString() method on the value to get a nice presentation of the value. In a future version I will need to put the basic values (int, string, DateTime etc.) in the Values collection.

Of course this is not finished by far. I needed a way to decorate the designed classes with validation, descriptions, etc. Because I do not yet have the DynamicData beta installed on my machine I decided to borrow their approach to decorate generated code. Here's a snippet for the NewsItem class:

image

Because the data context designer generates partial classes, we have the ability to add functionality to those classes. Since I only need to apply some attributes to the generated class I need to define the class again (as partial) and apply my attributes. Since I cannot redefine the properties of that class I have to create an extension class that shadows the properties of the generated class (very nice idea, DynamicData Team!). To use the attributes of the extension class I do need to use reflection, but that is only once. Here's a video of how it works and how it is used. I just copied the concept to there's little more to tell.

A lot of work still needs to be done but I hope you understand that the rest is just more along the same line.

Looks like I still have a job!


Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Silverlight usage

June 18, 2008 01:35 by Rob

Over the last couple of days some discusion was raised about the use of silverlight in the new media, especially by the dutch public networks (add link), but it seems that more and more companies are starting to use silverlight for their presentations instead of just resorting to flash. I especially like this site from Renault, when they show off their new line of cars for the european market. I was surprised with the subtle use of animations and video in this silverlight app so check it out!

I'm probably not the first to notice this, but if you show off new technology, you'd better spell it right!


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

I love it!

June 7, 2008 14:20 by Rob

From what I've seen, Silverlight 2 beta 2 and Blend 2.5 june preview are amazing.

I've watched some of the new tutorials on silverlight.net, and I especially like the video titled ADD STATES TO A USERCONTROL FOR SILVERLIGHT 2. This demonstrates the fantastic way to add animations, flyout menus etc while having to write very little code or XAML yourself.

I'll definitely be spending some time with these new toys!


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Visual Studio | Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Silverlight 2.0 beta 2 is coming this week

June 5, 2008 14:44 by Rob

Check out the changelist.

 


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Silverlight 2.0 Modified WrapPanel

June 3, 2008 19:18 by Rob

Inspired by this article on Codeproject I decided to give reproduce the wrappanel so I could learn a little more about custom panels in SilverLight 2.0 beta 1. The code provided by Inear has some small problems, one of it not being able to behave properly in a scrollviewer. I made some small adjustments after scourging the internet and here is my solution:

Just before you pass on the ArrangeOverride to the base class, inject this code fragment:

[code:c#] 

if (this.Height != startPoint.Y + largestHeight)

{

this.SetValue(HeightProperty, startPoint.Y + largestHeight);

}

I did make some other small modifications to clean up the code a bit, and I didn't implement the Orientation=Vertical stuff, but check below for the full code.  

WrapPanel.zip (1,11 kb)


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: C# | Silverlight
Actions: E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Nice Silverlight + WCF demo

May 23, 2008 12:28 by Rob

Below is a ling to a video that demonstrates how easy it becomes to develop a datadriven Silverlight application.

http://blogs.msdn.com/swiss_dpe_team/archive/2008/03/17/silverlight-2-beta1-wcf-linq-to-sql-a-powerfull-combination.aspx

 


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Visual Studio | Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Visual Studio 2008 SP1

May 13, 2008 20:11 by Rob

According to some post on the web, it seems that visual studio 2008 sp1 is out. Biggest noticable change (for me) is that VS2008 Express received the ability to build Web project and class library which make it possible to build Silverlight 2.0 applications with Express. Yeah.


Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: Visual Studio | Silverlight
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Adding errors to a ValidationSummary

April 24, 2008 14:31 by Rob

Instead of having a custom <asp:Label> with the exception from the backend, you can inject a custom error message to the validationsummary of an asp.net page.

Of course, you could use a server-side validator, but that fires everytime you submit the page, and not only after a succesful post. A Custom Validator is therefore in the wrong place.

Here's what I came up with:

public class CustomValidationError : IValidator

{

private string _message;

public CustomValidationError(string message)

{

_message = message;

}

public string ErrorMessage

{

get { return _message; }set {}

}

public bool IsValid

{

get{ return false; }

set

{

}

}

public void Validate()

{

}

}

To add a custom message to the summary, just do this:

this.Validators.Add(new CustomValidationError("There is no backend!"));


Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net | C#
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed