0

I am trying to use spring.net in Asp.Net web forms.

I am following this article for reference https://www.codeproject.com/Articles/23213/Integrating-Spring-NET-with-ASP-NET-Web-Site?msg=5459558#xx5459558xx

But in Page_Load I am getting math and personDAO as null, not sure what I am missing.

Please let me know if you want me to share code, I am using same code in the article so haven't shared it again.

Web.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- Spring -->
        <sectionGroup name="spring">
            <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
            <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
            <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
        </sectionGroup>
    </configSections>
    <!-- Spring -->
    <spring>
        <parsers>
            <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
        </parsers>
        <context>
            <resource uri="config://spring/objects" />
        </context>
        <objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">
            <!-- You may choose any database -->
            <!--<db:provider id="DbProvider" provider="SqlServer-1.1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=F:\projects\pm\App_Data\PM9Database.mdf;Integrated Security=True;User Instance=True"/>
            <db:provider id="DbProvider" provider="SqlServer-1.1" connectionString="Data Source=.\SQLEXPRESS;"/>-->
            <db:provider id="DbProviderMySQL" provider="MySql" connectionString="Server=localhost;Database=codeproject;User ID=root;Password=root;" />
            <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
                <property name="ConfigSections" value="databaseSettings" />
            </object>
            <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
                <!-- Depending on database you want set it-->
                <property name="DbProvider" ref="DbProviderMySQL" />
                <property name="MappingAssemblies">
                    <list>
                        <value>CodeProject.DAO</value>
                    </list>
                </property>
                <property name="HibernateProperties">
                    <dictionary>
                        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
                        <entry key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
                        <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
                    </dictionary>
                </property>
            </object>

      <!-- TxManager -->
      <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
        <property name="DbProvider" ref="DbProviderMySQL" />
        <property name="SessionFactory" ref="SessionFactory" />
      </object>

            <object name="MyMathObj" type="Math, App_code" />

            <object id="PersonDAO" type="CodeProject.DAO.PersonDAO, CodeProject.DAO">
                <property name="SessionFactory" ref="SessionFactory" />
            </object>

      <object id="PersonDAOTx" type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data">
        <property name="PlatformTransactionManager" ref="HibernateTransactionManager" />
        <property name="Target" ref="PersonDAO" />

        <property name="TransactionAttributes">
          <name-values>
            <add key="Save*" value="PROPAGATION_REQUIRES_NEW" />
            <add key="SaveO*" value="PROPAGATION_REQUIRES_NEW" />
            <add key="Delete*" value="PROPAGATION_REQUIRED" />
            <add key="Update*" value="PROPAGATION_REQUIRED" />
            <add key="Query*" value="PROPAGATION_REQUIRED" />
          </name-values>
        </property>

      </object>

      <object id="FirstServiceImpl" type="CodeProject.DAO.FirstService, CodeProject.DAO">
        <property name="Message" value="Test Message" />
      </object>

      <!--Web Services-->
      <object id="FirstService" type="Spring.Web.Services.WebServiceExporter, Spring.Web">
        <property name="TargetName" value="FirstServiceImpl" />
        <property name="Namespace" value="http://myCompany/services" />
        <property name="Description" value="My First web service" />
      </object>


      <!-- Pages -->
            <object type="Default.aspx">
                <property name="Message" value="Hello from Web.Config" />
                <property name="Math" ref="MyMathObj" />
                <property name="PersonDAO" ref="PersonDAOTx" />
            </object>
        </objects>
    </spring>
    <appSettings />
    <connectionStrings />
    <system.web>
        <compilation debug="true">
            <assemblies>
                <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
                <add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /></assemblies></compilation>
        <authentication mode="Windows" />
        <httpHandlers>
            <!-- Spring Handler -->
            <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
      <add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
        </httpHandlers>
        <httpModules>
            <add name="SpringModule" type="Spring.Context.Support.WebSupportModule, Spring.Web" />
            <!-- Required for managing NHibernate session between http requests-->
            <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate12" />
        </httpModules>
    </system.web>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Spring.Core" publicKeyToken="65e474d141e25e07" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-0.0.0.20110" newVersion="0.0.0.20110" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Spring.Aop" publicKeyToken="65e474d141e25e07" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-0.0.0.20110" newVersion="0.0.0.20110" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.1.2.4000" newVersion="2.1.2.4000" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>

    </system.webServer>
</configuration>

Default.aspx.cs

public partial class _Default : System.Web.UI.Page 
{
    private string message;

    public string Message
    {
        get { return message; }
        set { message = value; }
    }

    private Math math;

    public Math Math
    {
        get { return math; }
        set { math = value; }
    }

    IPersonDAO personDAO;

    public IPersonDAO PersonDAO
    {
        get { return personDAO; }
        set { personDAO = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(message);
        Response.Write(math.add(30, 50));

        Person p = new Person();
        p.Name = "Maruf";
        personDAO.Save(p);
        Person p100 = personDAO.LoadByID(1); 
    }
}

Person.cs

namespace CodeProject.DAO
{
    public class Person : IBusinessEntity
    {
        private long id;
        private string name;
        private int versionNumber;
        //These methods should be virtual for NHibernate Mapping
        public virtual long Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual int VersionNumber
        {
            get { return versionNumber; }
            set { versionNumber = value; }
        }


        public virtual string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

}

BseDAO.cs

using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate;
using NHibernate;
using System.Collections;

namespace CodeProject.DAO
{
    public abstract class BaseDAO<EntityT, idT> : IBaseDAO<EntityT, idT>
    {
        protected HibernateTemplate hibernateTemplate;

        public ISessionFactory SessionFactory
        {
            set
            {
                hibernateTemplate = new HibernateTemplate(value);
                hibernateTemplate.TemplateFlushMode = TemplateFlushMode.Auto;
            }
        }
        public BaseDAO()
        {
        }

        public virtual EntityT LoadByID(idT id)
        {
            EntityT entity = (EntityT)hibernateTemplate.Load(typeof(EntityT), id);
            return entity;
        }

        public virtual IList LoadAll()
        {
            return hibernateTemplate.LoadAll(typeof(EntityT));
        }

        public virtual IList Load(string hsqlQuery, object[] values)
        {
            return hibernateTemplate.Find(hsqlQuery, values);
        }

        public virtual void Save(EntityT fine)
        {
            IBusinessEntity entity = (IBusinessEntity)fine;
            hibernateTemplate.Save(fine);
        }

        public virtual void SaveOrUpdate(EntityT fine)
        {
            IBusinessEntity entity = (IBusinessEntity)fine;
            hibernateTemplate.SaveOrUpdate(fine);
        }
    }
}

IBaseDAO.cs

using System;
using System.Collections.Generic;
using System.Collections;

namespace CodeProject.DAO
{
    public interface IBaseDAO<EntityT, idT>
    {
        IList LoadAll();
        EntityT LoadByID(idT id);
        IList Load(string hsqlQuery, object[] values);
        void Save(EntityT fine);
        void SaveOrUpdate(EntityT fine);
        NHibernate.ISessionFactory SessionFactory { set; }
    }
}
Johny Bravo
  • 393
  • 5
  • 26
  • 1
    Why would you use a framework that hasn't released in 5 years in a new project? – fredrik Nov 29 '17 at 09:56
  • 1
    Just to clarify @fredrik's comment - [Spring.Net](http://springframework.net/) hasn't had much love since 2012. Ninject, SimpleInjector, AutoFac, StructureMap are all actively supported, and Asp.Net Core now has a [built in IoC](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection) which can be switched out. You're also not missing out on much - Spring.Net tended to bring a lot of other opinions to the table which you don't necessarily want. CamiloT - the NRE link / close vote is rather overused IMO. – StuartLC Nov 29 '17 at 09:59
  • @mjwills :) I am using the same code in the article so I have not shared the code again. Still if you want I will share the code – Johny Bravo Nov 29 '17 at 10:02
  • 1
    @StuartLC totally agree with your views here, but we have a legacy application in Asp.Net Web forms which was built with Spring.Net, so need to fix this issue – Johny Bravo Nov 29 '17 at 10:04
  • @JohnyBravo from how you've written the question - it looks more like you're starting a new project than trying to maintain an existing... – fredrik Nov 29 '17 at 10:06
  • @fredrik yes it may seems like it, this legacy application is brought to us by another vendor and for us this is a fresh start. So we are trying to do a POC – Johny Bravo Nov 29 '17 at 10:10
  • 1
    @JohnyBravo If that is all relevant code - then it's clear that the two variables you've mentioned are still null, as I can't see them being set anywhere. As to why that is, probably an oversight or bug in the exanple project. – fredrik Nov 29 '17 at 10:14

0 Answers0