Enums as value objects and modularisation - grails

I would like to split a Grails app in smaller plugins. Some domain classes have attributes which are of type Enum. Some values from these Enums would logically be part of the plugins, so I'm looking for a way for each plugin to register its own values into the 'core' Enums.
Obviously this cannot be done with Enums as they cannot be modified at runtime.
One option I see is replacing every Enum with a class with static attributes and add a static method to register new values from plugins. This has the following consequences:
GORM will not know what to store for the fake Enum values so it will throw an Exception
Therefore, I would have to change the type of these attributes to String, this way it will still work with the present values in storage, but I lose the advantage of seeing where that attribute can take its values.
Is there a better way to do this? Am I taking an approach which is not the "Grails way"?
In the end I am looking for a way to do an extension point in the core app for the plugins, like I would do with a ServiceProvider in Java.

I have a feeling that you are over complicating this problem, I would define the enums in different files depending on their type as probably they are now, then use them across your applications. I personally prefer to group the enums by their type not by their application, but it depends on your design.
Regarding the extension point, if you define them in core plugin, by the nature of Grails plugin you will have extendability. Meaning your applications can override or extend those values.
Hope this helps

I've faced the same problem in the development process. There is one solution I've found.
Transfer your enums to domains and init values with your plugins. Make static methods to get values, so domains will look like enums. For example:
class Person {
String name
static mapping = {
cache usage: 'read-only'
table 'Person '
id column: 'Person ID', generator: 'identity'
name column: 'Name'
}
static Person getJOHN() {
getByName('John')
}
static Person getByName(String name) {
Person.findByName(name)
}
}

Related

DI and runtime parameters - structuremap

I'm increasingly finding myself mixing runtime parameters and implicit construction injection and it smells bad to me.
Example - I have a base class describing a Filter, and various inherited types for specific filters (tag, category, date, author, etc etc)
var filter = StructureMap.ObjectFactory
.With("caption").EqualTo("Posts filtered by tag:")
.With("parameters").EqualTo(parameters)
.With("displayInSummary").EqualTo(true)
.GetInstance<TagListFilter>();
The reason I do this is because in the constructor I have an interface using which I wish StructureMap to inject a concrete class (IArticleConfigurator):
public TagListFilter(string caption, IDictionary<string,string> parameters, bool displayInSummary, IArticleConfigurator configurator)
:base(caption, parameters,displayInSummary, configurator)
But it just occured to me that I replaced a simple constructor, albeit with a concrete class instead of interface, with, essentially, the same thing but using DI to inject 1 concrete type. I'm doing this because currently our configs are in a xml file but will be moved to a CMS, so seemed like a good idea to use an interface.
It seems wrong and not in the spirit of DI.
Should I use a factory to generate my various filters? If so, can I still leverage DI to get a concrete instance of my IArticleConfigurator?
You shouldn't pass parameters explicitly from one dependency to another or, at least, you should minimize their number. One big disadvantage of resolving instances with parameters is that you specify parameter names as string literals - which make you code very fragile in changes of constructor signature.
One example I might think of (note that I have no clue regarding your domain and responsibilities of entities) is to inject provider or, as you already said, factory. For example, create something like ITagListFilterConfigurationProvider (you should change the name as you want to, I just trying to give motivation). You might create very abstract provider like IFilterConfigurationProvider with three methods as below, if you have same parameters for filters:
interface ITagListFilterConfigurationProvider
{
string Caption { get; }
IDictionary<string,string> GetParameters();
bool IsDisplayInSummary { get; }
}
Now you constructor will look like:
public TagListFilter(ITagListFilterConfigurationProvider configurationProvider, IArticleConfigurator configurator)
All you need is to implement it as you already did (because you are passing concrete parameters to constructor) and extract this behaviour to provider. What is left - is to register concrete provider with StructureMap and resolve filter without passing any concrete parameters
var filter = StructureMap.ObjectFactory.GetInstance<TagListFilter>();

How do you inject your dependencies when they need differents parameters?

For instance I have this bit of code
public class ProductService{
private IProductDataSource _dataSource = DependencyManager.Get<IProductDataSource>();
public Product Get(int id){
return _dataSource.Select(id);
}
}
I have 2 different data source:
XML file which contains the informations only in 1 language,
a SQL data base which contains the informations in many languages.
So I created 2 implementation for IProductDataSource, for for each kind of datasource.
But how do I send the required language to the SQL data source ?
I add the parameter "language" to the method "IProductDataSource.Select" even if I won't use it in the case of the XML implementation.
Inside the SQL implementation I get the language from a global state ?
I add the language to the constructor of my SQL implementation, but then I won't use my DependencyManager and handle my self the dependency injection.
Maybe my first solution is not good.
The third option is the way to go. Inject the language configuration to your SQL implementation. Also get rid of your DependencyManager ServiceLocator and use constructor injection instead.
If your application needs to work with multiple languages in a single instance I think point one is a sensible approach. If the underlying data does not provide translations for a request language then return null. There is another solution in this scenario. I'm assuming that what you have is a list of products and language translations for each product. Can you refactor your model so that you do not need to specify or asertain the langauge until you reference language specific text? The point being a product is a product regardless of the language you choose to describe it. i.e. one product instance per product, only the product id on the Datasource.Select(..) method and some other abstraction mechanism to deal with accessing the correct text translation.
If however each instance of your application is only concerned with one language set I second Mr Gloor.
First of all I need to point out that you are NOT injecting any dependencies with your example - you are depending on a service locator (DependencyManager) to get them for you. Dependency injection, simply put, is when your classes are unaware of who provides the dependencies, e.g. using a constructor, a setter, a method. As it was already mentioned in the other answers, Service locator is an anti-pattern and should be avoided. The reasons are described in this great article.
Another thing is that the settings you are mentioning, such as language or currency, seem to be localization related and would probably be better dealt with using the built-in mechanisms of your language of choice (e.g. resource files, etc).
Now, having said that, depending on how the rest of your code is structured you have several options to solve this while still using Service locator:
You could have SqlDataSource depend on some ILanguageProvider which pulls the current language from somewhere. However, with more settings like these (or if it is difficult to get current language in an isolated way) this can get messy very fast.
You could depend on IProductDataSourceFactory instead (or, if you are using C#, Func<IProductDataSource>) which would return the concrete implementation with the correct settings. Again, you need to be able to get the current language in an isolated way in order to use this.
You could go with option 1 in your question. This would be a leaky abstraction but would be the simplest to implement.
However, if you decide to get rid of service locator and start using some DI container, the best solution would be using option 3 (as it was already stated) and configuring container accordingly to provide the correct value. Some good ideas of how to do this in an elegant way can be found in the answer to this question

Is there a list of forbidden method names for Grails domain objects?

Often as I add a helper method to a domain object I get an error when compiling which resolves to "x property is not found". This seems to happen for methods name getX, setX, and also recently isX. Is there a list of name forms that I should avoid? Is there a way to annotate or otherwise label these methods so Grails doesn't confuse them with auto properties?
Grails autodetects properties and assumes that they're persistent. Public fields in Groovy create a getter and setter under the hood so getters are assumed to be associated with persistent fields.
But if you want a helper method that starts with 'get' or 'is' but isn't a getter for a persistent field, you have two options. One is to use the transients list - see http://grails.org/doc/latest/ref/Domain%20Classes/transients.html
The other option is to declare the return value as def. Since it's not typed (def is an alias for Object) Hibernate can't persist it since it doesn't know what data type to use, so it's ignored.
My preference is the transients list because I would rather have self-documenting methods where it's obvious what they do, what parameter types they accept, and what they return.
I have no idea of common list - it's too diverse. Convention methods are added by different parts of Groovy and Grails:
Groovy convention about property getters/setters is very basic thing. It's impossible have a getX() method and not have read access to x property.
Grails domain class dynamic methods and Domain class convention properties are specific to Grails domain classes;
Same for controllers, and so on;
Groovy convention about property getters/setters, including methodMissing, propertyMissing, $staticMethodMissing, getProperty, properties and so on;
Groovy adds a number of as<Type>() methods, like asInteger();
different plugins could inject more convention methods.
To access declared field, not getter/setter, use java field access operator.
As far as I understand your problem, you can use transient !
static transients = ['feildName']

Distinguishing between Grails domain-class fields and getBlah() methods via GrailsDomainClassProperty

I'm writing a Groovy script (as part of a Grails plugin) and I want to get a list of properties for a GrailsDomainClass that a user of my plugin might define. I can do this using domainClass.properties (where domainClass is a GrailsDomainClass).
However, suppose a user has the grails domain class:
class Example {
String name
static constraints = {
}
def getSomeNonExistingProperty(){
return "Not-a-real-property"
}
}
In this case, domainClass.properties returns a list with both name and someNoneExistingProperty
I understand that this is because of Grails is generating a read-only property on-the-fly for use where someone has a getBlah() method. That's great, but in my script I want to perform some actions with the "real" properties only (or at least non read-only properties).
That is, I would like some way of distinguishing or identifying someNonExistingProperty as a read-only property, or, alternatively, as a property generated by Grails and not entered explicitly as a field in the domainClass by the user of my plugin.
I've looked at the GrailsDomainClassProperty Class and it has a range of methods providing information about the property. However, none of them appear to tell me whether a property is read-only or not, or to allow me to distinguish between a field defined in the domainClass and a field created on-the-fly by Grails as a result of a "getSomeNonExistingProperty()" method.
Am I missing something obvious here? Is there a way of getting a list of just the explicitly user-defined fields (eg name, in the above example)?
I believe transient properties are what you are trying to exclude
I've run into this problem a few times, and instead of trying to work around it I typically just end up renaming my getX() method. It's probably the easiest option.
Edit:
Alternatively, I wonder if you could use reflection to see which methods are defined on the class, and while iterating over your properties see if the property has an explicit getter defined, and omit it. I'm not very familiar with reflection when it comes to Groovy and Grails, especially with the dynamic methods, but it's a possible route of investigation.

Guice: Varying the type injected according to how the owner has been injected

I have a guice based app that now needs multiple instances of a given type so I plan on using a named annotation to disambiguate the dependencies. However a dependency of this type also needs to vary based on which one I get.
To illustrate lets say I have
#Singleton
public class FooCache {
private final FooCacheListener listener;
#Inject
public FooCache(FooCacheListener listener) {
this.listener = listener;
}
// do stuff
}
and then lets say I have a need for 2 separate instances so I might have
#ThatOne FooCache
in one class and
#ThisOne FooCache
in another.
Now lets say I want a different listener in each case (maybe one writes something to a database and the other sends a notification over JMS or to some distributed cache). How would I do that? I can't see that I can stick a name on the FooCacheListener as I'd need a different name in one situation vs the other whereas I have just one place here. The only way I can think of doing this is by subclassing FooCache but that seems a really clumsy approach to me.
Cheers
Matt
You might be able to use PrivateModules. Go here and scroll down to How do I build two similar but slightly different trees of objects? It is a way to have two different instances of the same class,which sounds almost exactly what you are trying to do. You could pass in your cachelisteners instead of the "lefty" and "righty" passed in in the example.
There are more links with details from there if it looks like what you want.
Another option might be to inject a factory, which is also discussed in the link above, in the question How do I pass a parameter when creating an object via Guice?

Resources