Click here to Skip to main content
5,785,816 members and growing! (19,058 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Advanced

MVP in ASP.NET with .NET Remoting

By Marcin Kawalerowicz

.NET Remoting and ASP.NET MVP. Extending Billy McCaffertys "Model View Presenter with ASP.NET".
C#, Windows, .NET 2.0, .NET, ASP.NET, Visual Studio, VS2005, Dev

Posted: 21 Aug 2006
Updated: 28 Aug 2006
Views: 21,172
Bookmarked: 33 times
Note: This is an unedited reader contribution
Announcements
Loading...



Search    
Advanced Search
Sitemap
6 votes for this Article.
Popularity: 2.45 Rating: 3.15 out of 5
0 votes, 0.0%
1
2 votes, 33.3%
2
0 votes, 0.0%
3
3 votes, 50.0%
4
1 vote, 16.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

MVP with ASP.NET and .NET Remoting

Introduction

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".

Extending MVP

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
    }

Configring .NET Remoting

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:

  • http://localhost/MvpSampleRemoteDao/OrderDao.rem?wsdl
  • http://localhost/MvpSampleRemoteDao/CustomerDao.rem?wsdl
  <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>

Points of Interest

There are some things you have to keep in mind while working with this achitecure:
  1. all the classes you intend to work over the boundries have to by serializable
  2. after you compile tne Data project you have to transfer somehow the ddl to IIS virtual directory. You could configure the Data project to output the ddls into bin subdirectory and make the Data project folder by the virtual directory. You can also write some AfterBuild task in your prj File to deploy the dlls to local/remote server, there are a lot of possibilities. For the time being you have a folder MvpSampleWeb in zip file provided with this article. You have to make it a virtual directory on your IIS.
  3. in his original article Billy came with an great idea of httpModule that opens the NHibernate session on every page served. You won't need it while working with remote objects. So you can turn this part of web.config off:
    <httpModules>
      <add name="NHibernateSessionModule" 
             type="MvpSample.Web.NHibernateSessionModule"/>
    </httpModules>
  4. There is a question weather you need special tests for your remote DAOs. Surely yes!
  5. Oh and yes, I’m deplyin the same assempbly to the remoting server and client. I’m not working with interfaces as a contract between client and server. Well that approach seves a little bit of work and after all it is server software, you don’t have to by afraid of someone else peeping into you code, right!

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Marcin Kawalerowicz



Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralSome AdditionsmemberBill Pierce10:23 21 Aug '06  
GeneralRe: Some AdditionsmemberMarcin Kawalerowicz22:49 21 Aug '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Aug 2006
Editor:
Copyright 2006 by Marcin Kawalerowicz
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project