A Layered ASP.NET MVC Application – Part III

Posted: April 12, 2009 in .NET, ASP.NET, WCF
Tags: ,

Building the Service

In my last post I have explained step by step how we can implement the Repository pattern with ADO.NET Entity Framework.In this post we will see how to expose that functionality as a WCF service.But prior to that let us briefly touch upon the Service Interface pattern to get our concepts clear.While developing enterprise application most of the times we try to decouple the Business Logic components from the UI components so that the two can evolve and change independently of each other.Along with this the ability to scale out the application across multiple nodes is also another driver.This pattern addresses these forces and provides a solution where we expose our business functionality via a Web Service which can be accessed by multiple clients over the network using varied protocols.

Now we will see step by step how to develop the WCF based Service Interface for our Bookmark Repository.

Step 1 – Create the Service Contract

As we know to develop a WCF service we need to define the service interface with ServiceContract attribute, and operations marked with OperationContract attribute as shown below:

[ServiceContract]
public interface IBookmarkService {
    [OperationContract]
    Bookmarks GetBookmark(int bookmarkId);
    [OperationContract]
    IEnumerable<Bookmarks> GetBookmarkList();
    [OperationContract]
    void AddBookMark(Bookmarks bookmark);
    [OperationContract]
    void UpdateBookmark(Bookmarks bookmark);
    [OperationContract]
    void DeleteBookmark(Bookmarks bookmark);

}

Here we have the Bookmarks class parameter or return type of the methods.So we need to add the DataContract attribute to this for proper generation of xsd mapping in wsdl.This class auto generated by Entity Framework and DataContract attribute is by default added as shown below:

    [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName=”BookmarkDBModel”, Name=”Bookmarks”)]
    [global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
    [global::System.Serializable()]
    public partial class Bookmarks : global::System.Data.Objects.DataClasses.EntityObject
    {

        [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [global::System.Runtime.Serialization.DataMemberAttribute()]
        public int BookmarkId
        {
        ……..
        }
 

       ……………………………..

    }

Step 2 – Implement the Contract

The implementation of this ServiceContract will contain only wrapper calls to the BookmarkRepository as shown below:

public class BookmarkServiceImpl : IBookmarkService {

      public Bookmarks GetBookmark(int bookmarkId) {
          return new BookmarkRepositoryManager().GetBookmark(bookmarkId);
      }

      public IEnumerable<Bookmarks> GetBookmarkList() {
          return new BookmarkRepositoryManager().GetBookmarkList();
      }

      public void AddBookMark(Bookmarks bookmark) {
          new BookmarkRepositoryManager().AddBookMark(bookmark);
      }

      public void UpdateBookmark(Bookmarks bookmark) {
          new BookmarkRepositoryManager().UpdateBookmark(bookmark);
      }

      public void DeleteBookmark(Bookmarks bookmark) {
          new BookmarkRepositoryManager().DeleteBookmark(bookmark);
      }

}

Step 3 – Host the Service

I have hosted the service using a console program and used basic http binding as shown below:

using (ServiceHost host = new ServiceHost(typeof(BookmarkService.BookmarkServiceImpl))) {
    host.Open();
    Console.WriteLine(“Bookmark Service Started..”);
    Console.Read();

}

Service Model Config

<system.serviceModel>
    <bindings />
    <client />
    <behaviors>
      <serviceBehaviors>
        <behavior name=”bookmarkServiceBehavior”>
          <serviceDebug includeExceptionDetailInFaults=”true” />
          <serviceMetadata httpGetEnabled=”true” httpGetUrl=”
http://localhost:9001/BookmarkService/WSDL” />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration=”bookmarkServiceBehavior” name=”BookmarkService.BookmarkServiceImpl”>
        <endpoint address=”
http://localhost:9001/BookmarkService” binding=”basicHttpBinding
          name=”bookmarkHttpEndpoint” contract=”BookmarkService.IBookmarkService” />
      </service>
    </services>
  </system.serviceModel>

In my last post I had mentioned about the connection string setting for Entity Framework connectivity that needs to be added in this config as well.

<connectionStrings>
  <add name=”BookmarkDBEntities” connectionString = “metadata= res://*/BookmarkModel.csdl|res://*/BookmarkModel.ssdl|res://*/BookmarkModel.msl;  provider=System.Data.SqlClient; provider connection string=&quot;Data Source=TEST; Initial Catalog=BookmarkDB; User ID=sa;Password=xyz;Pooling=False; MultipleActiveResultSets=True&quot ;”  providerName=”System.Data.EntityClient” />
</connectionStrings>

So our WCF service is ready.In the next and last post we will discuss about how to consume this service from ASP.NET MVC Application.

Advertisements
Comments
  1. […] A Layered ASP.NET MVC Application – Part III « Sankarsan’s Journal […]

  2. […] to VoteA Layered ASP.NET MVC Application – Part III (4/12/2009)Sunday, April 12, 2009 from sankarsan.wordpress.comIn the next and last post we will discuss about […]

  3. […] the options for the O/R layer isolation. In the second part, he creates the repository. In the third part,  he creates the service layer. In the fourth part, he build the ASP.NET MVC application. […]

  4. rune007 says:

    Hi interesting project, but is it possible to have a look at the source code? Thank you.

  5. Kok says:

    Hi,

    Thank you for this great posts.

    I have one question:

    Your repository interface in this case is only a contract what class needs to implement. Is there any way to use in service layer? I mean with this layered structure, do you need repository interface?

    Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s