5

I have a typical web application with the controllers calling the business methods. Should the methods in the business classes be implemented using static methods or instance methods. The business layer classes doe not maintain any state information.

Some additional information + The business classes do not maintain state specific information. + Would instantiating these objects on a per request basis consume a lot of memory as opposed to using static methods

user3547774
  • 1,621
  • 3
  • 20
  • 46
  • I would recommend staying away from static variables. If methods are all on their own(i.e. Not using any static variable), yes then you can go for it. – Ashish Charan Jul 22 '14 at 09:23

1 Answers1

2

Does "yes" count as an answer? I hate to say it, but both answers are valid; choosing between them requires context. If you have no use for per-instance state, then: why create instances? However, it should be noted that per-instance state is useful for IoC/DI scenarios, which in turn are handy for testing.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Always use instance methods created through some IOC. The reason for this recommendation is that is allows proper testing of your application by allowing the tester to provide Stubs for your business logic tier and concentrate a test only on the tier they are interested in. Although its possible to use Fake Shims to divert a call to a static method its only available in higher versions of VS.net (Ultimate) and is only really provided to test legacy code. If you are starting from scratch, present all your logic through Interfaces, which in turn mandates that they are NOT statics. – PhillipH Jul 22 '14 at 10:04
  • 1
    @PhillipH always be cautious of any statement that starts with "always" ;p What constitutes an appropriate approach depends an awful lot on the problem at hand, and what is being "solved" by different approaches. It is, IMO, almost meaningless to talk in absolutes. It is far more useful to give an indication of the strengths and weaknesses of each option, and let the developer choose the *most appropriate* tool for the job in front of them. – Marc Gravell Jul 22 '14 at 10:18
  • Marc, agreed "be cautious", but instance methods are better for testability than statics, and I haven't found an instance where this is not the case. Probably should have said "strong recommendation" instead. :-) – PhillipH Jul 22 '14 at 10:22
  • Thank you everyone for your response, this provides a lot of information. One more question, will I have a gain in memory allocation if I use static methods instead of instance? since for each request I will be creating a number of instances as opposed to just using static methods? – user3547774 Jul 22 '14 at 10:55
  • @user3547774 generally, a few business-layer instances are going to be negligible compared to the other allocations happening per-request - it shouldn't make a huge difference, and they are typically cheap to collect (gen-0 or gen-1 at worst) – Marc Gravell Jul 22 '14 at 11:05