Make class names independent via db4o renaming api - db4o

I'm new in Db4o
As far as I checked, when I do a refactoring of a class name or path (packages),
db4o doesnt recognizes the class anymore and creates a new 'category'.
I want to make an API, so that any class refactoring in my app won't affect the database.
I've created the following function ...
public void saveClassAs(Class objClass, String nameInDB){
configuration.common().objectClass(objClass).rename(nameInDB);
}
and have it called for each of my classes. Since the nameInDB will be constant for each class, no matter where the class is located or its name.
The problem is that Im not sure if will this work and I don't know if this is a good idea in matters of speed ... I didn't find any details on db4o documentation about how the renaming API works. Any help?

If you want to use constant class names (irrespective to the real classes names) my best bet is to use aliases.
Regarding renaming configuration you can find details here.

Related

Access private properties and methods between class extension categories in Objective-C

I have a class that I want to split across multiple files. I tried using categories, but can't figure out how to make it work.
My class is named UserManager and I want to create UserManager+Amazon and UserManager+Facebook.
The problem is that I do need to access private properties and/or methods in UserManager+Amazon that are implemented in UserManager+Facebook, and vice-versa.
How can I extract methods outside the main UserManager.m file, while maintaining the access to private stuff?
EDIT: #Avi has an excellent idea below, although I have not tested it.
I have also just discovered a solution: Properties for Class and Its Subclasses Only
It works with categories and subclasses. I have tested it with my code and it works. It uses a class extension on the BaseClass.h, complete with an implemented example immediately below the accepted answer.
OLD:
I've been struggling with this recently as well.
My current idea is to create a third category UserManager+Private that implements all of the private methods and handles the properties via associated objects (http://nshipster.com/associated-objects/).
It feels very unwieldy, but it might work for you. I would still be interested in a better solution if one exists.

Using NSClassFromString() on a Swift declared class requires a fully qualified name

I have an issue where in Swift I have a class defined named Person, and when talking to some objective-c code, it will try to create new instances of that class.
Obviously NSClassFromString(#"Person") fails, because it needs to be a fully qualified name such as DBAccess_Test.Person.
My question is, am I able to programatically generate the DBAccess_Test. portion, by possibly searching through a registry of classes perhaps?
The reason being, that for an ORM we maintain, a Swift programmer may well decide to have all of his storage classes within a separate namespace to the application. But we wish to maintain standard table naming practices that match the names of the class as understood by the developer.
For instance, instead of a query being:
NSArray* r = [[[[Person query]
where:#"surname IN (SELECT surname FROM Employees)"]
orderBy:#"forename"]
fetch];
it would become:
NSArray* r = [[[[Person query]
where:#"surname IN (SELECT surname FROM DBAccess_Test_Employees)"]
orderBy:#"forename"]
fetch];
The ORM already has a bit of an identity crisis. We just wish to keep in check the table names, with the class names that the programmer will use.
Thanks in advance foe your help.
Annoyingly, I stumble across the answer as soon as I post this (Despite searching for hours).
You must put #objc(SwiftClassName) above your swift class.
Like:
#objc(SubClass)
class SubClass: SuperClass {...}
Then all of your original Objective-c code will see these classes and not try to do anything funky with them!
Thanks to #klaus
Original post can be found here:
https://stackoverflow.com/a/24448786/846035

ZF2 override framework classes via autoloader classmap

Is it possible to override the class file location of a framework class via classmap and autoloader? If yes, then how?
Example: I want to override Zend\Form\Fieldset, so that everywhere in the framework where Zend\Form\Fieldset is referenced, I want it to use my own class file instead of the original.
Motivation: When updating the framework, I want to keep my modifications safe from getting overwritten.
Known alternative: Modify the code in the framework.
Disadvantage: Modification gets lost when updating the framework.
writing the same class (FQCN) at another location is generally a bad idea. This causes two classes which are equally named to live in two separate locations. It's a much better idea to create your own Fielset in your own namespace. Say, Application\Form\Fieldset.
You can extend the ZF2 fieldset by your own. Then reference this new fieldset class and its all much more maintainable.
The downside of this method is you don't automatically use the new fieldset class. You have to reference the Application\Form namespace in every form you use. On the other hand, this makes it much more clear to other users of you code what exactly happens: there are no unexpected consequences using ZF2 code.
The only remark I have to make here is, for what do you need another fieldset? If you think you need that for view helpers, that's not true. You can modify the view helper to render fieldsets without modifying the Fieldset form class itself.

Access Properties from utility classes used by Action

I just want to know if there's a way to access the properties from a utility class used by an Action class. To access the properties from an Action class we extend the ActionSupport and use the getText("property.key.name") method.
So, my question is -should every other class extend the ActionSupport to access properties, even though its not an Action class? or is there any other way?
Thanks
I wouldn't extend ActionSupport unless you're actually defining an action.
The S2/XW2 ActionSupport class uses com.opensymphony.xwork2.DefaultTextProvider; you might be able to use it in your own classes. I'm a little wary of this since I'm not convinced non-action classes should be accessing the web-app's resources, but I haven't given it much thought, so it could be valid. I also haven't tried to do it.
ActionSuport is kind of helper class being developed by S2 developers to supplement the Development as it provides many features OOTB.
getText() is one of the use-case where S2 provides a way to read the property files.This method is specific to S2 as it know how to transverse the hierarchy to read the property files and in what order.
There are many ways to read the property files in a application and few of them are
ResourceBundle
if you are using Spring, it has a very handy mechanism to read property files
- how-to-read-properties-file-in-spring
Apache Common also provides a way to read the file
Apache-Common
In short to read properties file there are many ways, S2 getText() is a way developed by the S2 to read the property file with respect to your actions.
//I wanna make you understand how struts doing it.
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
//Action support implementation.
//Here TextProvider takes care about resource bundle thing.
}

Entity Framework context

I have an application using the Entity Framework code first. My setup is that I have a core service which all other services inherit from. The core service contains the following code:
public static DatabaseContext db = new DatabaseContext();
public CoreService()
{
db.Database.Initialize(force: false);
}
Then, another class will inherit from CoreService and when it needs to query the database will just run some code such as:
db.Products.Where(blah => blah.IsEnabled);
However, I seem to be getting conflicting stories as to which is best.
Some people advise NOT to do what I'm doing.
Other people say that you should define the context for each class (rather than use a base class to instantiate it)
Others say that for EVERY database call, I should wrap it in a using block. I've never seen this in any of the examples from Microsoft.
Can anyone clarify?
I'm currently at a point where refactoring is possible and quite quick, so I'd like some general advice if possible.
You should wrap one context per web request. Hold it open for as long as you need it, then get rid of it when you are finished. That's what the using is for.
Do NOT wrap up your context in a Singleton. That is not a good idea.
If you are working with clients like WinForms then I think you would wrap the context around each form but that's not my area.
Also, make sure you know when you are going to be actually executing against your datasource so you don't end up enumerating multiple times when you might only need to do so once to work with the results.
Lastly, you have seen this practice from MS as lots of the ADO stuff supports being wrapped in a using but hardly anyone realises this.
I suggest to use design principle "prefer composition over inheritance".
You can have the reference of the database context in your base class.
Implement a singleton for getting the DataContext and assign the datacontext to this reference.
The conflicts you get are not related to sharing the context between classes but are caused by the static declaration of your context. If you make the context an instance field of your service class, so that every service instance gets its own context, there should be no issues.
The using pattern you mention is not required but instead you should make sure that context.Dispose() is called at the service disposal.

Resources