GAE:如何有效地将面向对象的设计映射到Appengine数据存储中 [英] GAE : How to map object oriented designs into Appengine datastore efficiently

查看:91
本文介绍了GAE:如何有效地将面向对象的设计映射到Appengine数据存储中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有以下对象模型(我可以自由地以你喜欢的方式改变它,我的兴趣只是有效地映射),

  public class Product {
private String someAttribute;
// ...
}

//卖家有很多产品可以出售,多于一个卖家可以出售相同的产品
public class卖家{
私人列表<产品>产品;
// ...
}

公开课提供{
//谁提供
私人卖家卖家;
//多个产品可以提出报价
私人列表< Product>产品;
//定价相关
私人价格价格;
// ...
}

我想编写一个web应用程序,它将执行,读/写/搜索这些对象,并考虑入口点可能是其中的任何一个。



以下是我的查询, -


  1. 我将使用什么样的设计模式?我想某种适配器模式?

  2. 在存储卖方对象的同时,我是否也应该存储Product对象的引用?或只是关键?我的意思是我应该将卖家类转换为以下内容:

    公共类卖家{
    私人列表productKeys;
    }

  3. 如果我想编写一个可伸缩和可维护的应用程序,我认为应该有一些很好的方式来设计我的应用程序在分层架构中。有没有一些例子或者一些已经使用过的模式?例如某种JSP - >服务 - > DAO - > dataStore?示例将非常感激。 我也浏览了Java的AppEngineSDK,但没有找到一个示例一样。在答案或指针中提供帮助是非常值得赞赏的。

    解决方案

    对于NoSQL数据库(例如GAE Datastore),您应该考虑数据访问模型,而不是数据存储模型。即你应该考虑你将如何访问数据以及如何组织数据,以便访问效率最高(=更快,更便宜)。



    不幸的是,这往往违背了OOP原则和数据存储架构(没有Datastore架构,您意识到这一点,是吗?)不适合Java OOP范例。

    实现这一目标的一种方法是对数据进行反规范化处理,这意味着您可以将数据(尽可能多)写入一个大记录中,这也意味着数据是冗余的。

    现在,由于您无法将所有数据放入一个记录中,因此您需要做出一些让步。要做到这一点,您必须做出以下决定:


    1. 哪些数据最容易读取?

    2. 最大的变化是什么?

    在您的模型中,我猜想产品&卖家不会有太大变化,但是优惠通常会被创建,对吗?尝试设计,以便最大限度地减少读/写次数 - 从最常用的数据开始,并组织模式,以便以最少数量的读/写操作获取/放入大部分数据。



    尝试回答您的问题:


    1. 只需创建您的服务层,在其中执行基本的CRUD功能(添加/更新/删除/查找卖方/产品/等)和更高级别的业务功能(为产品创建卖方报价等)。 DAO是PITA,所以你应该尽可能地使用POJO - 使用Objectify。在这种情况下,你可以取消适配器模式。


    2. 如果通过引用指代ID(long,Long或String),那么您应该使用keys:keys是父键,种类和ID的组合。密钥保证是唯一的,而ID不是(只有当你不使用实体组时它们才是唯一的)。另外,通过客观化,键是类型安全的,而ID不是。

    3. 正如1.中所描述的,分层方法是要走的路。尽量避免使用DAO - 他们需要大量的手工工作,因此会产生错误。


    Lets assume that I have the following object Model with me (you are free to change it in a way you like, my interest is only mapping efficiently),

    public class Product{
        private String someAttribute;
        //...
    }
    
    // A Seller who has a lot of products to sell, More than one Seller can sell the same Product
    public class Seller{
        private List<Product> products;
        //...
    }
    
    public class Offer{
        // who is offering
        private Seller seller;
        // multiple products can make an offer
        private List<Product> products;
        // pricing related
        private Price price;
        //...
    }
    

    I want to write a web app, which will perform, read/write/search on these objects and consider the point of entry could be any one of them.

    Following are my queries,-

    1. What is the design pattern, that I shall use ? I guess some kind of Adapter pattern?
    2. While storing Seller objects, should I store the references of Product objects as well? or just the keys ? What I mean is should I convert the Seller class into following,

      public class Seller { private List productKeys; }

    3. If I want to write a scalable and maintainable application, I think there should be some nice way design my Application in layered architecture. Is there some example for that or some already used pattern? e.g. Some kind of JSP --> Service --> DAO --> dataStore ? Examples would be really appreciated.

    I went through the AppEngineSDK for java as well, but did not hit an example for the same. help in answers or pointers is truly appreciated.

    解决方案

    With NoSQL databases (such as GAE Datastore) you should be thinking in terms of data access model, not data storage model. I.e. you should be thinking how you will be accessing data and how you will organize data so that access is most efficient (= faster and cheaper).

    Unfortunately this often goes against OOP principles and Datastore "schema" (there is no Datastore schema, you realize that, don't you?) does not fit nicely into Java OOP paradigm.

    A way to achieve this is to de-normalize data, meaning that you write data (as much as possible) in one big record, which also means that data is redundant.

    Now, since you can not put all data in one record, you need to make several concessions. To do this you must decide:

    1. Which data will be read most?
    2. Which data will be changed most?

    From your model I'd guess that Product & Seller would not change much, but Offer will be often created, right? Try to design so that you minimize number of reads/writes - start with data that is used most and organise schema so that you get/put most of data with minimum number of reads/writes.

    Trying to answer your questions:

    1. Just create your Service layer, where you perform basic CRUD functions (add/update/delete/find Seller/Product/etc) and higher-level business functions (create Offer for Seller with Products, etc..). DAOs are PITA, so you should try to work with POJOs as much as possible - use Objectify. In this case you could do away with Adapter pattern.

    2. If by references you meant IDs (long, Long or String) then you should go with keys: keys are a combination of parent keys, kind and ID. Keys are guaranteed to be unique, while IDs are not (they are unique only if you are not using Entity Groups). Also, with objectify, keys are type-safe, while IDs are not.

    3. As described in 1., layered approach is the way to go. Do try to avoid DAOs though - they require a lot of manual work and as such are a source of errors.

    这篇关于GAE:如何有效地将面向对象的设计映射到Appengine数据存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