Introduction


Nearforums http://www.nearforums.com is a forum application built on top of ASP.NET MVC framework. Nearforums natively stores data in SQL Server or MySQL. Nearforums extension for XML data access http://nearforumsxml.codeplex.com allows the Nearforums application to store data in XML files.

The code for Nearforums extension for XML data access can be found in http://nearforumsxml.codeplex.com. The uploaded file has the complete source. The original nearforums source code can be found in http://nearforums.codeplex.com. The following steps can help you set Nearforums for XML data storage:
  1. Download the Nearforums application
  2. Download the Nearforums XML extension
  3. Update the source files in Nearforums application
  4. Append XML files in the Nearforums application

Download the Nearforums application

The Nearforums application can be downloaded from http://nearforums.codeplex.com. The application contains the following projects:
  • NearForums
  • NearForums.DataAccess
  • NearForums.Services
  • NearForums.Tests
  • NearForums.Web
  • NearForums.Web.Controllers
  • NearForums.Web.Output
Build the solution and check if it works. The default solution works with a SQL server or MySQL database. Now, we will extend this solution to work with XML data.

Download the Nearforums XML extension

The Nearforums extension for XML data access can be downloaded from http://nearforumsxml.codeplex.com. The unzipped file contains the following details:
  1. NearForums.DataAccess.Xml project
  2. Data folder (data)
  3. Referenced Library folder (lib)
  4. Source folder (src)
The NearForums.DataAccess.Xml project is the main project which has all the source files needed to store XML data. This project should be added to the NearForums solution (extracted in the step above).
The data folder (data), referenced library folder (lib), source folder (src) can be safely excluded from the solution.

Update the source files in the Nearforums solution

There are four files that needs to be modified in the original Nearforums solution:
  1. NearForums project
    1. Configuration/AppSettings.cs
    2. Configuration/SiteConfiguration.cs
  2. NearForums.Web.Output project
    1. Web.config
    2. DependenciesHelper.cs
Let us look at the changes one by one. In the SiteConfiguration.cs file, the AppSettings element is added:
       [ConfigurationProperty("appSettings")]
        public AppSettingsElement AppSettings
        {
            get
            {
                return (AppSettingsElement)this["appSettings"];
            }
            set
            {
                this["appSettings"] = value;
            }
        }
The AppSettings.cs should be added to the NearForums project under the Configuration folder. This will ensure that the DataPath - path where the XML files are stored is available to the extension.
        private string _dataPath;
        public string DataPath
        {
            get
            {
                if (_dataPath == null)
                {
                    _dataPath = ConfigurationManager.AppSettings["DataPath"];
                }
 
                return _dataPath;
            }

            set
            {
                _dataPath = value;
            }
        }

Next, we have to do a few changes in NearForums.Web.Output project. The DependenciesHelper.cs uses AutoFac to resolve the interfaces. In our case, we want to make sure that the DataAccess interfaces resolves to the classes in the NearForums.DataAccess.Xml project.
builder.RegisterAssemblyTypes(Assembly.Load("NearForums.DataAccess.Xml"))
				.Where(t => t.Name.EndsWith("DataAccess"))
				.AsImplementedInterfaces()
				.InstancePerDependency();
builder.RegisterType<XmlSettingsRepository>()
				.As<isettingsrepository>()
				.InstancePerDependency();

After we have resolved the interfaces with the classes in our project, we have to make a few changes to the web.config as well. The NearForums Membership provider should use our XmlMembership provider rather than the inbuild SqlMembership provider. The following change in Web.config ensures that this change happens:
<add type="NearForums.DataAccess.Xml.XmlMembershipProvider" name="ForumsMembershipProvider" applicationname="NearForums" passwordattemptwindow="10" minrequirednonalphanumericcharacters="0" minrequiredpasswordlength="6" maxinvalidpasswordattempts="30" passwordformat="Hashed" requiresuniqueemail="true" requiresquestionandanswer="false" enablepasswordreset="true" enablepasswordretrieval="false">
</add>
AppSettings in Web.config should also have an additional settings element - DataPath as follows:
<appSettings>
    <add key="XmlDataPath" value="" />
</appSettings>

Append XML files in NearForums application

The XML files in the data folder should be moved to the App_Data folder in NearForums.Web.Output project. The following screenshot gives an idea of how the project should look:
AppData.png
Build the solution and test. The application should now save data in XML files.

Last edited Jan 21, 2013 at 9:49 AM by vijayst, version 7

Comments

No comments yet.