![]() |
Web Development »
ASP.NET »
General
Advanced
MVP in ASP.NET with .NET RemotingBy Marcin Kawalerowicz.NET Remoting and ASP.NET MVP. Extending Billy McCaffertys "Model View Presenter with ASP.NET". |
C#, Windows, .NET 2.0, ASP.NET, VS2005, Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This article is an extension to Billy McCafferty�s great "Model View Presenter with ASP.NET". I�ll try to go with a solution provided by Billy a little bit deeper into "enterprise". I�ll discuss the situation where you have to host the site for internet (as opposite for intranet) and you cannot place the database server inside the DMZ. Demilitarized Zone is a concept of secure zone that divides the company "safe" inside with the war that is going on outside (in internet). So from the security reasons you have to place you database somewhere � apart from your website. This solution is something half way between the SOA and tight data access and presentation layer integration. With this solution you want have to introduce one more layer to you architecture (no need for separate service layer) but you will hat a clean physical separation of layers (picture).
Please make sure that you have red and understand Billy�s article before you go further with my text.
You have to keep in mind that using this solution you loose one of the prime NHibernate feature: "lazy loading". Working over boundaries every call to remote service means new NHibernate session, that means no easy way to load things "on demand".
We will abstract the DAOs (Data Access Objects) further more to make them work over .NET Remoting. First cut the inline DAOs definition form NHibernateDaoFacotry and paste it to its own file say NHibernateDoas. The DAOs definitions form this class will by used with both scenarios remotly and locally.
public class CustomerDaoNHibernate :
GenericNHibernateDao<Customer, string>, ICustomerDao { }
public class OrderDaoNHibernate :
GenericNHibernateDao<Order, long>, IOrderDao { }
To make this classes to work outside the normal boundries you have to make them inherit from MarshalByRefObject. To make it simple we will inherit only the base class for all DAOs GenericNHibernateDao.
Now you have to write the remote DAO factory class. Let�s name it NHibernateRemoteDaoFactory. It will look like this:
public class NHibernateRemoteDaoFactory : IDaoFactory
{
/// <summary>
/// Static contructor loads the remoting
/// configuration form configuration file.
/// </summary>
static NHibernateRemoteDaoFactory()
{
RemotingConfiguration.Configure(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
false);
}
#region IDaoFactory Members
public ICustomerDao GetCustomerDao()
{
NHibernateDaos.CustomerDaoNHibernate dao =
(NHibernateDaos.CustomerDaoNHibernate)
Activator.CreateInstance(typeof(
NHibernateDaos.CustomerDaoNHibernate));
return dao;
}
public IOrderDao GetOrderDao()
{
NHibernateDaos.OrderDaoNHibernate dao =
(NHibernateDaos.OrderDaoNHibernate)
Activator.CreateInstance(typeof(
NHibernateDaos.OrderDaoNHibernate));
return dao;
}
#endregion
}
Believe it or not but are done with programming here! Really from now on you wan�t have to code. The rest will by done by configuring and customizing.
So lets configure the server. I�ll use the IIS to make it simple and plain. We will use the binary formatter and http chanell. Please create a virtual directory and place all the MvpSample.Data dlls in its bin subdirectory. To make IIS understand what you want to serialize you have to place web.config file inside. It will look like this:
<configuration>
<configSections>
<section name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System,
Version=1.0.1.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<appSettings>
<add key="HBM_ASSEMBLY" value="MvpSample.Core"/>
</appSettings>
<nhibernate>
<add key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"/>
<add key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"/>
<add key="hibernate.connection.connection_string"
value="Data Source=mk-r52;Database=testdb;Trusted_Connection=True;" />
<add key="hibernate.connection.isolation" value="ReadCommitted"/>
</nhibernate>
<system.runtime.remoting>
<application>
<channels>
<serverProviders>
<formatter ref="binary"/>
</serverProviders>
<channel ref="http">
</channel>
</channels>
<service>
<wellknown mode="SingleCall"
type="MvpSample.Data.CustomerDaoNHibernate,MvpSample.Data"
objectUri="CustomerDao.rem"/>
<wellknown mode="SingleCall"
type="MvpSample.Data.OrderDaoNHibernate,MvpSample.Data"
objectUri="OrderDao.rem"/>
</service>
</application>
</system.runtime.remoting>
<system.web>
<customErrors mode="Off">
</customErrors>
<compilation>
<assemblies>
<add assembly="System.Runtime.Remoting, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
</system.web>
</configuration>
You probably already know the first part that configures NHibernate form Billys article. The rest configures the .NET Remoting. First you are specifying the chanell and than the services you want to host. We will make the calls of SingleCall type (so no state, very much like ordinary Web Service). We will by hosting the CustomerDaoNHibernate under the name CustomerDao.rem and OrderDaoNHibernate under OrderDao.rem. If your virtual directory name is MvpSampleRemoteDao then you can try by calling the WSDL for each service like this:
<system.runtime.remoting>
<application>
<channels>
<channel ref="http">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
<client>
<wellknown type="MvpSample.Data.CustomerDaoNHibernate,MvpSample.Data"
url="http://localhost/MvpSampleRemoteDao/CustomerDao.rem" />
<wellknown type="MvpSample.Data.OrderDaoNHibernate,MvpSample.Data"
url="http://localhost/MvpSampleRemoteDao/OrderDao.rem" />
</client>
</application>
</system.runtime.remoting>
Congratulations you are done. From now on you cann switch between remoting / no remoting by editing the Windsor Container configuration file Config\CastleComponents.config.
<configuration>
<components>
<component id="daoFactory"
type="OFS.ORM.Data.NHibernateDaoFactory, OFS.ORM.Data"
service="OFS.ORM.Core.DataInterfaces.IDaoFactory, OFS.ORM.Core" />
<!--component id="daoFactory"
type="OFS.ORM.Data.NHibernateRemoteDaoFactory, OFS.ORM.Data"
service="OFS.ORM.Core.DataInterfaces.IDaoFactory, OFS.ORM.Core" /-->
</components>
</configuration>
<httpModules>
<add name="NHibernateSessionModule"
type="MvpSample.Web.NHibernateSessionModule"/>
</httpModules>
I hope this article will help you set up a great enterprise application with ASP.NET and .NET remoting!
PS. Take a look at Advancing the Model-View-Presenter Pattern - Fixing the Common Problems by Acustic - its another great extension of Billy�s solution.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 28 Aug 2006 Editor: |
Copyright 2006 by Marcin Kawalerowicz Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |