Wednesday, December 19, 2012

Creating the .NET Solution Part 1


Let's now start with implementation and for that let's create an empty .NET solution called Fishbook under which we can start creating various layers designed in the form of .NET projects.

Presentation layer

In the Fishbook solution, create a new ASP.NET web application project, that will act as a presentation layer. We call it Web. Once the project is created you will see a structure as following:


Web Project
Web Project
Let's quickly look at this in detail:
  • Account folder: This folder contains the files for user login, user registration, and password change. The user authentication logic uses the asp. net membership provider, but we will be replacing this with custom authentication logic.
  • App_Data folder: This folder is to hold any application-specific data files like typically the membership provider's database.
  • Scripts folder: This folder contains jQuery 1.4.1 related files.
  • Styles folder: This folder contains the Site.css, the style sheet that contains the various styles that the site uses.
  • Site.Master: This is the asp.net master page that will provide the common look and feel for the web application and all other aspx pages will be content place holders for various sections within this page.
  • Global.asax: The global application level code that has code for application start and end and session start and end, apart from error handling routine.
  • Default.aspx: This is the application start page.
  • About.aspx: This contains the AboutBox kind of values for our web application.
  • Web.Config: This is the standard web configuration file, but in ASP.NET 4 project you will see multiple configuration files, one per build configuration. So you will have web.config.debug and web.config.release configuration files.
We will alter some of the structure and add/delete files as we go along.

Components layer

Let us discuss common functionalists that are needed across application such as logging, caching, email, etc. Each application will have specific logging and, caching requirements which can be satisfied with specific implementation. Hence it is best to build these points as extensibility points so that the existing implementation can be replaced with different implementation of the same as need be. Such plugin-plugout points in application are referred to as extensibility points. As discussed earlier, we
have chosen MEF to build extensibility in our core framework. To achieve this, we have bundled each of the extensibility points e.g. Caching, Email, Configuration, Logging, etc. as Interfaces in Interfaces project and implementation for the same in Components project, both are of type .NET class library. Let's look at some of the components that we will need in our site.
To get true extensibility, each of the plug-ins (Configuration, Caching, Email, etc.), should be in a respective dedicated class library project, so that we can replace any one component without affecting anything else. However, for our current site this will be an overkill for this application hence we decided to include implementation for all these in one Components project.

Configuration

For the configuration utility, we want to create something that is capable of returning a strongly typed item out of our configuration source. Of course, this configuration source will initially be the standard web.config. As the web.config only holds string values, our configuration object will be responsible for casting out the appropriate type.
  1. Right click in Fishbook solution and select Add/New Project
    Interfaces Class Library Project
    Interfaces Class Library Project
    1. Select C# Class Library from C# templete and add Interfaces as name of project in Fishbook solution .
    2. Delete Class1.cs file and right click in Interfaces project and select Add/New Item.
      IConfiguration.cs Interface
      IConfiguration.cs Interface
      1. Select Interface Item from C# Template and name it IConfiguration.cs.
      2. In the IConfiguration.cs Add The folowing Code:

        using System;
        namespace Fishbook.Interfaces
        {
            public interface IConfiguration
            {
                object GetConfigurationSetting(Type expectedType, string key);
            }
        }
      3. Right click in Fishbook solution and select Add/New Project 
        Components Class Libaray Project
        Components Class Libaray Project
        1. Select C# Class Library from C# templete and add Components as name of project in Fishbook solution 
        2. Right click in References and select Add References from .NET select:
          System.Activities
          System.ComponentModel.Composition
          System.Configuration
          System.Web
        3. Right click in References and select Add References from Projects select:
          Interfaces
        4. Delete Class1.cs file and right click in Components project and select Add/New Item
          Configuration.cs Class
          Configuration.cs Class
          1. Select Class Item from C# Template and name it Configuration.cs .
          2. In the Configuration.cs Add The folowing Code:

            using System;
            using System.ComponentModel.Composition;
            using System.Configuration;
            using Fisharoo.Interfaces;

            namespace Fishbook.Components
            {
                [Export(typeof(IConfiguration))]
                public class Configuration : IConfiguration
                {
                    public object GetConfigurationSetting(Type expectedType, string key)
                    {
                        string value = ConfigurationManager.AppSettings.Get(key);
                        if (value == null)
                        {
                            throw new Exception(string.Format("AppSetting: {0} is not configured.", key));
                        }

                        try
                        {
                            if (expectedType.Equals(typeof(int)))
                            {
                                return int.Parse(value);
                            }

                            if (expectedType.Equals(typeof(string)))
                            {
                                return value;
                            }

                            throw new Exception("Type not supported.");
                        }
                        catch (Exception ex)
                        {
                            throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.", key, expectedType),
                                                ex);
                        }
                    }
                }
            }
            Notes:
            As you can see, from the start we have included a MEF attribute above the class definition (Export) so that we can easily swap out this for another one if needed. For MEF support.
          3. The Configuration class currently exposes only a single method GetConfigurationSetting() to retrieve application configuration entries in a generic manner. It communicates with the config file using the ConfigurationManager and gets the string value that was specified by the key that was passed in. Rest of the code is fairly self-explanatory. We test whether the retrieved value is null, and throw an error if it is. We then try to return the expected type by parsing as int or string. If the expected type is neither int nor string, then we throw an error stating that the requested type is not supported.

          No comments:

          Post a Comment

          Automatic Traffic Exchange

          YallaTech Facebook page