Lazy<T> allows you to write code that not only executes on an as-needed basis, but is also guaranteed to be thread safe. I've put together a demo application that includes a couple unit tests as well as console output to demonstrate using simple lazy initialization (which does work great for single-threaded apps) versus Lazy<T> initialization.
Basic lazy loading:
private object _obj;
public object MyObject
{
get {
if (_obj == null)
_obj = new object();
return _obj;
}
Lazy<T> loading:
private Lazy<object> _obj = new Lazy<object>();
public object MyObject { get { return _obj.Value; } } //Lazy<T> guarantees that this will always return the same value
Note that this simple example is using a static initializer for _obj, I've found myself initializing Lazy<T> properties in the constructor much more often than using the static initialization, but in the spirit of a demo here we are. In my demo solution I do indeed initialize the private Lazy<T> var in the constructor.
The basic lazy loading example above is not thread-safe, in a single threaded application it will behave just as you would expect when you look at the code, but in a multi-threaded application the MyObject property will often return different objects across threads. This is due to the fact that multiple threads can evaluate the 'if (_obj == null)' statement prior to _obj being initialized.
The demo I put together can be found here: http://cid-edc8bd4a11230d7b.office.live.com/self.aspx/.Public/blog/LazyT.zip
Saturday, April 16, 2011
Creating a SQL Server login (windows account) without server admin rights
The default install configuration for SQL Server used to add the builtin\admin group to the server admin role, this is no longer the case with SQL Server 2008 and often causes the user installing SQL to be the only user that has any access to SQL. Not usually an issue when you're doing your own install, but if someone else configures your developer machine(s) you can be a bit stuck.
It's been a while since I ran this, but if I recall correctly, here are the steps to get yourself added as a server admin.
1. Shutdown SQL, this can be done in the SQL Server Configuration Manager or from the command prompt (if you have UAC enabled, you will need to run your command prompt with Administrative permissions).
Example commands from technet:
net stop MSSQLSERVER or net stop MSSQL$instancename
http://technet.microsoft.com/en-us/library/ms190236.aspx
2. Start an instance of SQL running under your current windows user account, replace <MachineName> with computer name found under system properties and <InstanceName> with the name of your SQL instance. If you're using the default install settings, MSSQL is the default (SQLEXPRESS if using the express version of SQL).
This is the location of my SQL executable (and therefore where I opened my command prompt to): C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn>
In a command prompt (path set to MSSQL\Binn directory), enter:
sqlservr.exe -S <MachineName>\<InstanceName> -E
If successful, you should get a 'SQL Server' popup/dialog with an OK button, do NOT click OK until we are finished with our 2nd command prompt.3. With the command prompt still open for step 2, open a new command prompt, and execute the following script/command. Note: I'm using 'WEBERNET' for my domain and 'TWEBER' for my user in this example. I have not attempted using this script outside of a domain, but my guess is that the domain can simply be replaced with the computer name if the computer or user is not part of a domain.
It's been a while since I ran this, but if I recall correctly, here are the steps to get yourself added as a server admin.
1. Shutdown SQL, this can be done in the SQL Server Configuration Manager or from the command prompt (if you have UAC enabled, you will need to run your command prompt with Administrative permissions).
Example commands from technet:
net stop MSSQLSERVER or net stop MSSQL$instancename
http://technet.microsoft.com/en-us/library/ms190236.aspx
2. Start an instance of SQL running under your current windows user account, replace <MachineName> with computer name found under system properties and <InstanceName> with the name of your SQL instance. If you're using the default install settings, MSSQL is the default (SQLEXPRESS if using the express version of SQL).
This is the location of my SQL executable (and therefore where I opened my command prompt to): C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn>
In a command prompt (path set to MSSQL\Binn directory), enter:
sqlservr.exe -S <MachineName>\<InstanceName> -E
If successful, you should get a 'SQL Server' popup/dialog with an OK button, do NOT click OK until we are finished with our 2nd command prompt.3. With the command prompt still open for step 2, open a new command prompt, and execute the following script/command. Note: I'm using 'WEBERNET' for my domain and 'TWEBER' for my user in this example. I have not attempted using this script outside of a domain, but my guess is that the domain can simply be replaced with the computer name if the computer or user is not part of a domain.
sqlcmd -S <MachineName>\<InstanceName> -E
CREATE LOGIN [WEBERNET\TWEBER] FROM WINDOWS
GO
exec sp_addsrvrolemember @loginame='WEBERNET\TWEBER', @ROLENAME='sysadmin'
go4. And we're done, go ahead and click OK on the SQL dialog and start SQL again as normal (net start MSSQL$SQLEXPRESS).
Microsoft does have a script available for doing this same thing on SQL Express, I haven't tried it out yet though nor do I know if it also works with the standard SQL install.
http://archive.msdn.microsoft.com/addselftosqlsysadmin/
Saturday, February 5, 2011
How To: Make Your View Models Disposable
This is actually a part two post to one of my earlier posts about using reflection and custom attributes to wire up dependent property change notifications. Using anonymous methods for such a task works quite well, but I was hitting some roadblocks when trying to use that architecture with models and view models that need to implement IDisposable.
In order for an object to be cleaned up by garbage collection (GC), it cannot be subscribed to an event on an object that is still being held in memory. The problem I was running into was that I had a couple specific view models that needed to be disposed of and made available for GC, but were still wired up to property changed (and collection changed) events on our business models, which were not being disposed of [intentionally, as they were still being used by the rest of the application].
So my task at hand was to implement IDisposable in such a way that all event handlers were removed, so the object was no longer referenced in code, and GC could proceed.
Here are the major changes I made to my demo project:
//tracks events subscribed to so Dispose() can unsubscribe
private readonly List<Tuple<object, EventInfo, Delegate>>
_subscribedEvents = new List<Tuple<object, EventInfo, Delegate>>();
/// <summary>
/// Register an event handler, handlers added using this method will be removed in Dispose() method.
/// </summary>
/// <param name="publisher">Object publishing event</param>
/// <param name="eventName">name of event</param>
/// <param name="handler">event handler to add</param>
protected void RegisterEventHandler(object publisher, string eventName, Delegate handler)
{
//already registered - return immediately
if (_subscribedEvents.Any(item => item.Item1.Equals(publisher) && item.Item2.Name.Equals(eventName) && item.Item3.Equals(handler))) return;
//get event info object
var eInfo = publisher.GetType().GetEvent(eventName);
//add handler
eInfo.AddEventHandler(publisher, handler);
//track the subscription, so we can un-wire it when Dispose() is called
_subscribedEvents.Add(new Tuple<object, EventInfo, Delegate>(publisher, eInfo, handler));
}
/// Register an event handler, handlers added using this method will be removed in Dispose() method.
/// </summary>
/// <param name="publisher">Object publishing event</param>
/// <param name="eventName">name of event</param>
/// <param name="handler">event handler to add</param>
protected void RegisterEventHandler(object publisher, string eventName, Delegate handler)
{
//already registered - return immediately
if (_subscribedEvents.Any(item => item.Item1.Equals(publisher) && item.Item2.Name.Equals(eventName) && item.Item3.Equals(handler))) return;
//get event info object
var eInfo = publisher.GetType().GetEvent(eventName);
//add handler
eInfo.AddEventHandler(publisher, handler);
//track the subscription, so we can un-wire it when Dispose() is called
_subscribedEvents.Add(new Tuple<object, EventInfo, Delegate>(publisher, eInfo, handler));
}
/// <summary>
/// Disposes of the object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//remove event handlers for subscribed events
foreach (var item in _subscribedEvents)
item.Item2.RemoveEventHandler(item.Item1, item.Item3);
_subscribedEvents.Clear(); //clear the subscribed events collection, so we're not referencing other objects either
}
}
/// Disposes of the object
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
//remove event handlers for subscribed events
foreach (var item in _subscribedEvents)
item.Item2.RemoveEventHandler(item.Item1, item.Item3);
_subscribedEvents.Clear(); //clear the subscribed events collection, so we're not referencing other objects either
}
}
Working project/demo download can be found here:
Tuesday, December 7, 2010
How To: Use interface types with data templates in WPF 4
The short story is, you cannot specify interface types as a value for DataType and this is an example I put together to demonstrate one way around this.
I was recently working on a user control in a resource library project that only had interfaces defined, and was crippled by this limitation in WPF.
Use of DataType is further described here.
Custom Template Selector Class:
public class CustomTemplateSelector : DataTemplateSelector
{
public CustomTemplateSelector()
{
TemplateMappings = new Dictionary();
}
public Dictionary TemplateMappings { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var itemType = item.GetType();
var matchingKey = TemplateMappings.Keys.FirstOrDefault(k => k.IsAssignableFrom(itemType));
return matchingKey != null ? TemplateMappings[matchingKey] : null;
}
}
Example Usage for ComboBox in XAML:
<ComboBox.ItemTemplateSelector>
<lib:CustomTemplateSelector>
<lib:CustomTemplateSelector.TemplateMappings>
<DataTemplate x:Key="{x:Type sys:String}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="DemoItemColumn" />
</Grid.ColumnDefinitions>
<!--bind with no path, item is a string-->
<TextBlock Text="{Binding }" />
</Grid>
</DataTemplate>
<!--IDemoItem data template, note we could specify the actual object type here, but if we were in a
class/resource library that only defined the interfaces, we wouldn't know the type to specify-->
<DataTemplate x:Key="{x:Type lib:IDemoItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="DemoItemColumn" />
</Grid.ColumnDefinitions>
<!--bind to description property of IDemoItem-->
<TextBlock Text="{Binding Description}" />
</Grid>
</DataTemplate>
</lib:CustomTemplateSelector.TemplateMappings>
</lib:CustomTemplateSelector>
</ComboBox.ItemTemplateSelector>
Full working demo can be downloaded here:
I was recently working on a user control in a resource library project that only had interfaces defined, and was crippled by this limitation in WPF.
Use of DataType is further described here.
Custom Template Selector Class:
public class CustomTemplateSelector : DataTemplateSelector
{
public CustomTemplateSelector()
{
TemplateMappings = new Dictionary
}
public Dictionary
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var itemType = item.GetType();
var matchingKey = TemplateMappings.Keys.FirstOrDefault(k => k.IsAssignableFrom(itemType));
return matchingKey != null ? TemplateMappings[matchingKey] : null;
}
}
Example Usage for ComboBox in XAML:
<ComboBox.ItemTemplateSelector>
<lib:CustomTemplateSelector>
<lib:CustomTemplateSelector.TemplateMappings>
<DataTemplate x:Key="{x:Type sys:String}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="DemoItemColumn" />
</Grid.ColumnDefinitions>
<!--bind with no path, item is a string-->
<TextBlock Text="{Binding }" />
</Grid>
</DataTemplate>
<!--IDemoItem data template, note we could specify the actual object type here, but if we were in a
class/resource library that only defined the interfaces, we wouldn't know the type to specify-->
<DataTemplate x:Key="{x:Type lib:IDemoItem}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="DemoItemColumn" />
</Grid.ColumnDefinitions>
<!--bind to description property of IDemoItem-->
<TextBlock Text="{Binding Description}" />
</Grid>
</DataTemplate>
</lib:CustomTemplateSelector.TemplateMappings>
</lib:CustomTemplateSelector>
</ComboBox.ItemTemplateSelector>
Full working demo can be downloaded here:
Saturday, October 30, 2010
Implementing a virtual file system interface for Windows Azure Blob Storage
This example is actually an implementation of N2 CMS' IFileSystem interface, but should work for any application with the need/desire for an Azure storage solution that behaves like a standard file system.
I used this interface for file management on my Azure site that I had hosted until Azure went commercial and the site starting costing me a little too much for a personal playground... I just got notice though, Azure pricing now has a "smaller" compute package that is 55% cheaper than the previous minimum package, so I might be looking into getting an Azure site back up and running.
Disclaimer: I haven't actually ran this interface in at least a year, so there might be changes in the Azure SDK that aren't accounted for.
I hope to get this re-tested with the latest Azure SDK ASAP, let me know if you have any questions/comments.
Edit: If the above links are not working, try this folder:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog
Travis
I used this interface for file management on my Azure site that I had hosted until Azure went commercial and the site starting costing me a little too much for a personal playground... I just got notice though, Azure pricing now has a "smaller" compute package that is 55% cheaper than the previous minimum package, so I might be looking into getting an Azure site back up and running.
Disclaimer: I haven't actually ran this interface in at least a year, so there might be changes in the Azure SDK that aren't accounted for.
I hope to get this re-tested with the latest Azure SDK ASAP, let me know if you have any questions/comments.
| C# file | Text File |
Edit: If the above links are not working, try this folder:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog
Travis
Saturday, October 23, 2010
How To: Use custom attributes and reflection to clean up your models and view models
Update: I've updated my demo project, see my updated post here: http://travisjweber.blogspot.com/2011/02/how-to-make-your-view-models-disposable.html
I've put together a fairly simple example of some of the work I've done in an attempt to clean up endless property changed event handlers, switch statements, and string literals scattered throughout views and view models.
Think of a calculation that is performed on your view model, in it's getter, you simply return the result of a calculation made using values of other properties on the view model. This means though, that in order to notify the UI that the value of the calculation has changed, each property that affects the calculation must execute a property changed event for itself as well as the result property. This is pretty common practice, one set action may fire multiple property changed events.
But what if the property the calculation depends on is actually a property of another object (model) on the view model? This means you need to not only notify the result changed when the object changes, but also when a specific property of the object changes, meaning you need to wire up an event handler to the object, create a switch statement in the event handler to determine what property changed, and execute notifications accordingly.
Step 1: Define a custom attribute, used to decorate a property [usually/often read-only] that relies on one or more other properties.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
public class NotifyPropertyAttribute : Attribute
{
public string PropertyName { get; set; }
public NotifyPropertyAttribute(string propertyName)
{
PropertyName = propertyName;
}
}
Step 2: Auto-wire event handlers for property changed events in view models and models. I am using an abstract base class - NotifiableObject in my example for this. In the constructor, I am using reflection to loop through all properties of the class, when the NotifyProperty attribute is present an anonymous handler is added to the property changed event so a notification is also generated for the decorated property.
Here is the main block of code I'm using to loop through the properties with the notify property attributes, AttachChildren is a recursive method I'm using to do the dirty work, see example solution for more details.
private void WireUpBubblingPropertyChangeNotifications()
{
//this gets all properties, public and private, so you can use the INotifyPropertyChanged interface even for private fields
var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var prop in props)
{
var attrs = prop.GetCustomAttributes(true);
foreach (var attr in attrs.OfType<NotifyPropertyAttribute>())
{
AttachChildren(prop.Name, this, attr.PropertyName);
}
}
}
Sample Solution: NotifyPropertyAttributeExample.zip
Edit: Above link is not working, try downloading from here instead:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog
Feel free to shoot me an email with questions/suggestions, I still have some work to do to refine this a bit and provide more functionality. I am working on a project that has a very hierarchical data structure, and found myself constantly needing to wire up event handlers for child objects' property changed notifications, and bubble them up to parent models & view models. This approach has already cleaned up much of our code.
I've put together a fairly simple example of some of the work I've done in an attempt to clean up endless property changed event handlers, switch statements, and string literals scattered throughout views and view models.
Think of a calculation that is performed on your view model, in it's getter, you simply return the result of a calculation made using values of other properties on the view model. This means though, that in order to notify the UI that the value of the calculation has changed, each property that affects the calculation must execute a property changed event for itself as well as the result property. This is pretty common practice, one set action may fire multiple property changed events.
But what if the property the calculation depends on is actually a property of another object (model) on the view model? This means you need to not only notify the result changed when the object changes, but also when a specific property of the object changes, meaning you need to wire up an event handler to the object, create a switch statement in the event handler to determine what property changed, and execute notifications accordingly.
Step 1: Define a custom attribute, used to decorate a property [usually/often read-only] that relies on one or more other properties.
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
public class NotifyPropertyAttribute : Attribute
{
public string PropertyName { get; set; }
public NotifyPropertyAttribute(string propertyName)
{
PropertyName = propertyName;
}
}
Step 2: Auto-wire event handlers for property changed events in view models and models. I am using an abstract base class - NotifiableObject in my example for this. In the constructor, I am using reflection to loop through all properties of the class, when the NotifyProperty attribute is present an anonymous handler is added to the property changed event so a notification is also generated for the decorated property.
Here is the main block of code I'm using to loop through the properties with the notify property attributes, AttachChildren is a recursive method I'm using to do the dirty work, see example solution for more details.
private void WireUpBubblingPropertyChangeNotifications()
{
//this gets all properties, public and private, so you can use the INotifyPropertyChanged interface even for private fields
var props = GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var prop in props)
{
var attrs = prop.GetCustomAttributes(true);
foreach (var attr in attrs.OfType<NotifyPropertyAttribute>())
{
AttachChildren(prop.Name, this, attr.PropertyName);
}
}
}
Sample Solution: NotifyPropertyAttributeExample.zip
Edit: Above link is not working, try downloading from here instead:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog
Feel free to shoot me an email with questions/suggestions, I still have some work to do to refine this a bit and provide more functionality. I am working on a project that has a very hierarchical data structure, and found myself constantly needing to wire up event handlers for child objects' property changed notifications, and bubble them up to parent models & view models. This approach has already cleaned up much of our code.
Thursday, October 21, 2010
Background
I spend my time working with .NET and SQL Server technologies, currently I'm working with WPF 4 & SQL Server 2008 R2, and would eventually like to shift some [more] of my focus into the Silverlight space. Our current project is using Unity and Prism, and I spend much of my time finding creative ways to implement these technologies on large scale without compromising quality or maintainability.
I have a couple ideas/demos I will post soon, one is how I am implementing an undo handler in WPF using MVVM, and a second somewhat related, how to use reflection and property attributes to clean up your spaghetti factory of property change notifications and handlers.
Stay tuned!
Subscribe to:
Posts (Atom)