- Now to deal with data, Spring.NET provides NHibernate and ADO.NET support.
- To NHibernate handles all things about database and work in the object world only.
- Now download Spring.Net source and find bin folder and load Spring.Core, Spring.Web,Spring,Spring.Data,Spring.Data.NHibernate12 and NHibernate from lib folder.
Let us take an example and understand the complete process. For hibernate i defined a table in the database named employee, create a new class named Employee and a NHibernate mapping file (Employee.hbm.xml). Reference: Custom software development company:
Employee Class
using System;
namespaceMyProject.DAO
{
public class Employee : ICodeEntity
{
private long idno;
private string fname;
privateint age;
//These methods should be virtual for NHibernate Mapping
public virtual long Id
{
get { return idno; }
set { idno = value; }
}
public virtual int Age
{
get { return age; }
set { age = value; }
}
public virtual string FName
{
get { return fname; }
set { fname = value; }
}
}
}
Employee.hbm.xml
<?xmlversion="1.0"encoding="utf-8" ?>
<hibernate-mappingxmlns="urn:nhibernate-mapping-2.2"namespace="MyProject.DAO"assembly="MyProject.DAO">
<classname="Employee"table="tblemp">
<idname="Id">
<columnname="ID"not-null="true"/>
<generatorclass="increment" />
</id>
<versioncolumn="Age"name="Age" />
<propertyname="FName">
<columnname="FirstName"length="20"not-null="true" />
</property>
</class>
</hibernate-mapping>
Now using base class implements basic functionality of database like LoadALL,SaveOrUpdateetc. Define an Interface and implement.
using System;
usingSystem.Collections.Generic;
usingSystem.Collections;
namespaceMyProject.DAO
{
publicinterfaceIMainDAO<EntityT, idT>
{
IListGetAll();
EntityTGetByID(idT id);
IListLoad(stringhsqlQuery, object[] values);
void Save(EntityT fine);
voidSaveOrUpdate(EntityT fine);
NHibernate.ISessionFactorySessionFactory{ set; }
}
}
Implementation of interface
MainDAO
using System;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSpring.Data.NHibernate;
usingNHibernate;
usingSystem.Collections;
namespaceMyProject.DAO
{
publicabstractclassMainDAO<EntityT, idT> : IMainDAO<EntityT, idT>
{
protectedHibernateTemplatehibernateTemplate;
publicISessionFactorySessionFactory
{
set
{
hibernateTemplate = newHibernateTemplate(value);
hibernateTemplate.TemplateFlushMode = TemplateFlushMode.Auto;
}
}
publicMainDAO()
{
}
publicvirtualEntityTGetByID(idT id)
{
EntityT entity = (EntityT)hibernateTemplate.Load(typeof(EntityT), id);
return entity;
}
publicvirtualIListGetAll()
{
returnhibernateTemplate.LoadAll(typeof(EntityT));
}
publicvirtualIList Load(stringhsqlQuery, object[] values)
{
returnhibernateTemplate.Find(hsqlQuery, values);
}
publicvirtualvoid Save(EntityT fine)
{
ICodeEntity entity = (ICodeEntity)fine;
hibernateTemplate.Save(fine);
}
publicvirtualvoidSaveOrUpdate(EntityT fine)
{
ICodeEntity entity = (ICodeEntity)fine;
hibernateTemplate.SaveOrUpdate(fine);
}
}
}
By Adding a new property to Default.aspx code file named EmployeeDAO we have to use the interface of DAO class here.
Default.aspx code
using System;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingMyProject.DAO;
publicpartialclass_Default : System.Web.UI.Page
{
privatestringmsg;
publicstringMsg
{
get { returnmsg; }
set { msg = value; }
}
privateMathmathx;
publicMathMaths
{
get { returnmathx; }
set { mathx = value; }
}
IEmpDAOempDAO;
publicIEmpDAO EMPDAO
{
get { returnempDAO; }
set { empDAO = value; }
}
protectedvoidPage_Load(object sender, EventArgs e)
{
Response.Write(msg);
Response.Write(mathx.add(10, 20));
Employee e1 = newEmployee();
e1.FName = "Martin Parmar";
EMPDAO.Save(e1);
Employee e416 = EMPDAO.GetByID(1);
}
}
Now configuration of hibernate in web.xml file
<?xmlversion="1.0"?>
<configuration>
<configSections>
<!-- Spring -->
<sectionGroupname="spring">
<sectionname="context"type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
<sectionname="objects"type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
<sectionname="parsers"type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<!-- Spring -->
<spring>
<parsers>
<parsertype="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data"/>
</parsers>
<context>
<resourceuri="config://spring/objects"/>
</context>
<objectsxmlns="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:providerid="DbProviderMySQL"provider="MySql"connectionString="Server=localhost;Database=persondb;User ID=root;Password='';"providername="MySql.Data.MySqlClient"/>
<objecttype="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<propertyname="ConfigSections"value="databaseSettings"/>
</object>
<objectid="SessionFactory"type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<!-- Depending on database you want set it-->
<propertyname="DbProvider"ref="DbProviderMySQL"/>
<propertyname="MappingAssemblies">
<list>
<value>MyProject.DAO</value>
</list>
</property>
<propertyname="HibernateProperties">
<dictionary>
<entrykey="hibernate.connection.provider"value="NHibernate.Connection.DriverConnectionProvider"/>
<entrykey="hibernate.dialect"value="NHibernate.Dialect.MySQLDialect"/>
<entrykey="hibernate.connection.driver_class"value="NHibernate.Driver.MySqlDataDriver"/>
</dictionary>
</property>
</object>
<!--TxManager-->
<objectid="HibernateTransactionManager"type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
<propertyname="DbProvider"ref="DbProviderMySQL"/>
<propertyname="SessionFactory"ref="SessionFactory"/>
</object>
<objectname="MyMathObj"type="Math, App_code"/>
<objectid="EmployeeDAO"type="MyProject.DAO.EmployeeDAO, MyProject.DAO">
<propertyname="SessionFactory"ref="SessionFactory"/>
</object>
<objectid="EmployeeDAOTx"type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data">
<propertyname="PlatformTransactionManager"ref="HibernateTransactionManager"/>
<propertyname="Target"ref="EmployeeDAO"/>
<propertyname="TransactionAttributes">
<name-values>
<addkey="Save*"value="PROPAGATION_REQUIRES_NEW"/>
<addkey="SaveO*"value="PROPAGATION_REQUIRES_NEW"/>
<addkey="Delete*"value="PROPAGATION_REQUIRED"/>
<addkey="Update*"value="PROPAGATION_REQUIRED"/>
<addkey="Query*"value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>
<!-- Pages -->
<objecttype="Default.aspx">
<propertyname="Msg"value="Hello!!! I am from webconfig"/>
<propertyname="Maths"ref="MathObj"/>
<propertyname="EmployeeDAO"ref="EmployeeDAOTx"/>
</object>
</objects>
</spring>
<appSettings/>
<connectionStrings/>
<system.web>
<compilationdebug="true">
<assemblies>
<addassembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<addassembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<authenticationmode="Windows"/>
<httpHandlers>
<!-- Spring Handler -->
<addverb="*"path="*.aspx"type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
<addverb="*"path="*.asmx"type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
</httpHandlers>
<httpModules>
<addname="SpringModule"type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
<!-- Required for managing NHibernate session between http requests-->
<addname="OpenSessionInView"type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate12"/>
</httpModules>
</system.web>
</configuration>
Above source code sample is from a custom software development company.
I like your blog, I read this blog please update more content on hacking, further check it once at Dot NET Online Course Hyderabad
ReplyDelete