Wrap IList and use it for effective binding in Silverlight and WPF
If you use some ORM tool like NHibernate and your business entities or DTOs have to support collections of other elements, you are forced to use the interface notation IList<T> or ICollection<T> to hold a reference to the real collection implementation (cause usually those ORM tools tend to inject their own implementation of the collection classes).
Such collections aren’t very well suited to be used in a binding cause they usually doesn’t support any mechanism to inform the binding object that the collection has been changed or altered, so the change is not propagated to the user interface.
You have two way to solve the problem:
1- Convert the collection to a well known ObservableCollection<T>, but in this way you’ll loose the flexibility of having an ORM for your entity cause you’re modifying its normal way to handle object instances.
2- Wrap the ‘ISomething<T>’ collection with another class that exposes all you need to be effective as a binding source for Silverlight or WPF.
I prefer to follow the second approach, cause it eliminates a lot of conversions between objects and preserves objects integrity. Here is a simple implementation of a wrapping class for a generic IList<T> collection:
1: [Serializable]
CodeRush Xpress Tips
If you use CodeRush Xpress with Windows Vista you may experience continuous crash of the environment.
To resolve this issue you can right click on the Visual Studio exe (or link in the startup menu) and go to the compatibility tab.
Then put a check on ‘Disable visual theme’ and ‘Execute as administrator’...the translation may not be accurate cause my OS is in Italian (look at the picture below).
Plus CodeRush Xpress do not appear to have any menu attached to the normal menu bar of Visual Studio 2008. To open its control panel you can use the following key combination:
ctrl+alt+shift+o
Related Content
- NUnitit: Visual Studio Addin to support NUnit and some customizations to it (26/08/2015)
- Resharper and the OutOfMemoryException problem (26/08/2015)
- Spot Hidden Exceptions using IntelliTrace (26/08/2015)
- Visual Studio 2010 designer error: Value cannot be null. Parameter name: objectType (26/08/2015)
- VS2008 SP1 + PowerCommands = Toolbox AddItems...Crash! (26/08/2015)
- How to update your TFS Workspace after CodePlex upgrade to TFS2010 (26/08/2015)
- More related document (8)
Silverlight: simulate a ‘Windows’ desktop application - part 3 (Resizable Window)
It’s time to write the last chapter of this series and take a look at how the resizing capability of the Window is implemented. Since the last time some new feature were added:
- Code ported to Silverlight 2.0 RTW.
- Solution refactored, the control is now available as a standalone assembly.
- The template of the Window has been revised to correct some bugs.
- We can now resize from any side and corner of the window.
- We have support for automatic scrolling if we set the minimum size (MinWidth and MinHeight) of the framework element contained into the window control.
Here’s the actual result:
Like in the previous article, in which we talked about dragging the window around, the basic idea is to subscribe to any mouse event raised by the control and to write then code there to handle the resizing.
Resizing the window is a bit more complicated than dragging it around, so more code will be required.
We start with a consideration: we want to keep our template very very simple so, to avoid having some ‘hidden’ framework elements that defines the hotspot zones that the user can use to resize the window, we just get the current mouse position and we do some computations to see if it’s near the borders or the corners of the window (hotspots); if this condition is satisfied we can identify which side or corner the user is going to resize.
Then, when the user moves the mouse around, if we are in resizing mode, we compute the new window size and position and we update the dimensions of the external control and of the inner content presenter.
Let’s start with the xaml style template (some code was stripped):
1: <Style TargetType="windows:Window">
Silverlight Plug-in and the Height Percentage problem in Firefox
Today I added a small Silverlight application to an existing and styled website, the app was surrounded by the usual <div>:
1: <div style="height: 100%">
Some Code Analysis and Refactoring Utilities you may like
As a developer I always look for some utilities that can help raise my productivity bar and avoid common bugs. I always go for open-source projects and free software first, cause we all know that in many cases we have a very limited budget to invest. So here is some GREAT pieces of software that can help you.
FxCop (http://blogs.msdn.com/fxcop/)
A very good tool for code analysis, your code will be checked against a wide rage of rule sets that cover almost everything. It can be integrated with Visual Studio 2008.
Microsoft StyleCop (http://blogs.msdn.com/sourceanalysis/)
StyleCop analyzes C# source code to enforce a set of style and consistency rules, it can be integrated and run directly inside Visual Studio.
PowerCommands for Visual Studio 2008 (http://code.msdn.microsoft.com/PowerCommands)
It is a set of useful extensions for the Visual Studio 2008 adding additional functionality to various areas of the IDE.
CodeRush Xpress (http://devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/)
A free refactoring tool for C# language with limited functionalities that integrates a the default refactoring features exposed by Visual Studio 2008. It cannot be used with Visual Studio Express.
Refactor! for Visual Basic (http://devexpress.com/Products/Visual_Studio_Add-in/VBRefactor/)
A free refactoring tool for Visual Basic .NET, it cannot be used with Visual Studio Express.
Refactor! for ASP.NET (http://devexpress.com/Products/Visual_Studio_Add-in/RefactorASP/)
A free refactoring tool for ASP.NET, it cannot be used with Visual Studio Express.
Note that CodeRush Xpress, Refactor! for Visual Basic and Refactor! for ASP.NET are limited versions of full products and they cannot be installed together, so you have to choose and use the one that matches best your actual developing environment.
Related Content
- DotNetMarche Workshop material - Refactoring 2 the max (26/08/2015)
- Changing the blog RSS feed (26/08/2015)
- Get SharePoint Designer 2007 for Free (04/03/2009)
- The importance of having a License (10/09/2008)
- More related document (1)
NHibernate: how to control Delete Rule and Update Rule in a foreign key when using SchemaExport
If in your project you let NHibernate generate the database from the information you provide with the schema mappings (a thing extremely useful, especially for testing cause you can build up the database in engines like SQLite), sometimes you may need to control some attribute in a foreign key of a table.
Here is a sample mapping:
1: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >