locked
What is Repository vs Service pattern RRS feed

  • Question

  • User264732274 posted

    i know Repository pattern but not aware about Service pattern. so tell me with example what is Service pattern and how it is similar like Repository pattern?

    both are used for same purpose ?

    discuss if there is any difference between Service pattern and Repository pattern.

    when people go for Service pattern ? thanks

    Wednesday, September 16, 2015 9:51 AM

Answers

  • User-821857111 posted

    Both the previous people who have replied have linked to articles about the Service Locator pattern, which is not what you meant to ask about, I suspect. My understanding of your question is that you are asking about a service layer instead.

    The original definition of a service layer is one that exposes services to third parties via SOAP or HTTP over the web. You will also see examples of the Service layer in an MVC application implemented as a business logic layer. The service layer in such an application will make calls to the repository. You can see a good example of this in the open source nopCommerce application (http://www.nopcommerce.com/). Here's part of the code for the BlogService class:

    namespace Nop.Services.Blogs
    {
        public partial class BlogService : IBlogService
        {
            #region Fields
    
            private readonly IRepository<BlogPost> _blogPostRepository;
            private readonly IRepository<BlogComment> _blogCommentRepository;
            private readonly IRepository<StoreMapping> _storeMappingRepository;
            private readonly CatalogSettings _catalogSettings;
            private readonly IEventPublisher _eventPublisher;
    
            #endregion
    
            #region Ctor
    
            public BlogService(IRepository<BlogPost> blogPostRepository,
                IRepository<BlogComment> blogCommentRepository,
                IRepository<StoreMapping> storeMappingRepository,
                CatalogSettings catalogSettings, 
                IEventPublisher eventPublisher)
            {
                this._blogPostRepository = blogPostRepository;
                this._blogCommentRepository = blogCommentRepository;
                this._storeMappingRepository = storeMappingRepository;
                this._catalogSettings = catalogSettings;
                this._eventPublisher = eventPublisher;
            }
    
            #endregion
    
            #region Methods
    
            /// <summary>
            /// Deletes a blog post
            /// </summary>
            /// <param name="blogPost">Blog post</param>
            public virtual void DeleteBlogPost(BlogPost blogPost)
            {
                if (blogPost == null)
                    throw new ArgumentNullException("blogPost");
    
                _blogPostRepository.Delete(blogPost);
    
                //event notification
                _eventPublisher.EntityDeleted(blogPost);
            }
    
            /// <summary>
            /// Gets a blog post
            /// </summary>
            /// <param name="blogPostId">Blog post identifier</param>
            /// <returns>Blog post</returns>
            public virtual BlogPost GetBlogPostById(int blogPostId)
            {
                if (blogPostId == 0)
                    return null;
    
                return _blogPostRepository.GetById(blogPostId);
            }
    
            // rest of code removed for brevity
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 2:45 AM
  • User-821857111 posted

    i have no idea how service layer would interact with repository?

    The code shows that clearly. Repositories are injected into the service class via its constructor. You would call the service function from inside the controller action method. It will call repository method after it has executed any required business logic.

    service layer and repository both classes will be there in same project or in same solution hosted in same pc or different pc?
    That is a deployment decision and has nothing to do with the actual design patterns. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 8:56 AM

All replies

  • User1080785583 posted

    https://msdn.microsoft.com/en-us/library/ff648968.aspx

    Very well explained by Microsoft.

    Wednesday, September 16, 2015 11:33 AM
  • User1644755831 posted

    Hello Sudip_inn,

    A little difficult to exactly tell in the words but after little bit of searching I think below article has what you needs.

    Design patterns and practices in .NET: the Service Locator anti-pattern

    It gives us nice example using customer locator service and customer repository.

    With Regards,

    Krunal Parekh

    Thursday, September 17, 2015 2:21 AM
  • User-821857111 posted

    Both the previous people who have replied have linked to articles about the Service Locator pattern, which is not what you meant to ask about, I suspect. My understanding of your question is that you are asking about a service layer instead.

    The original definition of a service layer is one that exposes services to third parties via SOAP or HTTP over the web. You will also see examples of the Service layer in an MVC application implemented as a business logic layer. The service layer in such an application will make calls to the repository. You can see a good example of this in the open source nopCommerce application (http://www.nopcommerce.com/). Here's part of the code for the BlogService class:

    namespace Nop.Services.Blogs
    {
        public partial class BlogService : IBlogService
        {
            #region Fields
    
            private readonly IRepository<BlogPost> _blogPostRepository;
            private readonly IRepository<BlogComment> _blogCommentRepository;
            private readonly IRepository<StoreMapping> _storeMappingRepository;
            private readonly CatalogSettings _catalogSettings;
            private readonly IEventPublisher _eventPublisher;
    
            #endregion
    
            #region Ctor
    
            public BlogService(IRepository<BlogPost> blogPostRepository,
                IRepository<BlogComment> blogCommentRepository,
                IRepository<StoreMapping> storeMappingRepository,
                CatalogSettings catalogSettings, 
                IEventPublisher eventPublisher)
            {
                this._blogPostRepository = blogPostRepository;
                this._blogCommentRepository = blogCommentRepository;
                this._storeMappingRepository = storeMappingRepository;
                this._catalogSettings = catalogSettings;
                this._eventPublisher = eventPublisher;
            }
    
            #endregion
    
            #region Methods
    
            /// <summary>
            /// Deletes a blog post
            /// </summary>
            /// <param name="blogPost">Blog post</param>
            public virtual void DeleteBlogPost(BlogPost blogPost)
            {
                if (blogPost == null)
                    throw new ArgumentNullException("blogPost");
    
                _blogPostRepository.Delete(blogPost);
    
                //event notification
                _eventPublisher.EntityDeleted(blogPost);
            }
    
            /// <summary>
            /// Gets a blog post
            /// </summary>
            /// <param name="blogPostId">Blog post identifier</param>
            /// <returns>Blog post</returns>
            public virtual BlogPost GetBlogPostById(int blogPostId)
            {
                if (blogPostId == 0)
                    return null;
    
                return _blogPostRepository.GetById(blogPostId);
            }
    
            // rest of code removed for brevity
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 2:45 AM
  • User264732274 posted

    @Mike Thanks but code is not complete rather it is idea.

    normally how people interact with repository patter. from controller action method we call repository function and repository function then interact with EF to perform CRUD.

    but i have no idea how service layer would interact with repository?

    service layer and repository both classes will be there in same project or in same solution hosted in same pc or different pc?

    would u discuss further with small sample code to show how service layer and repository interact with each other.

    thanks

    Thursday, September 17, 2015 8:00 AM
  • User-821857111 posted

    i have no idea how service layer would interact with repository?

    The code shows that clearly. Repositories are injected into the service class via its constructor. You would call the service function from inside the controller action method. It will call repository method after it has executed any required business logic.

    service layer and repository both classes will be there in same project or in same solution hosted in same pc or different pc?
    That is a deployment decision and has nothing to do with the actual design patterns. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 17, 2015 8:56 AM
  • User264732274 posted

    so you try to say we would call service layer function from mvc action method and service layer would call repository function and repository function will interact with EF to perform CRUD operation...........am i right ?

    my question why should we work with so many layer rather we can call repository function from action. what would be the advantage to design service layer when working with repository pattern. if possible give me a example where people like to have service layer and repository both.

    thanks

    Thursday, September 17, 2015 10:34 AM