Search code examples
design-patternsoopmulti-tier

What's the common way for OOP Pattern design (Data Access)


Originally there was the DAL object which my BO's called for info and then passed to UI. Then I started noticing reduced code in UI and there were Controller classes. What's the decent recomendation.

I currently structure mine

Public Class OrderDAL

    Private _id Integer
    Private _order as Order

    Public Function GetOrder(id as Integer) as Order

        ...return Order

    End Function

End Class

then I have controller classes (recently implemented this style)

Public Class OrderController

    Private Shared _orderDAL as new OrderDAL

    Public Shared Function GetOrder(id) As Order

        Return _orderDAL.GetOrder(id)

    End Function

End Class

Then in my application

My app Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        msgbox(OrderController.GetOrder(12345).Customer.Name)

    End Sub


End app

I originally found that with the Shared Class I didn't have to keep creating a new instance of the DAL whenever I need to fetch data

Dim _orderDAL as New OrderDal

_orderDAL.GetOrder(1234)

.....

What's your take?

Thanks


Solution

  • I've used your solution in the past, and the only problem I faced is that "Shared" or "static" methods don't support inheritance. When your application grows, you might very well need to support different types of "OrderControllers".

    The estabilished way of supporting different OrderControllers would be, in theory, to create a factory:

    OrderControllerFactory.ConfiguredOrderController().GetOrder(42);
    

    The problem here is: what type is returned by "ConfiguredOrderController()"? Because it must have the static "GetOrder(int id)" method -- and static methods are not supported by inheritance or interfaces. The way around this is not to use static methods in the OrderController class.

    public interface IOrderController
    {
        Order GetOrder(int Id)
    }
    
    public class OrderController: IOrderController
    {
        public Order GetOrder(int Id)
        {}
    }
    

    and

    public class OrderControllerFactory()
    {
        public IOrderController ConfiguredOrderController()
        {}
    }
    

    Hence, you will probably be better off by using non-static methods for the controller.