Archives

Archives / 2009 / November
  • WCF RIA Services Unity DomainServiceFactory

    Note: This post is based on WCR RIA Services PDC beta and changes can be made in a future release. 

    In this post I will show you how it’s possible to create your own DomainServiceFactory which has the responsibility to create a DomainService instance. I decided to use Unity as a Dependency Injection framework to create my DomainServices. Why do we even want to create a custom DomainServiceFactory, well, to avoid dependencies inside our DomainServices, for example here is a DomainService which has a dependency to a CustomerRepository:

     
        [EnableClientAccess()]
        public class CustomerService : DomainService
        {
    
            public CustomerPM GetCustomerByID(int customerID)
            {
                var customerRepository = new CustomerRepository();
                var customer = customerRepository.GetCustomerByID(customerID);
    
                return new CustomerPM()
                       {
                          CustomerID = customer.CustomerID,
                          FullName = customer.FirstName + " " + customer.LastName,
                          Age = customer.Age
                       };
            }
        }

     

    The code above will break the Dependency Inversion Principle, High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. We can’t use the code above in a unit-test, because it will use the CustomerRepository, so in that case we are doing an integration test. Our DomainService is now tightly coupled with the CustomerRepository. So to remove the detail (CustomerRepository) and use an abstraction, we can use the following solution:

        [EnableClientAccess()]
        public class CustomerService : DomainService
        {
            private ICustomerRepository _customerRepository;
    
            public CustomerService(ICustomerRepository customerRepository)
            {
                _customerRepository = customerRepository;
            }
    
            public CustomerPM GetCustomerByID(int customerID)
            {
                var customer = _customerRepository.GetCustomerByID(customerID);
    
                return new CustomerPM()
                       {
                           CustomerID = customer.CustomerID,
                           FullName = customer.FirstName + " " + customer.LastName,
                           Age = customer.Age
                       };
            }
        }

     

    Now the detail is removed, and we work against an abstraction (ICustomerRepository). We can now easily use this DomainServices with different kind of CustomerRepository as long as they implements the ICustomerRepository. We can now create our a test stub for the CustomerRepository when we write our unit-test etc. The problem with the code above, is that WCF RIA Services will throw an exception when it will try to create an instance of the DomainService. It will by default use the default constructor. If we add a default constructor, there is no way for the WCF RIA Services to know that we need to pass in a ICustomerRepository, so our GetCustomerByID method will throw and null reference exception when we try to call the CustomerRepository’s GetCustomerByID method. To solve this we can create our own DomainServiceFactory, where we handle the creation of the DomainService.

    To create a DomainServiceFactory we need to create a class that implements the IDomainServiceFactory interface, and implement two methods, CreateDomainService and ReleaseDomainService:

     public class MyDomainServiceFactory : IDomainServiceFactory
     {
            
            public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
            {
                var service = Global.UnityContainer.Resolve(domainServiceType) as DomainService;
                service.Initialize(context);
    
                return service;
            }
    
    
            public void ReleaseDomainService(DomainService domainService)
            {
                domainService.Dispose();
            }
    }


    The CreateDomainSerivce method will be called when WCF RIA Services will create an instance of our DomainSerivce, and the ReleaseDomainService will be called when WCF RIA Services will release it. To make the factory work, we must call the Initialize method of the DomainService we have created before returning it. The code above will use Unity to create an instance of a DomainService and if they have any dependencies, Unity will inject them if they are registered to the UnityContainter.

    To register our IDomainServiceFactory we use the DomainSerivce.Factory property, which is a static property, we can for example use it in Application_Start in global.asax to configure the use of our factory:

        public static UnityContainer UnityContainer = new UnityContainer();


    protected void Application_Start(object sender, EventArgs e) { DomainService.Factory = new MyDomainServiceFactory();
    UnityContainer.RegisterType<CustomerService>(); UnityContainer.RegisterType<ICustomerRepository, CustomerRepository>(); }


    Note: For demonstartion purpose I simply add a static UnityContainer to the Global.asax file, and in the MyDomainServiceFactory I access the UtnityContainer by writing Global.UnityContainer.

    I also register my DomainService and Repositories in Global.asax.

    Now when WCF RIA Services wants to create a DomainService, it will use the MyDomainServiceFactory, which will use Unity to create an instance of the DomainService, and CustomerRepository will be injected to the CustomerService.

    Something I want to see in the future, is that Microsoft makes sure we can create our own DomainService method Invoker, or at least add two public events, OnOperationExecuting and OnOperationExecuted, so we can simply add pre- and post conditions, or have more possibility to extend the WCF RIA Services.

    If you want to know when I publish a new blog post, you can follow me on twitter: http://www.twitter.com/fredrikn

    Read more...

  • Using the WebBrowser control and debug a Silverlight Out-of-browser (OOB) application in VS 2010

    Yesterday I played with the WebBrowser control shipped with Silverlight 4. I was trying to create a little RSS Reader for Silverlight 4, only to try out some of the new features. The WebBrowser control have two methods to display a web page, the Navigate and the NavigateToString. The Navigate takes an URI, the NavigateToString takes a string with for example HTML to be diplsyed inside of the WebBrowser window. In my case I didn’t want to use the Navigate, instead the NavigateToString so I could just grab some RSS feeds and get the body of a post and pass it to the WebBrowser control. First of all I notice that the WebBrowser control will only work in a Out-of-browser application, if we try to use it in a browser we will se the following:

    image

    Here is the XAML:

    <UserControl x:Class="SilverlightBlog.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <WebBrowser x:Name="MyBrowserControl" Width="800" Height="600" />
        </Grid>
    </UserControl>

    To enable out-of-browser we can right click on the Silverlight application and select properties in the menu, then under Silverlight, we have the “Enable running application out of browser” option, and also the button “Out-of-Browser Settings”, to set up the out-of-browser application.

    image

    Note: I have selected the “Require elevated trust when running outside the browser” checkbox. The reason to this is to be able to use the WebClient class or the WebBrowser control to navigate to any URL. If the checkbox is not selected, we can only open pages located on our own server.

    With the “Require elevated trust when running outside the browser”, I can now easy use the WebClient to request a URL anywhere in the cloud:

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
    
            if (App.Current.IsRunningOutOfBrowser)
            {
               var blogRequest = new WebClient();
               blogRequest.DownloadStringCompleted += blogRequest_DownloadStringCompleted;
               blogRequest.DownloadStringAsync(
                           new Uri("http://weblogs.asp.net/fredriknormen/rss.aspx", UriKind.Absolute));
            }
        }
    
        void blogRequest_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            MyBrowserControl.NavigateToString(e.Result);
        }
    }

    In the code I will first check if the application is currently running out-of-browser. It not, there are no reason to try to request a URL and then add the response to the WebBrowser control. As you can see I take the RSS returned from my blog and added it to the NavigateToString method of the WebBrowser. This will only show a lot of text. I only wanted to see if I could get my RSS feed. What I was planning to do is to use the XmlReader to only get the blog post content which includes HTML and pass it to the NavigateToString method. During the implementation of this example, I wanted to debug the out-of browser application. One way to do it is by attaching to the out-of-browser launcher, but there is another way we can do it in VS 2010. First we need to install our application as out-of-browser:

    image

    image

    Note: When running in a elevated trust we will get another installation window. You will also notice that we now have a timer on the Install button when we install a out-of-browser app. So we need to wait about 3 seconds before we can install an app. It will make sure people need to pay more attention to what the window are displaying.

    Now when the application is up an running we can close it, then go back to VS 2010 and go to the property window for our Silverlight project, select the Debug tab and the “Installed out-of-browser application”. Make sure your Silverlight project is not the start-up project. Then just start debugging. I love this feature!

    image


    If you want to know when I publish more posts to my blog, you can follow me on twitter: http://www.twitter.com/fredrikn

    Read more...

  • Silverlight 4 and Asynchronous Validation with INotifyDataErrorInfo

    During this week I helped a company with the design of a RIA with Silverlight. In the existing Web and Window Forms application they have a form where the users can enter an account id (or was it a customer id ;)). When they leave the TextBox they did a check if the account id already exists or not. They use their own way of showing the validation message. Now in the new application they want to use the red boxes showed up in Silverlight when a validation fails. In Silverlight 3 the validation only showed up if the bounded property of a TextBox thrown an exception in the set method. It’s not easy to manually trigger the nice red validation message box for a specific control outside the set method of a bounded property. Every call to the service layer from the Client should be asynchronous and in that way the callback can’t trigger the validation error box. With Silverlight 4 it’s now possible to notify when a validation fails when an async. method is completed. This is thanks to the INotifyDataErrorInfo interface. This interface can be used to notify the UI when an validation fails, and that can be done outside the set method of a property, for example in a async. callback method. The INotifyDataErrorInfo has the following members:

    public interface INotifyDataErrorInfo
    {
         bool HasErrors { get; }
    
         event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
         IEnumerable GetErrors(string propertyName);
    }

    The HasError is used to tell if there are any errors, the GetErrors should return a list or errors for a specific property. A property can have several errors, for example several validations added to one property. The ErrorsChanged event is the event we can use to notify the UI if there is any errors.

    The INotifyDataErrorInfo is perfect in the ViewModel of the Model View View Model pattern pattern when async. operations can take place. Here is an example of a ViewModel for a View where the user can specify an account ID:

    public class AccountViewModel : ViewModel
    {
        private string _accountID = null;
    private const string ACCOUNT_ALREADY_EXIST_ERROCODE = 100;
    private AccountContext _accountContext = new AccountContext(); public string AccountID { get { return _accountID; } set { if (_accountID != value) { var propertyName = "AccountID"; ValidateAccountAlreadyExists( value, propertyName, ACCOUNT_ALREADY_EXIST_ERROCODE, string.Format("Account with the ID {0} already exists", value)); _accountID = value; NotifyPropertyChanged(propertyName); } } } private void ValidateAccountAlreadyExists( string accountID, string propertyName, int errorCode, string errorMsg) { _accountContext.DoesAccountExists( accountID, invokeOperation => { if (invokeOperation.Value) { AddErrorToPropertyAndNotifyErrorChanges( propertyName, new ValidationErrorInfo() { ErrorCode = errorCode, ErrorMessage = errorMsg }); } else { RemoveErrorFromPropertyAndNotifyErrorChanges( propertyName, errorCode); } }, null); } }

    I use WCF RIA Services Invoke (ServiceOpertation) operation in this example for the async. call. As you can see the AccountViewModel’s set method of the AccoundID property will do a async. validation to check if the account exists. The callback of the async. call will call a AddErrorToPropertyAndNotifyErrorChanges if the account exists, and if the accounts doesn’t exists or if the validation is fixed (a user enter an ID that don’t already exists) the RemoveErrorProeprtyAndNotifyErrorChanges will be called to remove an validation error message if it’s already added and displayed of the user. The Add and Remove methods are added to the ViewModel base class, only to reduce code and also to reuse code among different ViewModels. The following is the ViewModel base class implementation:

    public class ViewModel : INotifyPropertyChanged, INotifyDataErrorInfo
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
    
        private Dictionary<string, List<ValidationErrorInfo>> _errors = 
    new Dictionary<string, List<ValidationErrorInfo>>(); protected void RemoveErrorFromPropertyAndNotifyErrorChanges(
    string propertyName,
    int errorCode) { if (_errors.ContainsKey(propertyName)) { RemoveErrorFromPropertyIfErrorCodeAlreadyExist(propertyName, errorCode);
    NotifyErrorsChanged(propertyName); } } private void RemoveErrorFromPropertyIfErrorCodeAlreadyExist(
    string propertyName,
    int errorCode) { if (_errors.ContainsKey(propertyName)) { var errorToRemove = _errors[propertyName].SingleOrDefault(
    error => error.ErrorCode == errorCode); if (errorToRemove != null) { _errors[propertyName].Remove(errorToRemove);




    if (_errors[propertyName].Count == 0)
    _errors.Remove(propertyName);
    } } }

        protected void AddErrorToPropertyAndNotifyErrorChanges(
    string propertyName,
    ValidationErrorInfo errorInfo) { RemoveErrorFromPropertyIfErrorCodeAlreadyExist(propertyName, errorInfo.ErrorCode);
            if (!_errors.ContainsKey(propertyName))
                _errors.Add(propertyName, new List<ValidationErrorInfo>());
    
            _errors[propertyName].Add(errorInfo);
    
            NotifyErrorsChanged(propertyName);
        }
    
    
        public IEnumerable GetErrors(string propertyName)
        {
            if (!_errors.ContainsKey(propertyName))
                return _errors.Values;
    
            return _errors[propertyName];
        }
    
    
        public bool HasErrors
        {
            get { return this._errors.Count > 0; }
        }
    
        
        private void NotifyErrorsChanged(string propertyName)
        {
            if (ErrorsChanged != null)
                ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
        }
    
    
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    Note: I have also implemented the INotifyPropertyChanged interface into the ViewModel class, but it has nothing to do with the error notification, only to reuse code in the ViewModels.

    As you can see the GetErrors method will return a List of ValdationErrorInfo object for a specific property.

    public IEnumerable GetErrors(string propertyName)
    {
        if (!_errors.ContainsKey(propertyName))
            return _errors.Values;
    
        return _errors[propertyName];
    }

    Because the GetErrors method returns an IEnumerable for a specific property, I have added a Dictionary where the key is the name of the property and the values are a List of ValidationErrorInfo objects:

    private Dictionary<string, List<ValidationErrorInfo>> _errors = 
    new Dictionary<string, List<ValidationErrorInfo>>();


    Note: The GetErrors could simply return a list of strings containing the error messages, but because I want an easy way to remove an validation error after a user enter a valid account ID, I have added a ErrorCode property to hold an unique value of a specific error. At the end of this post, you will se the simples implementation of the INotifyDataErrorInfo, where only one validation error message is added to one property.


    The ValidationErrorInfo is a class which I have created to hold some validation errors, here is the code for the ValidationErrorInfo and as you can see the ToString is overridden, this is because the validation feature added to Silverlight will use the ToString method of the objects passed from the GetErrors method, and when it does, I want it to return the validation error message.

    public class ValidationErrorInfo
    {
        public int ErrorCode { get; set; }
    
        public string ErrorMessage { get; set; }
    
        public override string ToString()
        {
           return ErrorMessage;
        }
    }


    The HasErrors method added to the ViewModel class, returns true if the _errors Dictionary has any items added. If so there are some errors.

    public bool HasErrors
    {
        get { return this._errors.Count > 0; }
    }

    The NotifyErrorsChanged method added to the ViewModel class is a helper method to trigger the ErrorsChanged event for a specific property. When this event is trigged the UI will be notified if there are errors or not.

    private void NotifyErrorsChanged(string propertyName)
    {
        if (ErrorsChanged != null)
            ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
    }

    The Add and Remove methods will add or remove a ValidationErrorInfo from a specific property:

    protected void RemoveErrorFromPropertyAndNotifyErrorChanges(
                    string propertyName,
                    int errorCode)
    {
        if (_errors.ContainsKey(propertyName))
        {
            RemoveErrorFromPropertyIfErrorCodeAlreadyExist(propertyName, errorCode);
            NotifyErrorsChanged(propertyName);
        }
    }
    
    private void RemoveErrorFromPropertyIfErrorCodeAlreadyExist(
    string propertyName,
    int errorCode) { if (_errors.ContainsKey(propertyName)) { var errorToRemove = _errors[propertyName].SingleOrDefault(
    error => error.ErrorCode == errorCode); if (errorToRemove != null) {
                 _errors[propertyName].Remove(errorToRemove);




    if (_errors[propertyName].Count == 0)
    _errors.Remove(propertyName);
    } } } protected void AddErrorToPropertyAndNotifyErrorChanges( string propertyName, ValidationErrorInfo errorInfo) {
    RemoveErrorFromPropertyIfErrorCodeAlreadyExist(propertyName, errorInfo.ErrorCode);



    if (!_errors.ContainsKey(propertyName)) _errors.Add(propertyName, new List<ValidationErrorInfo>()); _errors[propertyName].Add(errorInfo); NotifyErrorsChanged(propertyName); }


    The Remove method takes two arguments, propteryName and errorCode. First I passed a ValidationErrorInfo object instead of a errorCode, but I thought it was just unnecessarily  to create a new ValidtionErrorInfo object when only the errorCode is needed to locate the ValidationErrorInfo which should be removed from the _errors Dictionary. The Add method takes a propertyName to which the ValidationErrorInfo object should be added to. The add method will first check if there is already an added validation with the same code to the _errors Dictionary, if so it will remove it so it will be repleced with the new validation error of the same kind. For example if the user enter a invalid UserID, the validation till be added to the _errors, it the user doesn't correct the error and write a new invalid userid, that previouse validation must be replaced with the new one.  If there aren’t any errors already added to the specific property, a new List of ValidationErrorInfo is created. The ValidationErrorInfo is then added to the _errors dictionary where the key is the property name. Both Add and the Remove methods will make a call to the NotifyErrorsChanged helper method to update the UI.

    Here is the XAML for the View which uses the AccountViewModel:

    <UserControl x:Class="SilverlightBlog.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wm="clr-namespace:SilverlightBlog"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <wm:AccountViewModel x:Name="accountViewModel"/>
        </UserControl.Resources>
    
        <Grid DataContext="{StaticResource accountViewModel}" ... >
            <TextBlock Text="Account ID:" ... />
            <TextBox Text="{Binding AccountID, Mode=TwoWay, NotifyOnValidationError=True}" ... />
        </Grid>
    </UserControl>


    To make sure the UI will display the validation error, we need to set the Binding expression’s NotifyValudationError property to true.

    Note: I Have not added one single of code to the code-behind, all logic is added to the ViewModel for separation of concerns.

    The following shows the nice validation error box after a async. call is made and the account already exists:

    image

    Here is the DomainService with the Invoke operation and the Account DTO if you are interested, I only fake the data access in the code:

    [EnableClientAccess()]
    public class AccountService : DomainService
    {
        [Invoke]
        public bool DoesAccountExists(string accountID)
        {
            if (accountID == "12345")
                return true;
    
            return false;
         }
    }
    
    
    public class Account
    {
        [Key]
        public string AccoutnID { get; set; }
    }

    Here is a short and simple example code where only one error message is added to a property, only to demonstrate how easy it’s to use the INotifyDataErrorInfo interface and the concept:

    public class SimpleModel : INotifyDataErrorInfo
    {
        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
    
        private Dictionary<string, List<String>> _errors = new Dictionary<string, List<String>>();
    
        private string _accountID = null;
    
        public string AccountID
        {
            get { return _accountID; }
            set
            {
                if (_accountID != value)
                {
                    var propertyName = "AccountID";
    
                    if (string.IsNullOrEmpty(value))
                    {
                        if (!_errors.ContainsKey(propertyName))
                            _errors.Add(propertyName, new List<string>());
    
                        _errors[propertyName].Add("AccountID can't be null or empty");
                    }
                    else
                    {
                        if (_errors.ContainsKey(propertyName))
    _errors.Remove(propertyName); } NotifyErrorsChanged(propertyName); //Maybe you don't want to set this field to a value if the validation fails _accountID = value; } } } public System.Collections.IEnumerable GetErrors(string propertyName) { if (_errors.ContainsKey(propertyName)) return _errors[propertyName]; return _errors.Values; } public bool HasErrors { get { return _errors.Count > 0; } } private void NotifyErrorsChanged(string propertyName) { if (ErrorsChanged != null) ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName)); } }

    If you want to know when I publish new blog posts or simply want to follow my life etc, you can find me on twitter: http://www.twitter.com/fredrikn

    Read more...

  • WCF RIA Services – What you need to know when creating DTO/Presentation Model

    Note: This is based on the WCF RIA Services Beta, so the code in this post can change in a future release of the framework.

    When defining DTO or a “Presentation Model” (Not the Presentation Model pattern by Martin Fowler, a model suited for presentation purpose, not all entities are) there are things you may want to now. The following is an simple DTO:

    Read more...

  • WCF RIA Services and DTO with association

    This post will be short, I notice that some people are asking about how to send an object graph which will include an association to other objects. First of all, be careful with the distribution of an object graph, wrong design can affect performance in a way that to much data is passed over the wire.

    Here is an simple Order class, this class has OrderRows:

    Read more...

  • Silverlight 4 Commanding enables ViewModels

    One feature out of many really great feature shipped with Silverlight 4 Beta, is Commanding. With Commanding we can use the MVVM (Model View View Model) Pattern. Commanding is something that WPF has and preview Silverlight doesn’t. Commanding can only be used on ButtonBase controls and Hyperlink control at the moment, and will only be executed by the Click event. In this post I’m going to show how you can use the new Commanding feature and create use the MVVM pattern in Silverlight 4. Here is my simple UI:

    ui


    I have a TextBox and a Button. The ViewModel will represent this view but in code. The main reason to use a ViewModel is because of separation of concerns. The complex UI logic is moved from the View into a class (the ViewModle). The ViewModel for the View above will look like this:

    public class CustomerViewModel : INotifyPropertyChanged
    {
         public event PropertyChangedEventHandler PropertyChanged;
    
         private string _firstName;
    
         public string FirstName
         {
             get { return _firstName; }
             set
             {
                 //Validation code
                _firstName = value;
    
                //NotifyPropertyChange
             }
         }
    
         public ICommand Save
         {
            get { return new SaveCommand(this); }
         }
     }


    The FirsrtName property represents the TextBox in the View, the Save property is the Command that should be executed when the Save button is clicked. As you can see the Save member is a property which will return a ICommand, in this case the SaveCommand. Here is the SaveCommand class:

    public class SaveCommand : ICommand
    {
        private CustomerViewModel _view;
    
        public SaveCommand(CustomerViewModel view)
        {
            _view = view;
        }
    
        public bool CanExecute(object parameter)
        {
                return true;
        }
    
        public event EventHandler CanExecuteChanged;
    
        public void Execute(object parameter)
        {
             //Do something like saving the Customer
         }
    }


    The SaveCommand implements the ICommand interface which has three members, the CanExecute property, CanExecuteChanged event and the Execute method. The CanExecute return true or false if the command can be executed. The method takes one parameter, this parameter comes from the CommandParamter attribute added to the Button or Hyperlink. The Execute method, is the method that will be executed if the Command can be executed, it’s where you can add your code to perform something when the user clicks the Save button, for example using WCF RIA Services to save the Customer. When the Command and the ViewModel is in place, we can just bind the ViewModel to a DataContext and then use normal Binding to bind to our ViewModel, like this:

    <UserControl x:Class="SilverlightApplication3.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:SilverlightApplication3">
        <UserControl.Resources>
            <vm:CustomerViewModel x:Name="myViewModel"/>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" DataContext="{StaticResource myViewModel}">
            <TextBlock  Text="First Name:" ... />
            <TextBox Text="{Binding FirstName, Mode=TwoWay}" .../>
            <Button CommandParameter="test" Command="{Binding Save}" Content="Save"  ..." />
        </Grid>
    </UserControl>


    As you can see the CustomerViewModel is added to the UserControl’s Resources and then added to the LayoutRoot’s DataContext. The TextBox is bind to the FirstName property of the ViewModel and the Command attribute of the Button is bind to the Save property. I just added the CommandParameter to the code so you can see how it can be used to pass a parameter into the CanExecute and Execute method.

    I hope the Silverlight team will also add a way to specify an ViewModel method to any kind of events and where we don’t need to implement the ICommand interface. The Commanding often results in many Command classes.

    Read more...

  • Is WCF RIA Services ready for the Enterprise?

    Today Microsoft released the WCF RIA Services Beta, it’s now on top of WCF and by default uses binary data end points and data contract serialization. By using binary data end points we will get better performance and make the data sent over the wire smaller (The other preview versions uses pure JSON). WCF RIA Services is a great framework for Rapid Application Development (RAD) of 2-tier applications. Based on the new changes to the WCF RIA Services it’s defiantly ready for the Intranet and small applications, but how about the Enterprise. First of all if you uses the Entity Framework or Linq to SQL Domain Services, it’s not ready for the Enterprise and will never be, IMO, because when we use it it distribute DAL types to the client. They are useful for small data driven applications. BUT! WCF RIA Services with the DTO (Data Transfer Object) support will make it closer to be ready for the Enterprise, but is that enough?

    It’s complex to build RIA (Rich Internet Application) and require a lot of plumbing. This is something WCF RIA Services is trying to solve. Just remember there is a reason why things are complex, and what every framework developers are trying to do, is to hide the complexity by adding a new level of abstraction. The thing is, the complexity is still there. At first I was skeptic about WCF RIA Services, but it was because the very early previous wasn’t that good, it was ok, good ideas but works had to be done. Now after almost 2 years it starts to look like something promising.

    I have notice several of developers using .WCF RIA Services and because it’s so easy to use, they seems to forgot about the network. What the WCF RIA Services Team have struggle with, is to make sure developers should be aware of the network. I know that even how much they try, some developers will use it in a wrong way. That is nothing they can do anything about. But is that so bad? NO! It gives the people that use it in the right way job opportunists ;)

    I wrote that WCF RIA Services with its DTO support will make it closer to be ready for the Enterprise and the uses of EF and L2S Domain Services are not a good choose when it comes to build Enterprise RIA. There are actually one advantage of using the EF and L2S DomainServices, and that is the way of passing a query from the client to the server. The query will be used when EF and L2S are querying the database (as far as I know, I hope it does at least ;)). It will results in less data sent from the data source. When using DTO the query will take place after we retrieve all the data from our DomainService query methods. But still on the server. But the good thing is that there is a solution to make sure the query also takes place when we query our data source and that is by overriding the DomainService Query method, it has the description of the query passed from the Client. But we had to interpret it and transform it to a query which can query our domain model.

    It looks like developers loves the data annotations added to WCF RIA Services. The validation part is great when it comes to the uses of EF and L2S Domain Services, because the DAL types are generated from the tools (hmm, thinking of the Devils is in the tools ;)), and it’s “hard” to add validation to it. In that case with WCF RIA Services we can use annotation to add validations, which will be available both on the client and the server. Isn’t that great? DTO also support validations buy adding annotations. You have to have in mind, that this validation can’t be tested with Unit Test, it’s the Framework which will execute it. But you also have to remember that DTO is just what is says an object which transfers data. On the client-side we may use a ViewModel which will handle the validation instead.

    Back to the main question, is WCF RIA Services ready for the Enterprise? If I say Yes, it may be a lie, if I say No, it’s definitely a lie. I think it’s ready, or at least I would give it a try. I think the team have made a great framework, I will with no doubt recommand it to others.

    Read more...

  • How to create a Module based Silverlight application (Part 1)

    When building RIA (Rich Internet Application) with Silverlight, it’s important to make sure the Silverlight application is loaded as fast as possible. Users don’t like to see a splash screen for several seconds or minutes, they want an application to start directly. One way to make a Silverlight app load fast, is by minimizing the XAP file, this can be done by not adding to much images, videos or other files to the Silverlight application project. If we have a large application, we can also split the application into small “modules”, for example assemblies or .XAP files. We can then load them on demand when they are needed. The base application can be kept small and be the core engine for the other modules. In this first part of my blog post I will add some examples how we can split our Silverlight applications into modules, and load them on demand asynchronous. The next part will be about how to use MEF (Managed Extensibility Framework) to create module bases Silverlight application.

    Read more...

  • Fluent-API to add ActionFilters to Controllers – ASP.NET MVC Part 3

    The latest source code for my Fluent-API to add Action Filters to Controllers is no available here. If you haven’t read about my test project, you can find the other posts here:

    Fluent-API to add ActionFilters to Controller in ASP.NET MVC

    Fluent-API to add ActionFilters to Controllers – ASP.NET MVC Part 2

    To use the Fluent-API, you only need to change the Default Controller factory to the ActionFilterConfigControllerFactory, then just configure your controller within the Global.asax. Here is an example where the OutputCache is added to two Action methods, and also an example how to add one or many ActionFilters to one Action method. You can also add Action Filters on a Controller level so they will be used on every Action methods. If you also want a specific Action Filter for all Controllers, you can just Configure the Controller type and add the filters to it.

    protected void Application_Start()
    {
                ControllerBuilder.Current.SetControllerFactory(typeof(ActionFilterConfigControllerFactory));
    
                RegisterRoutes(RouteTable.Routes);
                RegisterActionFilters();
    }
    
            
    private void RegisterActionFilters()
    {
                // Add OutputCache to all Action Methods for all Controllers which inherits the Contoller class
                //ConfigActionFilter.ConfigController<Controller>()
                //  .AddFilterToController(new OutputCacheAttribute() { Duration = 10, VaryByParam = "none" });
    
                ConfigActionFilter.ConfigController<HomeController>()
                                .AddFilterToController(new HandleErrorAttribute())
                                .AddFilterToActions(new OutputCacheAttribute() { Duration = 10, VaryByParam = "none" },
                                                    c => c.About(),
                                                    c => c.Index());
    
                ConfigActionFilter.ConfigController<AccountController>()
                                  .AddFilterToAction(c => c.LogOn(), 
    new HandleErrorAttribute(),
    new MyCustomFilterAttribute()); }

    This is as I wrote only a test project, and I do like the idea to add cross-cutting concerns by not adding attributes directly to the Controllers source code.

    Read more...

  • .NET 4.0 is so Lazy

    With .NET 4.0 there is a new class added to the System namespace called Lazy<T>. This class is what the name says, lazy. Here is an example where Lazy is used:

    var lazy = new Lazy<IList<OrderRow>>(
                                    () =>
                                    {
                                            var rows = //get order rows;
                                            return rows;
                                    });
    
    var rows = lazy.Value;


    The Lazy<T>’s constructor can take a Func<T> as an argument, the function passed as an argument to the contractor will first be invoked when the Value property of the Lazy<T> class is used, but not invoked the next time the Value property is used. The code above will first execute the function passed as an argument when the we request the value of the Lazy<T>, the returned value of the function will be cached. The next time Value is used, the function will not be invoked, instead the cached value will be returned. This class  can for example be used when we want some kind of Lazy Loading.

    I’m on twitter: http://www.twitter.com/fredrikn

    Read more...

  • Fluent-API to add ActionFilters to Controllers – ASP.NET MVC Part 2

    I’m working with my Fluent-API for adding Action Filters to Controllers and Action Methods. In my previous post, I created a new instance of each Action Filter and add it to an Action Method or Controller. Based on how the Action Filters are often implemented they don’t or shouldn’t keep any state, so in that case I don’t need to create a new instance of the same Action Filter with the same configuration for each Action Method I want to add it to. I also want to have an option to have a better overview of which Action Filter is added to which Controllers and Action Methods. I have added to methods, AddFilterToControllers and AddFilterToActions:

                .AddFilterToActions(new HandleErrorAttribute(),
                                    c=> c.About(),
                                    c=> c.Index());
                .AddFilterToActions(new OutOfMemoryException(),
                                    c=> c.About());
                 .AddFilterToActions(new HandleErrorAttribute() { ... },
                                    c=> c.MyMethod());
    
    

    The code is not yet available due to some more changes and testing.

    Read more...

  • Fluent-API to add ActionFilters to Controller in ASP.NET MVC

    Note: The name of the classes and the methods are just temporary and may change, I’m so bad when it comes to naming classes and methods. The source code is simple and haven’t done so much refactoring etc. Just wanted to see if I could get it to work, so please have that in mind.

    EDIT: Working with new methods to get a better overview of Action Filters added and also reusing AcitonFitlers, you can read about it here. 


    When we create controllers for our ASP.NET MVC application we can also add Action Filters to handle cross-cutting concerns, like Authorization, Error handling and Caching etc. If we want to have Error handling on every controller we need to add the HandleErrorAttribute to all controllers, like this:


    [HandleError] 
    public MyController : Controller 
    { 
    }


    By adding Action Filters by using attributes it can be hard to get a good overview of which controllers that has the HandleErrorAttribute. The same regarding Action methods, for example if we have a lot of Controllers and want to see see all the Action methods that uses for example the OutputCacheAttribute, we need to go through all Controllers and methods, there is no easy way to get a simple overview of them.

    Adding Action Filters to Action methods and Controllers also add some sort of “dependency” to action filters (not a big deal, though). I decided to try a way to add Action Filters to Controllers and Action methods in one single file, so I could get a better overview of which Controllers and Action methods uses what ActionFilter etc. Because ActionFitlers contains cross-cutting concerns I also wanted to move it away as attributes from the Controllers and Action methods so developers don’t need to care about the cross-cutting concerns during the creation of Controllers. instead add them later.

    I sort of used a Fluent-API for the configuration of ActionFilters, and the configuration is added to the Global.asax’s Application_Start event. Here is an example where I add the ErroHandler Action Filter to all Controllers Action Methods:


    ConfigActionFilter.ConfigController<Controller>() 
                      .AddFilterToController(new HandleErrorAttribute());
    


    If I want to add an Action Filters to a specific Controller I just use the type of the Controller:


    ConfigActionFilter.ConfigController<HomeController>()
                                  .AddFilterToController(new HandleErrorAttribute());

     

    Note: The reason I don’t use XML and a XML meta data provider as a configuration is because I wanted to make the configuration type safe. If we use XML we can only get an exception if we spell something wrong at runtime.


    If I want to add Action Filters to Action Methods I can write something like this:

     
    ConfigActionFilter.ConfigController<HomeController>()
                                  .AddFilterToController(new HandleErrorAttribute())
                                  .AddFilterToAction(c => c.About(),
                                                     new MyActionFilterAttribute(),
                                                     new MyOtherActionFilter() { Metadata=10 })
                                  .AddFilterToAction(c => c.Index(),
                                                     new MyActionFilterAttribute());

     

    I didn’t wanted to use a string for the ActionMethod (AddFilterToAction(“About”)) because it would not be type safe. I want to get a warning or error while typing the code, so instead I created an Expression. The AddFilterToAction takes a params of FilterAttributes, so I can easy add several of Action Filters to an Action Method. The AddFilterToController method will add Action Filter to the Controller, just like adding an Action Filter attribute to the class definition, so all Action Methods within the Controller will use the Action Filter.

    I created a Custom ControllerFactory so I could add my Custom ControllerActionInvoker to the Controller:


     public class ActionFilterConfigControllerFactory : DefaultControllerFactory
     {
            public override IController CreateController(RequestContext requestContext, string controllerName)
            {
                 ...
                    controllerInstance.ActionInvoker = new ActionFilerConfigControllerActionInvoker();
    
                return controller;
            }
     }

     

    The custom ControllerActionInvoker will make sure the Action Filter added by using my solution is added to the FilterInfo class. This is done by overriding the GetFilters method:


    public class ActionFilerConfigControllerActionInvoker : ControllerActionInvoker
    {
            protected override FilterInfo GetFilters(ControllerContext controllerContext,
                                                     ActionDescriptor actionDescriptor)
            {
                var filters = base.GetFilters(controllerContext, actionDescriptor);
    
                ...
    
                if (ConfigActionFilter.Config.ContainsKey(controllerName))
                    AddFiltersToFilerList(actionDescriptor, filters, controllerName);
    
                return filters;
            }

    }

     

    This project only a fun thing to do and I like the idea of having different options to add Action Filters, and this solution will make the Controller clean from Attributes and also have one place to add them. When I create my Controller I don’t need to worry or think about the cross-cutting concerns, I just add them later and into the Global.asax.

    I want to thanks Mikael Söderström for taking time to discuss this solution with him, and get some feedback and also “host” the source code for me.

    You can download the source code with an example here: http://vinkr.net/misc/ActionFilterConfig.zip

    Read more...

  • Visual Studio 2010 Launch Countdown Sidebar Widget



    Here is a sidebar widget that will show the countdown for Visual Studio 2010, launching on March 22, 2010.

    <style type="text/css">

    div#widget { position: relative; width: 250px; height: 155px; }

    body ul#cntdwn { width: 250px; height: 80px; background: transparent url(http://toysfortweets.com/visualstudiowidget/cntdwn-bg.png) no-repeat scroll left top; list-style-type: none; text-align: center; padding: 74px 0 0 0; margin: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; }

    body ul#cntdwn li { float: left; margin: 0 8px 0 0; background: transparent url(http://toysfortweets.com/visualstudiowidget/number-bg.png) no-repeat scroll left top; padding: 0 0 5px 7px; }

    body ul#cntdwn li.first { margin-left: 20px; }

    body ul#cntdwn li em { display: block;  color: #111; font-size: 1.6em; font-style: normal; font-weight: bold; background: transparent url(http://toysfortweets.com/visualstudiowidget/number-cap.png) no-repeat scroll right top; padding: 3px 7px 0 0; height: 35px; margin-bottom: -5px; }

    div#widget a#link { display: block; width: 250px; height: 155px; position: absolute; top: 0; left: 0; }

    </style>
    <script language="javascript" src="http://toysfortweets.com/visualstudiowidget/countdown.js" mce_src="http://toysfortweets.com/visualstudiowidget/countdown.js"> </script>

    Read more...