Data Access has been discontinued. Please refer to this page for more information.

How to: Modify The Behaviour Of The Service Methods

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

Suppose you have a ServiceStack layer based on a model created after the Northwind sample database, and this layer was generated with Service Wizard. Suppose also that you need to modify the Post method of CategoryServiceImplementation, so that it performs an additional check of the content in the AddCategory message. You can do this with the help of the next steps:

  1. In the project which holds the code base of the service layer, add a new class file.
  2. In this file define the CategoryServiceImplementationExtended class, which inherits CategoryServiceImplementation, and implements ICategoryServiceImplementation.
  3. In the CategoryServiceImplementationExtended class, override the default implementation of the Post method, and provide the necessary check in its body. For example:

    public partial class CategoryServiceImplementationExtended : 
        CategoryServiceImplementation, 
        ICategoryServiceImplementation
    {
        public CategoryServiceImplementationExtended(ICategoryRepository repository)
            : base(repository) { }
        public override HttpResult Post(AddCategory request)
        {
            //
            //The additional code for the request check 
            //
            if (request.Category == null)
            {
                throw new HttpError(HttpStatusCode.BadRequest, 
                    "No Category is provided", 
                    "The category property of the request is NULL");
            }
            //
            return base.Post(request);
        }
    }
    
    Partial Public Class CategoryServiceImplementationExtended
        Inherits CategoryServiceImplementation
        Implements ICategoryServiceImplementation
        Public Sub New(ByVal repository As ICategoryRepository)
            MyBase.New(repository)
        End Sub
        Public Overrides Function Post(ByVal request As AddCategory) As HttpResult
            '
            'The additional code for the request check
            '
            If request.Category Is Nothing Then
                Throw New HttpError(HttpStatusCode.BadRequest, _
                                    "No Category is provided", _
                                    "The category property of the request is NULL")
            End If
            '
            Return MyBase.Post(request)
        End Function
    End Class
    
  4. In the web application that holds your ServiceStack layer, add a new class file called AppHost.partial.cs(.vb). In it you will extend the ServiceStack service host, in order to register the new service implementation. This requires you to implement the ApplyCustomIocContainerServiceImplementationsConfiguration method. For example:

    namespace ServiceStack.Demo
    {
        public partial class AppHost
        {
            partial void ApplyCustomIocContainerServiceImplementationsConfiguration(Funq.Container container, 
                ref bool shouldRegisterDefaultServiceImplementations)
            {
                shouldRegisterDefaultServiceImplementations = false;
                container.RegisterAutoWiredAs<CategoryServiceImplementationExtended, 
                    ICategoryServiceImplementation>().ReusedWithin(ReuseScope.None);
                //
                //TODO: The configuration for the rest of the services in the service layer goes here. 
                //
            }
        }
    }
    
    Partial Public Class AppHost
        Private Sub ApplyCustomIocContainerServiceImplementationsConfiguration(ByVal container As Funq.Container,
            ByRef shouldRegisterDefaultServiceImplementations As Boolean)
            shouldRegisterDefaultServiceImplementations = False
            container.RegisterAutoWiredAs(Of CategoryServiceImplementationExtended,  _
                ICategoryServiceImplementation)().ReusedWithin(ReuseScope.None)
            '
            'TODO: The configuration for the rest of the services in the service layer goes here.
            '
        End Sub
    End Class
    
  5. The final step is to locate the ApplyIocContainerServiceImplementationsConfiguration method in the generated AppHost class, and to copy the registrations of all service implementations (if any), except the registration for CategoryServiceImplementation. At this point the AppHost class in AppHost.partial.cs(.vb) should look similar to this:

    namespace ServiceStack.Demo
    {
        public partial class AppHost
        {
            partial void ApplyCustomIocContainerServiceImplementationsConfiguration(Funq.Container container, 
                ref bool shouldRegisterDefaultServiceImplementations)
            {
                shouldRegisterDefaultServiceImplementations = false;
                container.RegisterAutoWiredAs<CategoryServiceImplementationExtended,
                    ICategoryServiceImplementation>().ReusedWithin(ReuseScope.None);
                container.RegisterAutoWiredAs<CurrentProductListServiceImplementation, 
                    ICurrentProductListServiceImplementation>().ReusedWithin(ReuseScope.None);
                container.RegisterAutoWiredAs<EmployeeServiceImplementation, 
                    IEmployeeServiceImplementation>().ReusedWithin(ReuseScope.None);
                container.RegisterAutoWiredAs<InvoiceServiceImplementation, 
                    IInvoiceServiceImplementation>().ReusedWithin(ReuseScope.None);
                container.RegisterAutoWiredAs<ProductServiceImplementation, 
                    IProductServiceImplementation>().ReusedWithin(ReuseScope.None);
            }
        }
    }
    
    Partial Public Class AppHost
        Private Sub ApplyCustomIocContainerServiceImplementationsConfiguration(ByVal container As Funq.Container,
            ByRef shouldRegisterDefaultServiceImplementations As Boolean)
            shouldRegisterDefaultServiceImplementations = False
            container.RegisterAutoWiredAs(Of CategoryServiceImplementationExtended,  _
                ICategoryServiceImplementation)().ReusedWithin(ReuseScope.None)
            container.RegisterAutoWiredAs(Of CurrentProductListServiceImplementation,  
                ICurrentProductListServiceImplementation)().ReusedWithin(ReuseScope.None)
            container.RegisterAutoWiredAs(Of EmployeeServiceImplementation,  
                IEmployeeServiceImplementation)().ReusedWithin(ReuseScope.None)
            container.RegisterAutoWiredAs(Of InvoiceServiceImplementation,  _
                IInvoiceServiceImplementation)().ReusedWithin(ReuseScope.None)
            container.RegisterAutoWiredAs(Of ProductServiceImplementation,  _
                IProductServiceImplementation)().ReusedWithin(ReuseScope.None)
        End Sub
    End Class
    
  6. Build your solution and run it for a test.