<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6738070462710484397</id><updated>2024-10-24T02:00:57.662-04:00</updated><category term="c#"/><category term="linq"/><category term="silverlight"/><category term="xaml"/><title type='text'>Bits for Twits</title><subtitle type='html'>Musings on Software Development</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bitsfortwits.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://bitsfortwits.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Dave</name><uri>http://www.blogger.com/profile/06186921251648245557</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6738070462710484397.post-2477847649517166531</id><published>2008-11-19T12:12:00.009-05:00</published><updated>2009-02-06T14:51:44.180-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="silverlight"/><category scheme="http://www.blogger.com/atom/ns#" term="xaml"/><title type='text'>Silverlight 2: Getting button positions on clicks</title><content type='html'>For whatever reason, In Beta 2 of Silverlight 2 Microsoft decided to remove support for the MouseLeftButtonDown event on Buttons.  The argument was the functionality of MouseLeftButtonDown was already in the Click event, so they got rid of it.  But there is one major difference: the event args of Click are RoutedEventArgs, while for MouseLeftButtonDown the args are MouseButtonEventArgs.&lt;br /&gt;&lt;br /&gt;For my purposes, the problem was that I needed access to the mouse position.  In my Silverlight 2 app, I’m displaying a popup usercontrol whose position is relative to where it was clicked.  It’ll draw a bit to the side with additional information on a DataGrid row.&lt;br /&gt;&lt;br /&gt;After some experimentation and frustration, I’ve implemented a solution: simply extend the Button class and override the OnMouseButtonLeftDown event, and have it set Handled to “false”.  Quite simply, this code re-wires the event for Button like it should have to begin with:&lt;br /&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;public class PositionButton : Button&lt;br /&gt;{&lt;br /&gt; protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)&lt;br /&gt; {&lt;br /&gt;  base.OnMouseLeftButtonDown(e);&lt;br /&gt;  e.Handled = false;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)&lt;br /&gt; {&lt;br /&gt;  base.OnMouseLeftButtonUp(e);&lt;br /&gt;  e.Handled = false;&lt;br /&gt; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Then at the top of your XAML, add in the reference to your custom UserControl namespace.&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;brush: xml; gutter: false; toolbar: false;&quot;&gt;&amp;lt;UserControl …xmlns:uc=&quot;clr-namespace:YourNamespace.UserControls&quot;&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;When you reference the button, instead of using &amp;lt;Button&amp;gt; use &amp;lt;uc:PositionButton&amp;gt;, like so:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;brush: xml; gutter: false; toolbar: false;&quot;&gt;&amp;lt;hc:PositionButton x:Name=&quot;btnViewComments&quot; MouseLeftButtonDown=&quot;clickComments&quot;...&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style=&quot;color:black;&quot;&gt;Then in your code behind, you can simply reference the Position:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;brush: csharp&quot;&gt;private void clickComments(object sender, MouseButtonEventArgs e)&lt;br /&gt;{&lt;br /&gt; Point pos =  e.GetPosition(App.Current.RootVisual);&lt;br /&gt; // do whatever you want with the position&lt;br /&gt;}&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsfortwits.blogspot.com/feeds/2477847649517166531/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bitsfortwits.blogspot.com/2008/11/silverlight-2-getting-button-positions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/2477847649517166531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/2477847649517166531'/><link rel='alternate' type='text/html' href='http://bitsfortwits.blogspot.com/2008/11/silverlight-2-getting-button-positions.html' title='Silverlight 2: Getting button positions on clicks'/><author><name>Dave</name><uri>http://www.blogger.com/profile/06186921251648245557</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6738070462710484397.post-6303049640404574276</id><published>2008-09-23T12:56:00.011-04:00</published><updated>2009-02-06T15:03:45.574-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="linq"/><title type='text'>LINQ: DataContexts and You</title><content type='html'>&lt;p&gt;For the past several weeks, I&#39;ve been working extensively with LINQ for the first time. One perpetual source of headaches was the use of DataContexts with LINQ to SQL -- specifically, using too many of them. The original code we inherited had, like most LINQ examples, code such as this:&lt;/p&gt;  &lt;pre class=&quot;brush: csharp&quot;&gt;using (var db = new YourDataContext())&lt;br /&gt;{&lt;br /&gt; var syncRow = (from date in db.Syncs&lt;br /&gt;       where date.Table == &quot;YourTable&quot;&lt;br /&gt;       select date).SingleOrDefault();&lt;br /&gt;&lt;br /&gt; if (syncRow == null)&lt;br /&gt; {&lt;br /&gt;  // make a new one (*snipped*)&lt;br /&gt;  db.Syncs.InsertOnSubmit();&lt;br /&gt; }&lt;br /&gt; else&lt;br /&gt; {&lt;br /&gt;  // update existing row&lt;br /&gt;  syncRow.Synced = currentSyncDate;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; db.SubmitChanges();&lt;br /&gt;}&lt;/pre&gt;This creates a DataContext that exists only within the “using” block, retrieves a row, creates a new one or updates an existing one as appropriate, then submits the changes.  There’s nothing wrong with this code from a low level, it all works and the simplicity and automatic disposal provided by the “using” block is actually quite convenient.  &lt;p&gt;There main problem is &lt;strong&gt;Data concurrency&lt;/strong&gt;.  When you submit to one DataContext, it does write to the database as you would expect.  What it doesn’t do is refresh other DataContext objects you may have open elsewhere in the codebase, particularly in another class or function.  In our application, this came up when every function had wrapped the activities in this using wrapper and as a result, we’d have stale data residing in the calling function since its DataContext was stale.  Sometimes this would be obvious (for example, a returned object from a previous query was not updated), but other times it wasn’t obvious due to the caching LINQ does behind the scenes.  &lt;em&gt;Even if you update and submit changes in one DataContext, this will not necessarily become available in a new query in a pre-existing DataContext&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;There is a hack here that could be used, but should not be used.  Sometimes this is unavoidable.  The hack is to force a refresh on the DataContext objects when you expect for a previous function to write to the database but cannot share a context.  In our application, our unit tests make use of this hack after populating the database with dummy data: &lt;/p&gt;&lt;pre class=&quot;brush: csharp; toolbar: false&quot;&gt;db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, yourLinqObject); &lt;/pre&gt;&lt;p&gt;The best overall solution to this problem is to simply share DataContexts as much as possible.  In our application, we’ve both used class-level (rather than function-level) contexts where appropriate and for other cases where different classes must share the same DataContexts, we simply pass them as parameters from function to function.  This eliminates the performance hit as well of constantly creating/destroying database connections, which may not be syntactically obvious from the innocuous “using” statement. &lt;/p&gt;&lt;p&gt;Another big win for having a single, class-global DataContext is how trivial it is to wrap it in a transaction.  Just wrap all of the code to fit in the transaction in a using block with a TransactionScope object:&lt;br /&gt;&lt;/p&gt;&lt;pre class=&quot;brush: csharp; tab-size=6&quot;&gt;using (TransactionScope ts = new TransactionScope())&lt;br /&gt;{&lt;br /&gt; try&lt;br /&gt; {&lt;br /&gt;  // get stuff&lt;br /&gt;  List&lt;YourTable&gt; messages = OtherObject.GetMessages(yourDb);&lt;br /&gt;&lt;br /&gt;  // loop through messages&lt;br /&gt;  foreach (YourObject currentObj in messages)&lt;br /&gt;  {&lt;br /&gt;   // update messages&lt;br /&gt;   OtherProvider.UpdateMessages(currentObj);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  // complete the transaction&lt;br /&gt;  ts.Complete();&lt;br /&gt; }&lt;br /&gt; catch (Exception ex)&lt;br /&gt; {&lt;br /&gt;  // insert your favourite exception handler&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;yourDb.Dispose();  &lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsfortwits.blogspot.com/feeds/6303049640404574276/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bitsfortwits.blogspot.com/2008/09/linq-datacontexts-and-you.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/6303049640404574276'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/6303049640404574276'/><link rel='alternate' type='text/html' href='http://bitsfortwits.blogspot.com/2008/09/linq-datacontexts-and-you.html' title='LINQ: DataContexts and You'/><author><name>Dave</name><uri>http://www.blogger.com/profile/06186921251648245557</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6738070462710484397.post-5916927689922094631</id><published>2008-09-06T20:54:00.000-04:00</published><updated>2008-09-06T21:02:28.272-04:00</updated><title type='text'>Obligatory first vacuous post</title><content type='html'>I&#39;ve noticed over the past several years how valuable the blogosphere can be when troubleshooting issues with software development.  It&#39;s also been a tremendous resource in expanding my knowledge about various technologies and development methodologies.  &lt;br /&gt;
&lt;br /&gt;
While I don&#39;t plan to regularly update this blog, when I discover a new &quot;gotcha&quot;, issue, idiom, or any other nugget of information that others could benefit from, I&#39;ll make an effort to post it here.</content><link rel='replies' type='application/atom+xml' href='http://bitsfortwits.blogspot.com/feeds/5916927689922094631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://bitsfortwits.blogspot.com/2008/09/obligatory-first-vacuous-post.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/5916927689922094631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6738070462710484397/posts/default/5916927689922094631'/><link rel='alternate' type='text/html' href='http://bitsfortwits.blogspot.com/2008/09/obligatory-first-vacuous-post.html' title='Obligatory first vacuous post'/><author><name>Dave</name><uri>http://www.blogger.com/profile/06186921251648245557</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>