November 2007 Entries



Looks like Microsoft added so many features to Silverlight, they are setting it now as version 2.0.  More info in my post at:

http://www.SilverlightCity.com

 




Some have run into the problem where you try to use the "(?i)" in a ASP.NET RegularExpresssionValidator to ignore case in the search.  JavaScript does not understand this and will generate an error on the client side, while it still works as expected on the server side.  I do not know of a method that will allow a search to be case-insensitive by supplying a regular expression to JavaScript, due to the "i" flag being a modifer and required to be supplied as a parameter to the RegExp(). 

So, the soution I came up with is to replace the ASP.NET "RegularExpressionValidatorEvaluateIsValid" method with:


<script type="text/javascript" language="javascript"> 
function RegularExpressionValidatorEvaluateIsValid(val) 
{
  var value = ValidatorGetValue(val.controltovalidate); 
  if (ValidatorTrim(value).length == 0) 
return true;
  var rx = null; 
  if(val.validationexpression.indexOf("(?i)")>=0) 
{
rx = new RegExp(val.validationexpression;.replace("(?i)",""),"i");
}
else
{
rx = new RegExp(val.validationexpression;);
}
  var matches = rx.exec(value);
  return (matches != null && value == matches[0]); 
}
</script>


Then when I want a case-insensitive search on a page, I include the above funtion at the bottom of my page (typically under the form tag) and add "(?i)" to the beginning of my regular expressions.

It is kind of a hack, but seems to work from what I can see.




Are you planning to upgrade your Visual Studio 2008 to the RTM version?  You need to plan some time to fix a few breaking changes.  When I first started upgrading some of my sites:

I found this link useful:

http://weblogs.asp.net/bschooley/archive/2007/11/21/converting-web-site-projects-from-visual-studio-2008-beta-2-to-visual-studio-2008-rtm.aspx

The post details changing the DataSetExtentsion referrence from 2.0 to 3.5 and changing LINQ 2 SQL Add/Remove method name changes, OneValidate method change and how to change your *.dbml files to load in the designer.

 




This is an older article I happened to find recently that opened a few more areas of thought about LINQ.  Yes, I know LINQ is used for many things, not just data storage, XML or easy of collection handling.  That said though, I really did not thnk about it in the use of a type of logic parser.   What a good idea!

This is article gives examples of how to use LINQ to reduce the coding and complexity of scoring Yahtzee results on dice.   Pretty cool!

http://www.c-sharpcorner.com/UploadFile/mgold/Yahtzee LINQ07222007010520AM/YahtzeeLINQ.aspx

 




Scott Guthrie has just listed part 1 of a same e-commerce storefront application using the new ASP.NET MVC framework to show off how to put things together.  Give it a looksie.. :

http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx

 

 




Just spent another couple days trying to track down and work around an issue I was having with LINQ 2 SQL and the current entity structure that is used.  The problem is basically, the change tracking routines to keep up with what changes you have made to an entity, is handled by the DataContext and not in your entity.  If you close your DataContext, you will have no change tracking.  Even worse yet, you will have problems "attaching" that instance to a new DataContext unless it is truly detached and you have a copy of the original data that the attach method can use to see what changes were made.

Boy or boy, there was a lot of fun tracking all this stuff down.  Here is a post that talks about these kind of issues:

http://west-wind.com/weblog/posts/135659.aspx

In my books, the problem is that LINQ 2 SQL does not go far enough to provide features to make it fully useful when workign with disconnected data.  I understand the desire to have lightweight entities and simplistic syntax, but a couple of extra features could really help out.  Currently, I am working with VS 2008 Beta 2 and changes to the Attach are expected in the RTM, but I still do not think they will go as far as I need.

My goal is simply an entity that keeps track of its own original state to use when I wish to attach an entity to complete changes.  So, the first step I wanted to handle is adding a new property and instance variable for the original state.

In the follow example, let us say we have a database table such as:

table Customer:
Customer_ID int NOT NULL
Phone nvarchar[26] NOT NULL
CustomerName nvarchar[128]

Okay, I use the designer and create a LINQ 2 SQL class file by dropping the table on it.  Now add another class file to the project and a partial class for my Customer class.  In this class I will drop an variable called "original".  I also add a property called "Original":

public partial class Customer
{
     internal Customer original = null;

    public Customer Original
    {
       get { return original; }
       set { original = value; }
    }
}

Now, I wanted a method to copy all the fields of the class, but did not want to bother with reflection or anything like that.  I just made a CopyData method which copied the data properties from the old to the new instance without copying the "original" variable.  Then I build a Clone method that fully copied the instance along with the Original value (I used static methods but you can make the instance methods:

internal static void CopyData( Customer newCustomer, Customer oldCustomer)
{
    if(oldCustomer == null)
   {
       newCustomer = null;
   }
   else
   {
      newCustomer.Customer_ID = oldCustomer.Customer_ID;
      newCustomer.Phone = oldCustomer.Phone;
      newCustomer.CustomerName = oldCustomer.CustomerName;
   }
}

public static Customer Clone(Customer oldCustomer)
{
   Customer newCustomer = new Customer();
   CopyData(newCustomer, oldCustomer);

   Customer newOriginal = new Customer();
   Customer oldOriginal = oldCustomer.Original;
   CopyData(newOriginal, oldOriginal)

   newCustomer.Original = newOriginal;
   return newCustomer;
}

Now, the only thing left to do is to copy off the original value when read in.  There is a method called OnLoaded() that will fit that bill!

partial void OnLoaded()
{
   original = Clone(this);
}

That is a done deal now.  Our Entity will carry a copy of its original data. To make a copy that we want to use disconnected from the DataContext, we would use the Clone() method to make the instance such as:

Customer myNewSpiffyEntity = Customer.Clone(myFoundEntityIWantToUse);

It is now safe to do what we want, we can pass this up through tiers or just about anything we want and when the time to update has arrived we simply call:

DataClassesDataContext db = new DataClassesDataContext();

db.Attach(myNewSpiffyEntity, myNewSpiffyEntity.Original);
db.SubmitChanges();


That is all there is to it.  The entity is still simple, yet it tracks its changes to the individual properites.  Of course, this will not work in all situations, but in basic needs, this can go a LONG ways to making LINQ much more useful for disconnected data manipulation.  I know I will use this a lot in my web applications without the need to persist DataContext'es or any funky stuff like that.

Now if only this featuer was built in or at least offered as an option inside the designer so I did not have to go through this extra effort on each class I plan to offer this functionality.  I did ponder how nice it would be to simply click an option in the designer or a property on the table inside the entity editor, to tell it to generate the copydata, clone and OnLoaded to maintain the original state.  I see a number of posts out there where it appears people would like to have the entities manage state when not handled by the DataContext.




Here is a handy little tool to play around with:

http://www.albahari.com/linqpad.html

It allows you to play around with LINQ queries against your database in a friendly manner.