From here
Although WCF provides some support for writing REST-style services,
the support for REST in ASP.NET Web API is more complete and all
future REST feature improvements will be made in ASP.NET Web API
So for REST services it's preferable to use Web API, not WCF
That link also provides some useful info (like whether they are interchangeable, features of technologies)
Given a Python class which will be instantiated only once, i.e. there
will be only one object of the class. I was wondering in which cases
it makes sense to create a single class instance instead of working
directly with the class instead.
So it's this:
class Singleton:
'''don't bother instantiating me'''
clsvar1 = 'foo'
@classmethod
def foobar(cls, *args, **kwargs):
if condition():
cls.clsvar1 = 'bar'
versus this?
class Singleton:
'''instantiate and use me'''
def __init__(self):
self.var1 = 'foo'
def foobar(self, *args, **kwargs):
if condition():
self.var1 = 'bar'
Recommendation
I definitely would prefer the one that's intended to be instantiated. When you create a "type of thing" that implies that you create things of that type.
The motivation is to group related code and data and having an interface to it.
That said, why not just use a module since all you want is a namespace? Modules are singletons, group related code and data, and this is a bit simpler and probably would be considered more Pythonic:
var1 ='foo'
def foobar(*args, **kwargs):
global var1
if condition():
var1 = 'bar'
So the usage would be instead of:
from modules.singleton import Singleton
Singleton.foobar()
or
from modules.singleton import Singleton
the_singleton = Singleton()
the_singleton.foobar()
do this:
from modules import singleton
singleton.foobar()
Best Answer
Mock frameworks line NMock (for generating mock classes in unit test scenarios) do this.
Another application I can think of may be a generic database tool generating classes at run time for each table in the DB (of course, there may be better solutions for this purpose).
In C#, every time when you have the requirement to construct a function at runtime (because, for example, the user is able to enter a function defined by himself in your application), you will need a dynamic class for that purpose since in C# functions cannot live "on their own".
And another application I know of are the dynamically created COM wrappers in C#4.0 for every COM component. Up to C# 3.5, you needed first to generate some type library wrapper which had to be compiled first before you could use it. With C#4.0 and the
dynamic
keyword, this is not necessary any more, the wrapper can be constructed at run time by the framework and you can write code which calls methods of those classes.