Click here to Skip to main content
12,507,674 members (54,181 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Stats

33.7K views
414 downloads
65 bookmarked
Posted

Presentation Model and Dependency Injection

, 26 Apr 2009 Ms-PL
ASP.NET MVVM provides a framework to implement the Presentation Model pattern, a.k.a. the Model-View-ViewModel pattern in ASP.NET projects. Developers can take advantages of Dependency Injection and Event Broker to write concise, elegant and business focused code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web;

namespace Demo.Web.Mvvm
{
    internal class ReflectionCache
    {   
        private static readonly object locker = new object();

        internal static PropertyInfo[] GetProperties(Type type, Type attributeType)
        {
            string key = string.Format("{0}-{1}-{2}", type.Module.Name, type.FullName,
                attributeType == null ? "" : attributeType.ToString());

            PropertyInfo[] properties = HttpRuntime.Cache[key] as PropertyInfo[];
            if (properties == null)
            {
                lock (locker)
                {

                    if (attributeType == null)
                    {
                        properties = type.GetProperties();
                    }
                    else
                    {
                        var props = from property in type.GetProperties()
                                    where property.GetCustomAttributes(attributeType, false).Length > 0
                                    select property;

                        properties = props.ToArray();
                    }
                    
                    HttpRuntime.Cache[key] = properties;
                }
            }

            return properties;
        }


        internal static MethodInfo[] GetMethods(Type type, Type attributeType)
        {
            string key = string.Format("{0}-{1}-{2}", type.Module.Name, type.FullName,
                attributeType == null ? "" : attributeType.ToString());

            MethodInfo[] methods = HttpRuntime.Cache[key] as MethodInfo[];
            if (methods == null)
            {
                lock (locker)
                {
                    if (attributeType == null)
                    {
                        methods = type.GetMethods();
                    }
                    else
                    {
                        var mthds = from method in type.GetMethods(
                                         BindingFlags.DeclaredOnly | BindingFlags.Public | 
                                         BindingFlags.NonPublic | BindingFlags.Instance)
                                    where method.GetCustomAttributes(attributeType, false).Length > 0
                                    select method;

                        methods = mthds.ToArray();
                    }
                    HttpRuntime.Cache[key] = methods;
                }
            }

            return methods;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Yiyi Sun
Architect
Canada Canada
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 26 Apr 2009
Article Copyright 2009 by Yiyi Sun
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid