Best way to persist an ID String? - vba

I am curious as what the best way to persist a user ID is in VBA/Access. The user ID would be used in various forms to track changes a user has made. In .net what I would do is to create a class that holds the user ID variable, or use my.system.settings. Seeing as this is not an option, what are some ways or "the way" I can keep and hold a variable in memory or as a reference. The thought that comes to mind is a "Settings" table, but im not sure if thats overkill, or if there is a known and better method.

Persisting a value means, in Access, storing it in a database table. From your description I assume you mean persisting the value during the current session. In which case, once the value is obtained from a table, you could store it in a global variable, or as a property of an instance of a class that you have instantiated.
Another, Access specific, option is to set the value as the Caption for a hidden label on a form. This can either be on a main switchboard-form, that remains open throughout the session, or on a hidden form that is opened on start-up and, again, remains open throughout the session.
It is possible to create a registry-entry, but I would probably prefer to use a hidden-form. Whichever approach you take, it is important to ensure that the value remains intact throughout the session.

If you want the ID to persist only across that specific session, then a Global variable would probably suit you best.
If you want it to persist on a machine until changed, then using the registry would be better.
'SaveSetting appname, section, key, setting
SaveSetting "MyApp", "ID", "Value", "12345678"
'GetSetting(appname, section, key[, default])
CurrentID=GetSetting "MyApp","ID","Value" ' Add ,"DefaultValue" if you want a marker for if there is no value set
'DeleteSetting appname, section[, key]
DeleteSetting "MyApp", "ID" ' add ,"Value" if you only want this specific key deleted

Related

Does NSIncrementalStoreNode updateWithValues's "values" argument require only the changed values or a complete new copy of the data?

I'm calling:
- (void)updateWithValues:(NSDictionary *)values
version:(uint64_t)version
in an NSIncrementalStore subclass in order to update the cache with update NSManagedObject values. My question concerns the values argument. Do I only need to put in the updated attributes or a complete new copy of the data?
The description in the documentation says: "Update the values and version to reflect new data being saved to or loaded from the external store. // The values dictionary is in the same format as the initializer."
It isn't clear to me whether or not the "values" that "reflect the new data" refers to only the updated attributes or all the attributes in the object.
It requires the complete data. I agree it wasn't very clear but I suppose the reason is so you can do the conflict handling first. The annoying thing is there is no way to get the values back from the node to merge in the new ones and set them again. Annoyingly this means you can't use the node as your cache object, I'm still learning the NSIncrementalStore so likely there reason for this design will come clear at some point.

RavenDb GenerateDocumentKey

I need to generate Id for child object of my document. What is the current syntax for generating document key?
session.Advanced.Conventions.GenerateDocumentKey(document) is not there anymore. I've found _documentSession.Advanced.DocumentStore.Conventions.GenerateDocumentKey method but its' signature is weird: I am okay with default key generation algorithm I just want to pass an object and receive an Id.
The default implementation of GenerateDocumentKey is to get the "dynamic tag name" for the class, and append a slash. For example, class Foo would turn into Foos/ which then goes through the HiLoKeyGenerator so that ids can be assigned on the client-side without having to consult the server each time.
If you really want this behavior, you could try to use the HiLoKeyGenerator on your own, but have you considered something simpler? I don't know what your model is, but if the child thing is fully owned by the containing document (which it should be, to be in the same document) have you have several much easier options:
Just use the index within the collection
Keep a int NextChildThingId property on the document and increment that every time you add a ChildThing
Just use a Guid, although those are no fun to read, type, look at, compare, or speak to someone over the phone.

When is it a good idea to give an ADT a name data member?

If I have a Store class, I can write
Store starbucks;
Here, the variable name is the name of the object itself.
Alternatively, I can write
Store shop( "starbucks" );
where a name variable inside Store is initialized with "starbucks". Here, the variable name is generic but the object contains the specific store name.
Which is preferable, and when?
I can't think of a real world use for Store starbucks;. Any time you wanted to add a new store you would have to write all new code and recompile. Outside of test data, for unit tests and whatnot, this should never be used.
For similar reasons, hard coding Store shop( "starbucks" ); is also a bad idea. Again, changing instance data should not cause you to recompile your code.
Most code that I've written uses a combination of user input and a data store to create instances. This is done something like Store shop; shop.load(); or more likely Store shop = storeFactory.getStore();
In addition, I prefer to use the type of the object as the name of the object. This would make the example Store store = storeFactory.getStore();. It lowers the cognitive load of the reader, because they don't have to remember that shop is of type Store.

