开发者

Java library for extended attributes, typing and validation

Background: I have an object model that needs to be extended by adding several attributes to a few types, which can vary across installations. For example, my 'user' objects need to have a few extended attributes. On some sites 'users' need several identifiers whereas in other sites 'users' need other attributes like extra phone numbers, addresses and dates. I prefer to avoid maintaining several开发者_JAVA技巧 object models and interfaces, and instead model this as "extensible object properties".

Question: Is there a Java library that can handle type definitions, providing typed property bags, methods to list properties, enforce typing, validate values, convert values to and from String, and maybe help with the related SQL queries and form processing...?

I need to be able to define types and attributes programatically. Ability to load type definitions from configuration files would be desirable.

Comments are very welcome. Thank you.


If I get you right it is barely possible in scala with trait mechanizm, which is like Interfaces you may mixin in some class, but may contain code, so you don't need to rewrite abstract methods all the times you use them.

trait Stackoverflow {
   val foo = "FOO"
}

trait MetaStackoverflow {
   val bar = "BAR"
}

class User(val name: String)

now in REPL:

scala> val u = new User("jjmontes") with Stackoverflow with MetaStackoverflow   
//u: User with Stackoverflow with MetaStackoverflow = $anon$1@8825a5

scala> u.bar  
//res0: java.lang.String = BAR

scala> u.foo  
//res1: java.lang.String = FOO

Or like this:

scala> class UserA(name: String) extends User(name) with Stackoverflow
//defined class UserA

scala> val u = new UserA("jjmontes")
//u: UserA = UserA@1aecf45

scala> u.foo
//res8: java.lang.String = FOO

scala> u.bar
<console>:12: error: value bar is not a member of UserA
              u.bar

Scala has almost full interop with Java (except a some rare cases, where it is still has interop, but painful).


It's difficult in java. But, of course, you have Properties :-)

You can use Groovy, or Javascript.

In java, you can use perhaps some string-object-document, with Properties, or with XML, Json, Yaml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