I am absolutly new in REST and I have the following doubt about what are safe method in REST and what are idempotent method.
I know (but it could be wrong) that GET, HEAD, OPTIONS and TRACE methods are defined as safe because they are only intended for retrieving data.
But now I am reading this article: http://restcookbook.com/HTTP%20Methods/idempotency/ and it say that:
Safe methods are HTTP methods that do not modify resources. For
instance, using GET or HEAD on a resource URL, should NEVER change the
resource.
And untill here it is ok, it is nothing different from what I yek know, but after it assert that:
However, this is not completely true. It means: it won't change the
resource representation. It is still possible, that safe methods do
change things on a server or resource, but this should not reflect
in a different representation.
What exactly means this assertion? What exactly is a representation? and what means that a safe method so change on a resource but that this change is not refleted into a different representation?
Then it does also this example:
GET /blog/1234/delete HTTP/1.1
and say that it is incorrect if this would actually delete the blogpost and assert that:
Safe methods are methods that can be cached, prefetched without any
repercussions to the resource.
What exactly is a representation?
A "representation" is the data that is returned from the server that represents the state of the object. So if you GET at http://server/puppy/1 it should return a "representation" of the puppy (because, it can't return the actual puppy of course.)
However, this is not completely true. It means: it won't change the
resource representation. It is still possible, that safe methods do
change things on a server or resource, but this should not reflect in
a different representation.
What exactly means this assertion?
They mean that if you GET /server/puppy/1 two times in a row, it should give you the same response. However, imagine you have a field that contains the number of times each puppy was viewed. That field is used to provide a page listing the top 10 most viewed puppies. That information is provided via GET /server/puppystats. It is okay for GET /server/puppy/1 to update that information. But it should not update information about the puppy itself. Or, if it DOES update the information about the puppy itself, that information is not part of the representation of the puppy returned by GET /server/puppy/1. It is only part of some other representation that is available via another URL.
If it helps, this is a similar concept to the "mutable" keyword in C++ when applied to a const object. "mutable" allows you to modify the object, but it should not modify it in a way that is visible outside of the class.
Related
Looking at the Qt doc, the correct way to handle with objects shared between pages is to use QWizardPage::registerField and QWizardPage::field.
I personally think is more simple, since we are under C++, to pass to the QWizardPage(s), in their constructors, a pointer to my shared object, since there's no risk on cuncurrent access on the shared resource. Every QWizardPage change the value of that object safely and it's shared between pages because the pointer location is the same.
What am I missing? Why the need of such methods?
They are different approaches:
With a shared pointer you need a member for each object you want to share, which means you need to change the interface of your classes.
With the field-API you don't change the interface, but it is then not defined in the interface what fields exist. This means you should document them separately. This seems to me is the better way when having a multitude of fields.
Also note the automatic validation by the wizard:
If an asterisk (*) is appended to the name when the property is registered, the field is a mandatory field. When a page has mandatory fields, the Next and/or Finish buttons are enabled only when all mandatory fields are filled.
To consider a field "filled", QWizard simply checks that the field's current value doesn't equal the original value (the value it had when initializePage() was called). For QLineEdit and QAbstractSpinBox subclasses, QWizard also checks that hasAcceptableInput() returns true, to honor any validator or mask.
As you see: it's mainly a convenience feature. And it might save you from recompiling lots of stuff when working with bigger projects.
As you see: it's mainly a convenience feature. And it might save you from recompiling lots of stuff when working with bigger projects.
What is this "internal state" people talk about all the time precisely? The term really irritates me. The internet couldn't provide me with a definition yet.
From Object-Oriented Analysis and Design with Applications
The state of an object encompasses all of the (usually static)
properties of the object plus the current (usually dynamic) values of
each of these properties
In object oriented programming the objects can have state (data) and behavior (function).
The behavior specifies what the object can do, and it is usually conditioned by its state.
The state can be represented by any member or static variable, and it will depend of the definition of the class the object is instance of.
Update: The internal state refers to those private variables that affect the behavior of the object but are not visible from the outside world.
For example, let's say you have an HTTP client having the following interface:
class HttpClient {
public:
HttpClient(std::string host);
HttpResponse get(std::string path);
HttpResponse post(std::string path);
};
This object might have a getter for host but none for the current connection state.
A good optimization might be to keep the connection alive between requests (assuming the server allows it) so, in the first call to get or post the object will have to establish the connection and save the socket description in some internal variable that is not exposed to the user. The next time get or post is called the connection is already established (and the user has no idea).
In this case, the connection is part of the internal state of the object.
What is your internal state?
Hungry, Thirst,
Put some variables on that.
So in OO terms.
My state is
drinks-requirement: two glass of water,
food-requirement: sandwich
So the same concept applies in terms of an object. The sum total of the variables of anobject
Building off of what #AdamBurry said, think of an object as a black box that another piece of code can use. That code instantiates it:
Order o = new Order();
Then the code asks for the object to modify itself:
OrderItem oi = new OrderItem("Widget", 5.5);
o.AddItemToOrder(oi);
Then the code asks for the object to do something.
o.GetTotal();
How is the order computing the new total, given the item that just got added? Does it have a list of OrderItems, complete with prices? You bet. It has internal details that the code calling into may have no way of getting to. Those black-boxy details that the object needs to very carefully keep track of are the internal state of the object.
A much more practical example of something you may never want to expose to the "outside" world are variables which maintain the "dirty" state of an object. Has it been modified, but not committed to the database, yet? External code should never need to know this, but the object may need to.
What about an object that lets you step forward or backward through a list? Somewhere in that object, there's going to be an internal state variable that acts as a pointer to the current record. Again, the calling code would never need to see this, but when the code calls the .MoveNext() method, the object is going to have to increment that pointer by one to maintain the state of where it is in the list.
The internal state of an object is the set of all its attributes' values. One particular aspect of the internal state is that a method applied to the object being in a defined internal state (= a specific set of all its attributes' value) will result in another, also defined (and reproducible) internal state.
This aspect is important when you somehow record system states that you want to replay afterwards in a simulation of the recorded system. By recording the original internal state of an object you are able to reproduce all its subsequent internal states by simply calling its methods without having to store any additional data. However this is easier said than done in practice...
Applied to C++ the internal state will not be altered by a const method.
A mutable attribute (= attribute modifiable by a const method) can be altered without semantically affecting the internal state of an object. At least this is the contract the developer goes for when he uses this modifier...
Is it safe to store a CFC object in the REQUEST scope to be accessed later? Right now, our sites load up navigation data at least twice, possibly three times if they use our breadcrumbs feature. Some times, this data can vary, however, most of the time, three separate calls end up being made to grab the same exact navigation data...
So, I was thinking after the first load, save the navigation data in the REQUEST scope in some sort of struct, and in subsequent calls, just check to see if that data is already there, and if so, just use what is stored rather than re-creating it again. I know this would be accessing a shared scope outside of a contained object, which is probably not good practice, but in the end could shave off half of our page load times...
I know it can be done, however, we have had problems with the server recently, some of it possibly being memory leaks from how we use/store certain things, so was wondering if this was safe to do...
Either the variables or request scope would be suitable for your purpose, however more advisable would be to modify the functions that require access to this variable to accept your cached variable as an argument. With regard to CFCs it could be passed in the init() method and stored for use by the methods within that CFC (assuming you initialise it)
By relying on a global variable (even one restricted to current request) you are potentially just causing difficulties for yourself down the line, which would be solved by ensuring the methods are more encapsulated.
As mentioned in my comments earlier, ColdFusion - When to use the "request" scope? is worth a quick read as it has relevant information in the answers.
Yes. The only request that has access to the REQUEST scope is the current request.
I have to write a bunch of DTOs (Data Transfer Objects) - their sole purpose is to transfer data between client app(s) and the server app, so they have a bunch of properties, a serialize function and a deserialize function.
When I've seen DTOs they often have getters and setters, but is their any point for these types of class? I did wonder if I'd ever put validation or do calculations in the methods, but I'm thinking probably not as that seems to go beyond the scope of their purpose.
At the server end, the business layer deals with logic, and in the client the DTOs will just be used in view models (and to send data to the server).
Assuming I'm going about all of this correctly, what do people think?
Thanks!
EDIT: AND if so, would their be any issue with putting the get / set implementation in the class definition? Saves repeating everything in the cpp file...
If you have a class whose explicit purpose is just to store it's member variables in one place, you may as well just make them all public.
The object would likely not require destructor (you only need a destructor if you need to cleanup resources, e.g. pointers, but if you're serializing a pointer, you're just asking for trouble). It's probably nice to have some syntax sugars constructors, but nothing really necessary.
If the data is just a Plain Old Data (POD) object for carrying data, then it's a candidate for being a struct (fully public class).
However, depending on your design, you might want to consider adding some behavior, e.g. an .action() method, that knows how to integrate the data it is carrying to your actual Model object; as opposed to having the actual Model integrating those changes itself. In effect, the DTO can be considered part of the Controller (input) instead of part of Model (data).
In any case, in any language, a getter/setter is a sign of poor encapsulation. It is not OOP to have a getter/setter for each instance fields. Objects should be Rich, not Anemic. If you really want an Anemic Object, then skip the getter/setter and go directly to POD full-public struct; there is almost no benefit of using getter/setter over fully public struct, except that it complicates code so it might give you a higher rating if your workplace uses lines of code as a productivity metric.
I am writing a perl script to parse a mysql database schema and create C++ classes when necessary. My question is a pretty easy one, but us something I haven't really done before and don't know common practice. Any object of any of classes created will need to have "get" methods to populate this information. So my questions are twofold:
Does it make sense to call all of the get methods in the constructor so that the object has data right away? Some classes will have a lot of them, so as needed might make sense too. I have two constrcutors now. One that populates the data and one that does not.
Should I also have a another "get" method that retrieves the object's copy of the data rather that the db copy.
I could go both ways on #1 and am leaning towards yes on #2. Any advice, pointers would be much appreciated.
Ususally, the most costly part of an application is round trips to the database, so it would me much more efficient to populate all your data members from a single query than to do them one at a time, either on an as needed basis or from your constructor. Once you've paid for the round trip, you may as well get your money's worth.
Also, in general, your get* methods should be declared as const, meaning they don't change the underlying object, so having them go out to the database to populate the object would break that (which you could allow by making the member variables mutable, but that would basically defeat the purpose of const).
To break things down into concrete steps, I would recommend:
Have your constructor call a separate init() method that queries the database and populates your object's data members.
Declare your get* methods as const, and just have them return the data members.
First realize that you're re-inventing the wheel here. There are a number of decent object-relational mapping libraries for database access in just about every language. For C/C++ you might look at:
http://trac.butterfat.net/public/StactiveRecord
http://debea.net/trac
Ok, with that out of the way, you probably want to create a static method in your class called find or search which is a factory for constructing objects and selecting them from the database:
Artist MJ = Artist::Find("Michael Jackson");
MJ->set("relevant", "no");
MJ->save();
Note the save method which then takes the modified object and stores it back into the database. If you actually want to create a new record, then you'd use the new method which would instantiate an empty object:
Artist StackOverflow = Artist->new();
StackOverflow->set("relevant", "yes");
StackOverflow->save();
Note the set and get methods here just set and get the values from the object, not the database. To actually store elements in the database you'd need to use the static Find method or the object's save method.
there are existing tools that reverse db's into java (and probably other languages). consider using one of them and converting that to c++.
I would not recommend having your get methods go to the database at all, unless absolutely necessary for your particular problem. It makes for a lot more places something could go wrong, and probably a lot of unnecessary reads on your DB, and could inadvertently tie your objects to db-specific features, losing a lot of the benefits of a tiered architecture. As far as your domain model is concerned, the database does not exist.
edit - this is for #2 (obviously). For #1 I would say no, for many of the same reasons.
Another alternative would be to not automate creating the classes, and instead create separate classes that only contain the data members that individual executables are interested in, so that those classes only pull the necessary data.
Don't know how many tables we're talking about, though, so that may explode the scope of your project.