MbUnit – New website and Documentation

Today, we finally launched the new Mbunit Website and we have the documentation in a great format online and offline.  Dan Maharry and I have been working on this for a while, I have been putting together the site and the Sancastle/DocProject setup while Dan has been working hard on the actual content – so it will actually all make sense!

Dan has blogged about the docs more over @ http://blogs.ipona.com/dan/archive/2007/08/13/8417.aspx

Visit the new main site at – http://www.mbunit.com

Visit the new documentation site at – http://docs.mbunit.com

Download the documentation in an offline format – http://docs.mbunit.com/Mbunit.Documentation.chm

We are really interested in hearing your feedback on this, so please let us know.

Technorati tags:

Windows Live Folders becomes Live SkyDrive

Today, Windows Live Folders was renamed to Windows Live SkyDrive.   This service currently offers 500Mb of free online disk storage space.   When the service was first announced, it wasn’t open in the UK however it seems to have expanded as I am now able to use the service.

My initial thoughts are, do I really want all of my documents online?  I thought it would be cool to have them online, but in reality, i’m not sure I do want it.  The site mentions that your personal data is password protected with your Live ID and the site uses SSL.  Well, SSL is only a limited form of protection and doesn’t stop anything if there is a vulnerability in the site.  Also, I haven’t heard anything about how the files are actually stored on the disk?  I know some services use Blowfish encryption for an added layer of protection while others use Advanced Encryption Standard (AES) which is the standard encryption algorithm of the US government.  So what encryption does SkyDrive use?  If I knew for a fact they where using encryption which protected the files at all points along the journey then I would be happier.  However, if my Live ID was hacked then this wouldn’t be any good away….

Moving onto the service, everyone has their own reasons for wanting to store there files online, for me the main reason is an off-site backup solution.  I’m sure this service will be linked into Windows Home Server so files could be automatically sync’ed.

As for the user experience, there are three levels of sharing – Personal, Shared, Public. As mentioned, Personal are protected by your Windows LIve ID,  shared can be accessed by defined users using their live ID and you decide what they can and cannot do (read/write) and the public folders can be access by anyone and they can only read.

Uploading files is initially limited to 5 files at a time, which is a very slow process.  However, you can download a ActiveX addin which allows you to drag and drop any files onto the upload surface embedded in the webpage which greatly improves the experience.

The UI is easy to navigate.  At the moment, editing files is very limited. You have to download the file locally, make the change, then re-upload the file.  At the moment there is no way to integrate the service into Windows, for example using WebDav and as a Network Location.  I’m guessing security together with usability are the key here.

I’m sure I will use the service in the end as it will solve a number of problems and will be really interesting when the API is released. For the moment, I think I will see how others get on.

Technorati Tags: , ,

Linq to SQL – Difference between Concat and Union?

This morning I came across the Concat and Union methods within Linq to SQL.  From the surface they look very similar.

var query = from o in db.Orders
                        where o.ShipCountry == “UK”
                        select o;

var query2 = from o in db.Orders
                        where o.ShipCountry == “France”                       
                        select o;

var union = query.Union(query2);
Console.WriteLine(union.Count());

var concat = query.Concat(query2);
Console.WriteLine(concat.Count());

The SQL they is also very similar, Union is first, concat is second.

SELECT COUNT(*) AS [value]
FROM (
    SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]
    FROM [dbo].[Orders] AS [t0]
    WHERE [t0].[ShipCountry] = @p0
    UNION
    SELECT [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]
    FROM [dbo].[Orders] AS [t1]
    WHERE [t1].[ShipCountry] = @p1
    ) AS [t2]

SELECT COUNT(*) AS [value]
FROM (
    SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]
    FROM [dbo].[Orders] AS [t0]
    WHERE [t0].[ShipCountry] = @p0
    UNION ALL
    SELECT [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]
    FROM [dbo].[Orders] AS [t1]
    WHERE [t1].[ShipCountry] = @p1
    ) AS [t2]

The only difference being, Union uses ‘UNION’ while Concat uses ‘UNION ALL’.  A look on W3 Schools gives us the difference between the two keywords.  Union returns distinct values – duplicate rows removed, while UNION ALL selects all the values and doesn’t remove any duplicates.  In this example, no duplicates are produced so they return the same result set. However, if we change it to (for example purposes only)

var query = from o in db.Orders
                        where o.ShipCountry == “UK”
                        select o;

