none
Static Class as the Data Provider: Is This a Good Idea? RRS feed

  • Question

  • Dear Friends,

    I have CodeFirst work-flow. Rather than accessing the plain and simple db.Cars.Add() in my controller action, I am thinking about a static "Data" provider class, which provides an API for regular CRUD operations. Proposed API is something like:

    DataProvider.Add(Poco)

    DataProvider.Delete(Poco)

    The entire application is intended to be of medium to large scale. My question is, is it preferable to use this kind of static class based API? I initially thought of regular non-static DataProvider, but thought later that there is no point in "new"ing it up only to get Pocos from the database.

    Your views are most awaited.

    Regards,

    Vivekanand

    Saturday, August 16, 2014 11:26 AM

Answers

  • >> is it preferable to use this kind of static class based API?

    No, in the general case you shouldn't use a static class for this purpose, you should use a non-static class instead. One of the benefits of using a non-static class is that you could use dependency injection in the controllers and programming against an interface that the non-static class implements instead of calling methods of a static class directly. This will for example make unit testing of the controller and the class itself a lot easier.

    Please refer to the following thread for a list of more benefits of using a non-static class: http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp

    Saturday, August 16, 2014 11:56 AM