Home
  • Deutsch (DE-CH-AT)
  • English (United Kingdom)
  • Japanese(JP)
News
Student Partner of the Year 2011 PDF Print
Tuesday, 30 August 2011 10:00
This year's "Student Partner of the Year" award goes to Matthias Baumann, a Bachelor student at IVS/OVGU Magdeburg. Matthias was awarded for the great efforts he put into establishing the Windows Azure platform among the Magdeburg students. Matthias is the institute's most promising research talent.
 
Webcast: Azure Caching PDF Print
Wednesday, 06 July 2011 12:29
Matthias Baumann, a Microsoft Student Partner at OVGU, has shared his MSP TechDay Online Webcast on Windows Azure Caching on youtube: http://www.youtube.com/watch?v=GSVsDQl5DDc
Last Updated on Wednesday, 06 July 2011 12:34
 
Enrollment for Cloud Computing with Azure PDF Print
Sunday, 10 April 2011 11:31
The enrollment system for "Cloud Computing with Azure" is now online. Please follow this link to enroll. Try to enroll only once! The system automatically activates on Monday, 11. April at 08:00 a.m. Please be on time as only the first 45 students can be given an Azure account.

Link: http://cloudovgu.cloudapp.net
 
Accelerating Linq2SQL PDF Print
Tuesday, 21 September 2010 17:30

Note: The project is now available on http://linqaccelerator.codeplex.com. Sources and binaries can be downloaded from there.


I was using Linq2SQL, an excellent C# extension for database connectivity, quite a lot since it got released with VS2008.

One thing I have noticed very early is that when using Linq, there was a lot of code I was writing over and over again.

Whenever I wanted to get an object from the database, set it or delete it, I was basically writing the same code.

The following snippets sample the "set", "get", and "delete" I have mentioned above.

 

Set

Get

Delete

 

When you are dealing a lot with different tables or objects that you have persisted into a table, you will find yourself implementing

the above sort of functions over and over again without writing something new. I have tried to factor these three functions out into

something that can be used for any arbitrary get/set/delete database operation. My solution heavily relies upon a C# language features

that is called "Generics" as well as a feature that is called "Lambda expressions". I thought it would look cool, if we were able to specify

the data context from which we want to read as a Generic type parameter while we pass the "search rule" of the object(s) we are looking

for as a Lambda to a function. My thoughts resulted into the following:

 

SetLinqObject


Usage: SetLinqObject<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID, Linq2SQLTypeInstance);


GetLinqObjectFirst


Usage: GetLinqObjectFirst<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


GetLinqObjectAll


Usage: GetLinqObjectAll<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


DeleteLinqObject


Usage: DetLinqObject<Linq2SQLType, Linq2SQLTypeDataContext>((x) => x.ID == strID);


 

This should give you a handy tool with a stunning syntax. We have transformed 5 - 8 lines of code into 1 single line of code.

Of course, you can catch the exception in the surrounding code and rethrow whatever you want to rethrow here or just directly

process the exception.

 

 

Thanks,

Robert

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
public abstract class UT_Linq2SQL
{
public static T GetLinqObjectFirst<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There is no such object in the database.");

return query.First();
}

public static T[] GetLinqObjectAll<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There are no such objects in the database.");

return query.ToArray();
}

public static void SetLinqObject<T, U>(Func<T, bool> _func, T _object)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() != 0)
{
DeleteLinqObject<T, U>(_func);
ctx.SubmitChanges();

}

ctx = new U();
ctx.GetTable<T>().InsertOnSubmit(_object);
ctx.SubmitChanges();
}

public static void DeleteLinqObject<T, U>(Func<T, bool> _func)
where T : class
where U : System.Data.Linq.DataContext, new()
{
System.Data.Linq.DataContext ctx = new U();
var query = ctx.GetTable<T>().Where(_func);

if (query.Count() == 0)
throw new Exception("There is no such linq object in the databas.");

ctx.GetTable<T>().DeleteOnSubmit(query.First());
ctx.SubmitChanges();
}
}
}

Last Updated on Sunday, 14 November 2010 10:41
 
«StartPrev1234NextEnd»