Friday, August 2, 2013

Caller Information - .NET 4.5

Pretty slick way to implement INotifyPropertyChanged, without having to pass in property names (string parameters).

http://msdn.microsoft.com/en-us/library/hh534540.aspx

public class Notifiable : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
 
public void OnPropertyChanged([CallerMemberName] string property = null)
{
//get an instance of the current handle(s)
var handler = PropertyChanged;

if (handler != null && property != null)
{
var args = new PropertyChangedEventArgs(property);
handler(this, args);
}
}
 }

Then simply call OnPropertyChanged() in your property setter:


private decimal input1;
public decimal Input1
{
get { return input1; }
set
                                {
                                                if (value != input1)
                                                {
                                                                input1 = value;
                                                                OnPropertyChanged();
                                                }
                                }
                }

Tuesday, December 20, 2011

Compiling MVC Views

One of the initial complaints I had working with MVC 3 was that the views don't compile at runtime.

Came across this today, thanks Phil!

http://haacked.com/archive/2011/05/09/compiling-mvc-views-in-a-build-environment.aspx

In Summary:
Open up your .csproj file and update the MvcBuildViews value: <MvcBuildViews>true</MvcBuildViews>

Saturday, April 16, 2011

Lazy: What it is and why you should be using it

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

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.



sqlcmd -S <MachineName>\<InstanceName> -E

CREATE LOGIN [WEBERNET\TWEBER] FROM WINDOWS
GO

exec sp_addsrvrolemember @loginame='WEBERNET\TWEBER', @ROLENAME='sysadmin'
go
 
4.  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)); 
}
/// <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

}

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:
                          

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.

C# fileText File





Edit: If the above links are not working, try this folder:
http://cid-edc8bd4a11230d7b.office.live.com/browse.aspx/.Public/blog

Travis