Hello everyone,
today I want to share an implementation I’m using in my personal prototyping-Framework for Silverlight. This is important to mention, because I have tested it only for “works” but not including any productive parameters like memory-usage or processing-time.
The meaning of a prototyping-framework for me is to get thinks working as quick as possible, to show the outcome to my customer. There are some things I need very often, and so I’m using base-classes a lot.
Click here to read more.. »
I like using Base-Classes for any kind of “Class-Group”. Very prominent groups are Models and ViewModels in Silverlight-Applications.
The ViewModel-Baseclass can hold any functionality, that applies to every ViewModel inheriting from it and, if we are talking about Generics, can be very convenient to use.
For example, if I want to apply the factory-pattern to my ViewModels and want every Model to have a static Create-Method I can simply implement this Method in the Base-Class.
public class ViewModelBase
{
public virtual ViewModelBase Create()
{
return new ViewModelBase();
}
}
Of cause, without Generics we are limited to the return-Type. We could now override this method in every specific ViewModel to at least return the right ancestor of ViewModelBase. An easier way is, to use Generics for this case. The class could look like this :
public class ViewModelBase<TViewModel> where TViewModel : ViewModelBase
{
public virtual TViewModel Create()
{
return Activator.CreateInstance(typeof(TViewModel));
}
}
Now we can simply create our new specific ViewModel like this
public class MyViewModel : ViewModelBase<MyViewModel>
to get what we want generically.
Another possible scenario would be the following. We know, every ViewModel contains a Collection of Models, all inheriting from a class called ModelBase. we could extend out code the following way:
public class ViewModelBase<TViewModel> where TViewModel : ViewModelBase
{
public virtual TViewModel Create()
{
return Activator.CreateInstance(typeof(TViewModel));
}
public List<ModelBase> MyList {get;set;}
}
or, the better way, we could extend our Generic header to be type safe.
public class ViewModelBase<TViewModel, TChildModel>
where TViewModel : ViewModelBase
where TChildModel : ModelBase
{
public virtual TViewModel Create()
{
return Activator.CreateInstance(typeof(TViewModel));
}
public List<TChildModel> MyList {get;set;}
}
Defining a Class by
public class MyViewModel : ViewModelBase<MyViewModel, MyModel>
Like always, if we talk about inheritance, you should avoid to go too far with that. I implemented a Framework I use for rapid prototyping where a lot of things are done in the base-class via reflection, but in a productive environment its not always what you want. Plan your development as precise as possible.
- What do you want to be individually implemented by each ViewModel ?
- When is it a good thing to implement it in the Base-Class ?
- How confusing is you inheritance for other developers ?
Good Luck
I read very often things like “Should work, cause its downward compatible”. But I’m too long in the business to trust sentences like this – so basically If I write a Silverlight 3 App for a customer I want to test it with the SL 3 Runtime, if I play around with the new SL 4 Features, I obviously need SL 4 Runtime. Are you like me ? Don’t you like VMs for these small requirements ?
Click here to read more.. »
Silverlight 4, Various Februar 16th 2010
Hello everyone,
as most of your Silverlight-Interested-People might already know, Microsoft has released the beta-version of the upcoming Silverlight Version 4. Lot of things are new, but instead of spreading it out myself, I want to post some references to really good summaries and tutorials I found.
All I really needed to do, to get started with SL 4 was (beside installing Visual Studio 2010 Beta 2 which I already had on my machine) go to this page:
GetStarted Page for Silverlight 4 beta
download an install the tools. the new runtime is in the package too.
A pretty good summary of the new features can be found on Tim Heuers Blog:
Silverlight 4 Beta – A guide to the new features
And there is also a series of articles published by Alex Golesh that are hands-on-tutorials for some of the new features
I will surely write more about this topic during the next weeks, so stay tuned
)
Sascha
Silverlight 4, Various November 22nd 2009