2016年7月21日 星期四

[Scala IA] The Basics : Ch3. OOP in Scala - Building a Scala MongoDB driver: user stories

Preface 
In this chapter you’ll build a Scala driver for MongoDB. MongoDB is a scalable, document-oriented database. You’ll build this driver incrementally using the object-oriented constructs provided by Scala, and along the way I’ll explain each concept in detail. Scala has made some object-oriented innovations, and one of them is the trait. Traits are similar to abstract classes with partial implementation. You’ll explore how to use traits when building Scala applications. As you move through the chapter, you’ll learn about Scala case classes. Case classes are useful when it comes to building immutable classes, particularly in the context of concurrency and data transfer objects. Case classes also allow Scala to bridge the gap between functional programming and OOP in terms of pattern matching. Without wasting any more time, let’s learn OOP programming in Scala by building a MongoDB driver. 

Building a Scala MongoDB driver: user stories 
To explore Scala’s object-oriented constructs and use, let’s build a MongoDB driver. While building this example driver, you’ll dive deep into concepts for a thorough understanding. You won’t need to start from scratch because you’ll use an existing Java driver for MongoDB. You’ll build a Scala wrapper over the Java MongoDB driver. That way, you don’t have to deal with the low-level MongoDB API and can focus on your objective of learning Scala.

The user stories you’ll be implementing in your Scala wrapper driver are as follows: 
As a developer, I want an easier way to connect to my MongoDB server and access document databases.
As a developer, I want to query and manage documents.

WHAT’S A USER STORY? 
A good way to think about a user story is as a reminder to have a conversation with your customer (in Agile, project stakeholders are called customers), which is another way to say it’s a reminder to do some just-in-time analysis. In short, user stories are slim and high-level requirements artifacts.

MongoDB is a scalable, high-performance, open source, schema-free, documentoriented database written in C++.1 MongoDB is a document-based database that uses JSON (JavaScript Object Notation). The schema-free feature lets MongoDB store any kind of data of any structure. You don’t have to define your database tables and attributes up front. You can add or remove attributes whenever you need them. This flexibility is achieved through the document-based model. Unlike relational databases, in a document-based model records are stored as documents in which any number of fields of any length can be stored. For example, you could have the following JSON documents in a single collection (collection in MongoDB is like a table in a traditional RDBMS): 
  1. { name : "Joe", x : 3.3, y : [1,2,3] }  
  2. { name : "Kate", x : "abc" }  
  3. { q : 456 }  
In a schema-free environment, the concept of schema moves more toward the application than to the database. Like any other tool, there are pros and cons for using a schema-free database, and it depends on the solution you’re trying to solve. 

The format of the document in which the information is stored in MongoDB is BSON (binary JSON). Other document-based databases like Lotus Notes (IBM) and SimpleDB (Amazon.com) use XML for information storage. JSON has an added advantage when working with web-based applications because JSON content can be easily consumed with little transformation. A great place to get a feel for MongoDB is http://try.mongodb.org. Go ahead and download MongoDB (www.mongodb.org/display/DOCS/Downloads). Then, unpack it and run the following command to start the MongoDB server: 
$ bin/mongod

To connect to the MongoDB server, use the client shell that ships with the distribution of MongoDB: 
$ bin/mongo
MongoDB shell version: 1.2.4
url: test
connecting to: test
type "help" for help

>

At this point you should be ready to start building the Scala wrapper driver. If you’re interested in learning more about MongoDB, look at the MongoDB tutorial (www.mongodb.org/display/DOCS/Tutorial).

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...