var query2 = from o in db.Orders
                        where o.ShipCountry == “UK”                       
                        select o;

Then UNION will return 56 rows, while UNION ALL returns 112 and that is the difference between .Union() and .Concat().

Technorati Tags: , , ,

Visual Studio 2008 Beta 2 Sample Code

Just spotted two downloads for sample code updated for VS 2008 Beta 2.

Samples for Visual Studio 2008 Beta 2 (VB.net)

LINQ and language samples for Visual Studio 2008 Beta 2 (C#)

Lots of different samples here covering both Linq and the core language features. Always something handy to have on your harddrive.

——-

After downloading, I tried to run the C# sample for DynamicQuery and received the following error:

“Failed to generate a user instance of SQL Server due to a failure in copying database files. The connection will be closed.”

I changed the query string to be user instance=false which caused a much more helpful error message.

“CREATE DATABASE permission denied in database ‘master’.
An attempt to attach an auto-named database for file E:UsersBen HallDocumentsVisual Studio SamplesCSharpSamplesLinqSamplesDataNORTHWND.MDF failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.”

So I changed the query string to use my pre-exiting Northwind Database

“string connString = string.Format(“server={0};database=northwind;Integrated Security=SSPI;Connection Timeout=30″, sqlServerInstance);”

This change will need to be made to all of the database interacting samples. 

Technorati Tags: , , ,

How Linq knows if a partial method has been implemented

My previous post got me thinking, how does the DataContext know if a partial method has been implemented in order to direct the call to the partial method instead of its own internal method?  It’s interesting because, if the partial method has not been implemented, all the calls are removed at compile time, and the names of the methods are quite dynamic (InsertObject…(object o)) making it difficult to call this ‘known’ method.  The methods are defined by the designer, so the internal framework will not know anything about these methods.  While I am taking in terms of how Linq does this, this could be valid for any application.

So, in situations like this there is only one option – Lutz Roeder’s Reflector. The Linq implementation is massive and takes a little bit of time to understand how its all been put together.  There is a video by some of members of the Linq team talking about the pipeline processing of Linq to SQL – watch it here

In Linq to SQL, every object returned from the database is tracked by an internal Lookup Table.  There are many reasons why Linq tracks all of the objects, such as caching and change management.  These objects are of type StandardTrackedObject, which inherits from TrackedObject.  If you look in reflector, you will have a list of ways these objects are exposed.

One of the main ways they are exposed is by a series of methods on the System.Data.Linq.ChangeDirector.StandardChangeDirector object.  Some of the methods are DynamicDelete, DynamicInsert, DynamicUpdate which take a TrackedObject as a parameter.  These methods are used is various places, but one place is within System.Data.Linq.DataContext.ExecuteDynamicUpdate(), there are equivalent delete and insert methods but I will continue to talk in terms of Update.  This means that this method is called to update the object and execute the command against the database.  As mentioned in the last post, this is the method we need to call in order to execute the command within a partial method, so this doesn’t decide which method to call.

One of the other places DynamicUpdate is used is by StandardChangeDirector.Update() which in turn is called by ChangeProcessor.SubmitChanges().  This means that when DataContext.SubmitChanges() is called, StandardChangeDirector.Update() is also called later in the process, if updates are required.

StandardChangeDirector.Update() is the important method that decides if the internal ExecuteDynamicUpdate() should be called, or if the call should be passed to the appropriate partial method.

The method checks to see if a property on the TrackedObject is null, this property is called UpdateMethod that returns a Reflection.MethodInfo object.  If it is null, then it calls ExecuteDynamicUpdate() otherwise the method relating to the MethodInfo is invoked using the Invoke command.  Just a bit of background on the MethodInfo object, when using reflection you can access any field or method at runtime without knowing about it at compile time.  The FieldInfo and MethodInfo objects are a bridge between the calling code and the method on the object, by calling MethodInfo.Invoke() the method can be indirectly invoked.  The MethodInfo object would be null, if the method does not exist on the object, or this case if the partial method hasn’t been implemented.  This is how Linq can override logic and execute a partial method instead of its own internal method without having to worry if it has been implemented by the developer.

But this still leaves a question unanswered. How does it know the name of the method to execute or even how it finds out if it has been implemented?  The framework knows that each object in a DataContext has three partial methods and the prefix for each of them but not the full name.

The answer is clever, it tries to find the method based on Prefix (“Update”) + this.RowType.Name.  RowType.Name is the name of the entity, so if the partial method has been implemented the object will have a method with that name and if its found it will populate the property ready to be invoked, if the method hasn’t been implemented, it wouldn’t be found and so will be null.

Simple but really clever!  Hope you found this interesting, I know I did.

Technorati Tags: , ,

Partial Methods on a Linq DataContext

Following on from my previous post and my connect feedback I thought I would write about the partial methods on a linq datacontext.

As I thought, Dinesh confirmed that the partial methods (Insert, Update, Delete) on a DataContext are overriding instead of notifications.  The reason for this is so that you can override the default logic, so you could redirect the update via a stored procedure.

However, you can continue processing the default way, even within the partial method by calling this.ExecuteDynamicXXXXX() method.

For example, the method below overrides the default UpdateOrder logic, writes out information to the console then continues to execute the update by calling ExecuteDynamicUpdate();

partial class DataClasses1DataContext
{
partial void UpdateOrder(Order instance)
{
Console.WriteLine(“UpdateOrder”);
this.ExecuteDynamicUpdate(instance);
}
}

This is definitely something to be aware of, but the new methods means that it is not a problem if you want to include custom logic but still process the command in the same way.

ReSharper and Visual Studio 2008 Beta 2

With every release of Visual Studio, all of the plugin’s need to be updated and released.  Over on Ilya Ryzhenkov (ReSharper Product Manager at JetBrains) blog he has posted information on the Early Access Program (EAP) where you can download nightly builds of ReSharper. 

Via the EAP, they have released ReSharper 3.0.2 which includes support for VS 2008 (Orcas) Beta 2.

ReSharper 3.0.2 nightly build

Remove any existing installations before installing.

However, ReSharper doesn’t play nicely with C# 3.0 and Linq and the only way to turn it off is to disable the plugin, which also displays it for C# 2.0 which works.

image

Hopefully they will support it soon.  I wonder what DevExpress are doing this C# 3.0….

Technorati Tags: ,

Obtaining table metadata using Linq to SQL – MappingSource

One of the posts on the MSDN forum asked how to obtain the type length of a column in the table using Linq, this post will just explain how to obtain this information and some information on the MappingSource object.  When linq does the mappings of database to objects, it transforms the DBType (NVarChar) into a CLR Type (String) which means when you are accessing the objects in code, you are accessing them as .Net types hence no simply way to get the underlying DBType.  One way would be to look the information up in the mappings file or the attribute but Dinesh Kulkarni (PM) pointed out an more directly way, in an indirect way.

The DataContext has a property which allows access to all of the mapping information, this property is Mapping.MappingSource.  The MappingSource object has a property GetModel() which requires the type of the DataContext which contains the table as a parameter. This then returns a MetaModel object which allows you to access some high level information. The method we are interested in is the GetMetaType() which accepts the typeof the entity object we are interested in and returns a MetaType object.  This is when it starts to get interesting.  The MetaType holds information about the table such as associations, DerivedTypes and Inheritance information, but the method we are interested in is GetDataMember() which takes a MethodInfo object relating to the column we want information about. This returns a MetaDataMember which holds all the information about the column, such as if it is a primary key, expression, nullable and DBType.

To demonstrate this in code, first we get the MemberInfo type for the column we are interested in for the GetDataMember method.  Then we can gain the DBType via the MappingSource.

string columnName = “OrderID”;
MemberInfo[] mf = Type.GetType(“LinqConsole.Order”).GetMember(columnName);

db.Mapping.MappingSource
                      .GetModel(typeof(DataClasses1DataContext))
                      .GetMetaType(typeof(Order))
                      .GetDataMember(mf[0])
                      .DbType;

Code isn’t that complex, but we can remove the need for obtaining the MemberInfo by going via the DataMembers collection and using a lambda question to return the MetaDataMember for the column name.

string orderIDType = db.Mapping.MappingSource
                                            .GetModel(typeof(DataClasses1DataContext))
                                            .GetMetaType(typeof(Order))
                                            .DataMembers.First(x => x.Name.Equals(columnName))
                                            .DbType;

The DBType in this case is “Int NOT NULL IDENTITY”.  For ShipName it comes out as “NVarChar(40)”.

Note, when using either of these ways you need to check to make sure that the column actually exists and handle situations when it doesn’t correctly.  In this cause, using the first approach might be easier.

Post link:

http://forums.microsoft.com/MSDN/showpost.aspx?postid=1944830&siteid=1

Technorati Tags: ,