Git Product home page Git Product logo

thneed's Introduction

Thneed

This thing is a Thneed.
A Thneed's a Fine-Something-That-All-People-Need!

A library for defining the traversable hierarchy of data models.

ModelGraph<DataModel> graph = ModelGraph.of(DataModel.class)
  .identifiedByDefault().by(BaseColumns._ID)
  .with(ANNOUNCEMENT)
  .where()
  .the(VENDOR).references(TRACK).by(Vendors.TRACK_ID)
  .the(SESSION).references(ROOM).by(Sessions.ROOM_ID)
  .the(SESSION).references(BLOCK).by(Sessions.BLOCK_ID)
  .the(SESSIONS_SPEAKERS)
  .links(SESSION).by(SessionsSpeakers.SESSION_ID)
  .with(SPEAKERS).by(SessionsSpeakers.SPEAKER_ID)
  .the(SESSIONS_TRACKS)
  .links(SESSION).by(SessionsTracks.SESSION_ID)
  .with(TRACK).by(SessionsTracks.TRACK_ID)
  .build();

It's a shirt.
It's a sock.
It's a glove.
It's a hat.

The end goal of defining your data model structure is reduction of boilerplate code in classes handling the data model. See this package of Google I/O 2012 Android app for an examples of the code that I'd like to get rid of. For every model and relationship between models programmer has to define a ContentProvider endpoint with Uri builder, Uri matcher, database query, ContentResolver notifications, and so on. This amounts to a lot of boilerplate code, especially when the number of different entities in the system grows.

Thneed's approach to this problem is to define all the relationships and models using fluent builder API as shown above. The DataModel class supplied to the ModelGraph builder should implement some meaningful interfaces:

interface DatabaseModel {
  String getTableName();
  // …
}

interface ContentProviderModel {
  boolean createTopLevelEndpoint();
  // ...
}

abstract static class DataModel implements DatabaseModel, ContentProviderModel {
}

Each entity type is an implementation of those interfaces:

static final DataModel SESSION = new DataModel() {
  // implementation of interfaces
}

The next step is writing a ModelVisitor or RelationshipVisitor which accepts a subtype of this interface and use the data gathered in ModelGraph object to do something useful:

Image visualisation = ModelGraphVisualiser.visualize(graph);

TableJoiner joiner = TableJoiner.from(graph);

ContentProviderHelper contentProviderHelper = ContentProviderHelper.from(graph);
UriBuilder uriBuilder = contentProviderHelper.getUriBuilder();
UriMatcher uriMatcher = contentProviderHelper.getUriMatcher();

But it has other uses. Yes, far beyond that.
You can use it for carpets. For pillows! For sheets!
Or curtains! Or covers for bicycle seats!

The nice thing about the Visitor pattern is that you can define as many different Visitors as you need. In theory this should allow you to get rid of all of the boilerplate code related to data models.

I'm very curious what problems can be solved with this approach, so please let me know how do you use the Thneed!

Usage

Just add the dependency to your build.gradle:

dependencies {
    compile 'org.chalup.thneed:thneed:0.7'
}

Caveats

  • The API is subject to heavy change. This is more of a draft of the proof of concept than a production ready library.
  • The Visitors from the code snippets above are not implemented anywhere (yet). They are just names suggesting how you can use the information about data model relationships to reduce boilerplate code.

License

Copyright (C) 2013 Jerzy Chalupski

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 

thneed's People

Contributors

chalup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

thneed's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.