Initialize a Struts 1 ActionForm with Data from HttpSession Object

I've done this a half dozen times so I know it's possible. I just can't remember how.
I would like to initialize a property of a Struts 1 ActionForm with data from the user's HttpSession object, but only when the form is first created. Actually don't worry too much about the fact that it comes from HttpSession, important is just the fact that the data is dynamic, per-user, and should only be initialized once.
Additionally, if the user changes the data in this field, the user's entry should persist. In other words, when the user first sees the form they will see the initialized data. If they then change the field and submit the form (by calling the associated action) and subsequently come back to this form later, they should see THEIR entry in that field.
Obviously initializing the field in struts-config.xml won't work because the data is dynamic and per-user. Same can be said for the form's constructor. I see the reset() method of ActionForm will be called to reset properties to a default state, but I don't remember if it is called before the first time the form is loaded and displayed in the page. I suppose if it is that's an option, but I would only want it to do the initialization on the first call. That sounds just mildly complicated (I would need a boFirstTime member variable flag or something?).
Can anyone help?
What I ended up doing was overriding reset() of ActionForm, and setting the desired property only if it is null or blank. The property I needed to initialize is represented in the class member variable _strMailTo (yeah I know nobody but me uses the underscores for member variables anymore).
It turns out that reset() is also called before the ActionForm properties are used for the first time to populate the fields of the form for the associated Action. In this way the first time the user sees the form the my pre-populated data is there. But if they change it and later land on the form again they see the text they put in the field the last time they submitted the form.
I guess maybe I'm also the only one still using Struts 1 anymore...
public void reset(ActionMapping mapping, HttpServletRequest request) {
if (_strMailTo == null || _strMailTo.equals("")) {
String strRemoteUser = request.getRemoteUser();
_strMailTo = chOps.UtilityUsers.getEmail(strRemoteUser);
}
}

Overextending object design by adding many trivial fields?

I have to add a bunch of trivial or seldom used attributes to an object in my business model.
So, imagine class Foo which has a bunch of standard information such as Price, Color, Weight, Length. Now, I need to add a bunch of attributes to Foo that are rarely deviating from the norm and rarely used (in the scope of the entire domain). So, Foo.DisplayWhenConditionIsX is true for 95% of instances; likewise, Foo.ShowPriceWhenConditionIsY is almost always true, and Foo.PriceWhenViewedByZ has the same value as Foo.Price most of the time.
It just smells wrong to me to add a dozen fields like this to both my class and database table. However, I don't know that wrapping these new fields into their own FooDisplayAttributes class makes sense. That feels like adding complexity to my DAL and BLL for little gain other than a smaller object. Any recommendations?
Try setting up a separate storage class/struct for the rarely used fields and hold it as a single field, say "rarelyUsedFields" (for example, it will be a pointer in C++ and a reference in Java - you don't mention your language.)
Have setters/getters for these fields on your class. Setters will check if the value is not the same as default and lazily initialize rarelyUsedFields, then set the respective field value (say, rarelyUsedFields.DisplayWhenConditionIsX = false). Getters they will read the rarelyUsedFields value and return default values (true for DisplayWhenConditionIsX and so on) if it is NULL, otherwise return rarelyUsedFields.DisplayWhenConditionIsX.
This approach is used quite often, see WebKit's Node.h as an example (and its focused() method.)
Abstraction makes your question a bit hard to understand, but I would suggest using custom getters such as Foo.getPrice() and Foo.getSpecialPrice().
The first one would simply return the attribute, while the second would perform operations on it first.
This is only possible if there is a way to calculate the "seldom used version" from the original attribute value, but in most common cases this would be possible, providing you can access data from another object storing parameters, such as FooShop.getCurrentDiscount().
The problem I see is more about the Foo object having side effects.
In your example, I see two features : display and price.
I would build one or many Displayer (who knows how to display) and make the price a component object, with a list of internal price modificators.
Note all this is relevant only if your Foo objects are called by numerous clients.