I'm looking for a nice way to set my configuration constants. My idea was to create a singleton with my config properties. Say mySingletonConf with: URL, USERID, PASSWORD. During the initialization of the sharedInstance, a configuration file should be read to set the properties in mySingletonConf.
Should I use properties for this type of constants? I take they should be class-level properties?
Is it possible to set the configuration dynamically? I. e.
by reading all Setter-Methods of mySingletonConf, then searching the loaded configuration (.plist-Dictionary) for the key == property name and then invoke the settter with the value?
It would be nice to have the things set dynamically, in case new constants are needed. Then I would just have to create new properties and adjust the configuration files.
Am I on the right track?
Thanks for any help!
NSUserDefaults can take care of a lot of stuff and it ensures that the configuration is user-specific, so if multiple users on the same Mac use your program they can configure your program independently. There is also an object in Interface Builder for binding your user interface elements to, making things even easier. If you do want to make your configuration system-wide, you should use the Core Foundation Preference Utilities.
For storing passwords, you can use Apple's Keychain Services. A user is able to specify which programs are allowed to use the stored password (which would ideally be just yours). Storing passwords in NSUserDefaults is also an option if the password is not of any particular importance.
Don't re-invent the wheel; application-wide user-or-host-specific configuration services are provided for you.
Well, it is possible :)
I ended up writing the mentioned Singleton, which has readonly public properties and readwrite access from within the class (used Categories for that, see private setter example).
The vars of the class are filled with values from the .plist file during the initialization. I used the Runtime API to get the list of variables (just search for "objective-c list of variables" on Stackoverflow) and get the value from the loaded.plist dictionary using the var name as key.
The the values can be use almost as constants:
MyConstants* testConstants = [MyConstants sharedInstance];
NSLog(testConstants.PARAM1);
Related
In OOP, should a Facade be an object or just a class? Which is better?
Most of the examples in Wikipedia creates Facade as an object which should be instantiated before use.
CarFacade cf = new CarFacade();
cf.start();
Can it be designed to be like this instead?
CarFacade.start();
UPDATE
Can a Facade facilitate a singleton?
A facade
represents a high level API for a complex subsystem (module).
reduces client code dependencies.
This means that your client code only uses the facade and does
not have a lot of dependencies to classes behind that facade.
It is better to use an instance of an interface, because
you can replace it for tests. E.g. mock the subsystem the facade represents.
you can replace it at runtime.
When you use a static methods, your client code is bound to that method implementations at compile-time. This is usually the opposite of the open/close principle.
I said "usually the opposite", because there are examples when static methods are used, but the system is still open for extension. E.g.
ServiceLoader
The static load methods only scan the classpath and lookup service implementations. Thus adding classes and META-INF/services descriptions to the classpath will add other available services without changing the ServiceLoader's code.
Spring's AuthenticationFacade for example uses a ThreadLocal internally. This makes it possible to replace the behavior of the AuthenticationFacade. Thus it is open for extension too.
Finally I think it is better to use an instance and interface like I would use for most of the other classes.
It's two fold. You can use it as a static method. Say for instance in spring security I use AuthenticationFacade to access currently logged in user Principal details like so. AuthenticationFacade.getName()
There are other instances, in which mostly people create an instance of Facade and use it. In my opinion neither approach is superior over the other. Rather it depends on your context.
Finally Facade can use Singleton pattern to make sure that it creates only one instance and provides a global point of access to it.
This question is highly subjective. The only reason I am responding is because I reviewed some of my own code and found where I had written a Façade in one application as a singleton and written almost the same Façade in a different application requiring an instance. I'm going to discuss why I chose each of those routes in their respective applications so that I can evaluate if I made the correct choice.
A façade vs the open/close principle is already explained by #Rene Link. In my personal experience, you have to think of it this way: Does the object hold the state of itself?
Let's say I have a façade that wraps the Azure Storage API for .NET (https://learn.microsoft.com/en-us/azure/storage/common/storage-samples-dotnet)
This facade holds information about how to authenticate against the storage API so that it the client can do something like this:
Azure.Authenticate(username, password);
Azure.CreateFile("My New Text File", "\\FILELOCATION");
As you can see in this example, I have not created an instance and i'm using static methods, therefore following the singleton pattern. While this makes for code that is more concise, I now have an issue if I need to authenticate to a given path with a different credential than the one already provided, I would have to do something like this:
Azure.Authenticate(username, password)
Azure.CreateFile("My New Text File", "\\FILELOCATION");
Azure.Authenticate(username2, password2);
Azure.CreateFile("My Restrictied Text File", "\\RESTRTICTEDFILELOCATION");
While this would work, it can be hard to determine why authentication failed when I call Azure.ReadFile, as I have no idea what username and password may have been passed into the singleton from thread4 on form2 (which is no where to found) This is a prime example of where you should be using an instance. It would make much more since to do something like this:
Using (AzureFacade myAzure = Azure.Authenticate(username, password))
{
Azure.CreateFile("My New Text File", "\\FILELOCATION"); // I will always know the username and password.
}
With that said, what happens if the developer needs to create a file in Azure in a method that has no idea what the username and password to Azure may be. A good example of this would be an application that periodically connects to Azure and performs some multi-threaded tasks. In said application, the user setups a connection string to azure and all mulit-threaded tasks are performed using that connection string. Therefore, there is no need to create an instance for each thread (as the state of the object will always be the same) However, in order to maintain thread safety, you don't want to share the same instance across all the threads. This is where a singleton, thread-safe pattern may come into play. (Spring's AuthenticationFacade according to #Rene Link) So that I could do something like this (psudocode)
Thread[] allTask = // Create 5 threads
Azure.Authenticate(username, password) // Authenticate for all 5 threads.
allTask.start(myfunction)
void myFunction()
{
Azure.CreateFile("x");
}
Therefore, the choice between an instance of a façade v. a singleton façade is completely dependent on the intended application of the facade, however both can definitely exist.
I want to add a property of type boolean in a derived RLMObject which is only needed during runtime. So it's not part of the database table. Is there a way to mark the property as not part of the table in Realm?
The reason I need this, is because I want to save the selected state of a uitablecell during runtime. This means, I don't need an extra field in the database table.
I hope my question is clear, thank you.
I think you will want to use ignored properties of Realm:
edit: included the Swift docs link, but the question is about ObjC
https://realm.io/docs/objc/latest/#ignored-properties
Override Object.ignoredProperties() to prevent Realm from persisting model properties. Realm won’t interfere with the regular operation of these properties: they’ll be backed by ivars and you can freely override their setters and getters.
Realm is noSql database so it has no tables - it stores graph of dependencies. That is why you can just create separate 'settings' class, include it in your realm module (#RealmModule) and store single instance of it in realm file for that module. This will be the single instance of that object in database.
Found DeltaSpike that sort of does what I want....
Working with EJB 3.1 and really want to be able to annotate class properties so that "environment" properties are injected either at class load time or lazy on access plus the ability to allow for updates. For example:
#Environment(name="greeting.for.aliens", fetch="lazy", updates="true")
private String welcomeSign;
The name attribute is a match on a property name pulled from different sources like Java System, environment variables (from the shell), JNDI, property files and so forth. The fetch either says load on initiation or lazy (underpinned by Instance wrapper). Then if the "environment" properties are updated the interested "class" properties listen for that event.
More amazed that nobody has put together something (beyond DeltaSpikes) like this for EJB/CDI? Or I haven't found a good combination of Google keys words to get me to the right implementation for me.
What's the best practice for putting additional configuration items, specific to my application, in a twistd ".tac" file? How do I access these items from inside my class?
Is there some property in the "application" object that's intended to store these?
Create your own twisted.application.service.IService implementation (by subclassing twisted.application.service.Service or just by implementing the correct methods and attributes on a class all of your own). Give this class an __init__ that accepts the application-specific parameters. Launch the rest of your application logic in the startService method that is automatically called when twistd starts the reactor (for all IService objects attached to application). Use the objects you passed to __init__ in startService to get your application going in the right direction.
For example, see the FingerService defined in one of the Twisted tutorials (but unlike that tutorial, don't define all your classes in the .tac file! define them in modules and import them into the .tac file).
We have a dynamically composed application, in which user can add services and operations. This application is installed on a server cluster.
Since adding services to application involves so much writing to web.config, i was wondering if its possible to read system.servicemodel section from a database instead of web.config.
Seems like microsoft's implementation of configuration is very tightly coupled with where its stored.
There is no "out-of-the-box" way to do that. However, it is possible.
Few feet below, Configuration class uses FileStream instance where it actually can use any Stream. That particular step can be replaced with custom implementation of IInternalConfigHost interface (a lot of properties and methods to implement there).
Particularly interesting are OpenStreamForRead and OpenStreamForWrite, both are returning Stream instances. There you can put logic to pull XML of configuration sections from database into ConfigurationSection instances and to put ConfigurationSection instances as XML into database.
The next step is to create an instance of Configuration class. However, here we must get dirty because its constructor never leaves the System.Configuration kingdom. The need to use reflection to reach and use it. I suggest implementation of IInternalConfigConfigurationFactory to wrap the reflection magic.
Configuration Create( Type typeConfigHost,
params object[] hostInitConfigurationParams );
As first parameter pass the type of implemented configuration host.
After we have Configuration instance, we can use it a custom ServiceHost, ChannelFactory<T> and DuplexChannelFactory<T>.