2
\$\begingroup\$

I'm putting together some RESTful services using ASP.NET Web API. This is the first time I have done this, so I'm looking for general feedback on this controller to make sure I'm not doing anything that doesn't make sense or that will be confusing to future maintainers of the code.

The factory is brought in using MEF, it's an already existing piece of code that just brings in any implementation of an interface that is available in the MEF container.

Imports System.Net
Imports System.Web.Http
Imports myContract
Imports System.ComponentModel.Composition

<Export>
Public Class OrgVendorServicesController
    Inherits ApiController

    <Import(GetType(IMySystemBusinessObjectFactory))>
    Private MySystemFactory As IMySystemBusinessObjectFactory

    ' GET api/OrgVendorServices
    Public Function GetAllOrgVendorServices() As IHttpActionResult
        Dim ovsColl As IOrgVendorServiceCollection = MySystemFactory.GetInstance(Of IOrgVendorServiceCollection)()
        ovsColl.ReadAll()
        Return Ok(ovsColl)
    End Function

    ' GET api/OrgVendorServices/5
    Public Function GetOrgVendorService(ByVal id As Integer) As IHttpActionResult
        Dim ovs As IMySystemOrgVendorService = MySystemFactory.GetInstance(Of IMySystemOrgVendorService)()
        ovs.OrgVendorServiceId = id
        ovs.Read()
        Return Ok(ovs)
    End Function

    Public Function GetCurrentOrgVendorServiceByServiceandOrg(legacyIdNumber As String, serviceId As String) As IHttpActionResult
        Return GetOrgVendorServiceByServiceandOrg(legacyIdNumber, serviceId, Nothing)
    End Function

    Public Function GetOrgVendorServiceByServiceandOrg(legacyIdNumber As String, serviceId As String, asOfDate As DateTime?) As IHttpActionResult
        Dim ovs As IMySystemOrgVendorService = MySystemFactory.GetInstance(Of IMySystemOrgVendorService)()
        legacyIdNumber = legacyIdNumber.Replace("-", "")
        If Not Regex.IsMatch(legacyIdNumber, "^\d{8}$") Then
            Return Content(HttpStatusCode.BadRequest, New With {.Message = String.Format("{0} is not a valid legacy Id number", legacyIdNumber)})
        End If
        ovs.ReadByServicelegacyIdNumberAndDate(legacyIdNumber, serviceId, asOfDate)
        If ovs Is Nothing Then
            Return Content(HttpStatusCode.NotFound, New With {.Message = String.Format("Vendor not found for {0} for service {1}", legacyIdNumber, serviceId)})
        End If
        Return Ok(ovs)
    End Function

    Public Sub PostOrgVendorService(<FromBody()> ByVal value As IOrgVendorService)
        Dim ovs As IMySystemOrgVendorService = MySystemFactory.GetInstance(Of IMySystemOrgVendorService)()
        ovs.OrgVendorServiceId = value.OrgVendorServiceId
        ovs.Read()
        ovs.Refresh(value)
        ovs.Save()
    End Sub

    Public Sub PutOrgVendorService(ByVal id As Integer, <FromBody()> ByVal value As IOrgVendorService)
        Dim ovs As IMySystemOrgVendorService = MySystemFactory.GetInstance(Of IMySystemOrgVendorService)()
        ovs.OrgVendorServiceId = id
        ovs.Read()
        ovs.Refresh(value)
        ovs.Save()
    End Sub

    Public Sub DeleteOrgVendorService(ByVal id As Integer)
        Dim ovs As IMySystemOrgVendorService = MySystemFactory.GetInstance(Of IMySystemOrgVendorService)()
        ovs.OrgVendorServiceId = id
        ovs.DELETE()
    End Sub
End Class
\$\endgroup\$
  • \$\begingroup\$ I honestly think there's more to say about the design of IMySystemOrgVendorService than the code you've got here. \$\endgroup\$ – RubberDuck Jan 30 '16 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.