Action<T> - "Encapsulates a method that takes a single parameter and does not return a value". There are additional versions for two, three and four parameters:
It can be used with method like this:
1: public static void UpdatePage(PageData page, Action<PageData> action)
2: {
3: var writableClone = page.CreateWritableClone();
4: action(writableClone);
5: DataFactory.Instance.Save(writableClone, SaveAction.Publish);
6: }
To updated page in EPiServer it's required to create writable clone first, then we can apply our changes and finally instance of PageData has to be saved. This is a procedure which you can probably find in many places in your projects. With delegates you can easily "close" standard steps into a single method and pass changeable part as a parameter, like this:
1: PresenterUtils.UpdatePage(CurrentPage,
2: p =>
3: {
4: p.Property["MetaAuthor"].Value = "Marek";
5: p.Property["MainBody"].Value = "Hello World!";
6: });
You can also use delegates which return a value:
- Func<TResult>
- Func<T, TResult>
- Func<T1, T2, TResult>
- Func<T1, T2, T3, TResult>
- Func<T1, T2, T3, T4, TResult>
In this example, method returns default value if property doesn't exist or is not set. If property is set then its value can be formatted in some special way:
1: public static string GetProperty<T>(PageData page, string propertyName, string defaultValue, Func<T, string> customAction)
2: {
3: if (page == null || string.IsNullOrEmpty(propertyName) || page[propertyName] == null)
4: {
5: return defaultValue;
6: }
7:
8: return customAction((T) page[propertyName]);
9: }
1: public string GetFormattedDate(PageData page, string format)
2: {
3: return PresenterUtils.GetProperty<DateTime>(page, "EventDate", "", x => x.ToString(format));
4: }
In the above example we are interested in EventDate property, if this property is not set then empty string (default value) will be returned, otherwise DateTime will be formatted appropriately.
Of course, in pre-ASP.NET 3.5 era things like this were also possible but it was required to define your own delegates etc. Now you can use those which are available for you in the framework. Personally I really like lambda expressions and this "way" of crating APIs, it's much more readable for me therefore I encourage you to give it a try! :)
4 comments:
Thanks you so much i now understand how to apply delegates and "Action" in my code !
I loved your article!! It has given me plenty to think about moving. Starting a business start with a logo.
Buy custom logo design
this is a great article thanks for sharing it with us...
we design the best websites & logos with a discount also with a guarantee are you interested?
Logo Designers
Thank you for sharing it with us.
I am really happy to see this blog.
It is very helpful for me.
Nursing assignment help
Post a Comment