Skip to content

Commit

Permalink
Merge pull request #101 from ml054/v4.0
Browse files Browse the repository at this point in the history
indentation Chapters 02 - 04
  • Loading branch information
ayende authored Jul 4, 2018
2 parents de0443a + 0604052 commit a85142e
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 229 deletions.
152 changes: 76 additions & 76 deletions Ch02/Ch02.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ Start by adding the code in Listing 2.2 to the query text.
from Categories
update {
this.Name = [
{"Lang": "en-us", "Text": this.Name }
{ "Lang": "en-us", "Text": this.Name }
];
this.Description = [
{"Lang": "en-us", "Text": this.Description }
{ "Lang": "en-us", "Text": this.Description }
];
}
```
Expand Down Expand Up @@ -506,32 +506,32 @@ like the code in Listing 2.11.
```{caption="Common pattern for initialization of the DocumentStore" .cs }
public class DocumentStoreHolder
{
private readonly static Lazy<IDocumentStore> _store =
new Lazy<IDocumentStore>(CreateDocumentStore);

private static IDocumentStore CreateDocumentStore()
{
var documentStore = new DocumentStore
{
Urls = // urls of the nodes in the RavenDB Cluster
{
"https://ravendb-01:8080",
"https://ravendb-02:8080",
"https://ravendb-03:8080",
},
Certificate =
new X509Certificate2("tasks.pfx"),
Database = "Tasks",
};

documentStore.Initialize();
return documentStore;
}

public static IDocumentStore Store
{
get { return _store.Value; }
}
private readonly static Lazy<IDocumentStore> _store =
new Lazy<IDocumentStore>(CreateDocumentStore);

private static IDocumentStore CreateDocumentStore()
{
var documentStore = new DocumentStore
{
Urls = // urls of the nodes in the RavenDB Cluster
{
"https://ravendb-01:8080",
"https://ravendb-02:8080",
"https://ravendb-03:8080",
},
Certificate =
new X509Certificate2("tasks.pfx"),
Database = "Tasks",
};

documentStore.Initialize();
return documentStore;
}

public static IDocumentStore Store
{
get { return _store.Value; }
}
}
```

Expand Down Expand Up @@ -562,8 +562,8 @@ who want to use the entity name as part of the property name. So they'll have Or

Here's how we tell the client API to apply the `TypeName + Id` policy:

documentStore.Conventions.FindIdentityProperty =
prop => prop.Name == prop.DeclaringType.Name + "Id";
documentStore.Conventions.FindIdentityProperty =
prop => prop.Name == prop.DeclaringType.Name + "Id";


Don't worry. We won't go over all of the available options, since there are quite a few of them. Please refer to the
Expand Down Expand Up @@ -648,10 +648,10 @@ As the name implies, this gives you the option of loading a document or a set of
loaded into the session is managed by the session. Any changes made to the document would be persisted to the database when
you call `SaveChanges`. A document can only be loaded once in a session. Let's look at the following code:

var t1 = session.Load<ToDoTask>("ToDoTasks/1-A");
var t2 = session.Load<ToDoTask>("ToDoTasks/1-A");

Assert.True(Object.ReferenceEquals(t1, t2));
var t1 = session.Load<ToDoTask>("ToDoTasks/1-A");
var t2 = session.Load<ToDoTask>("ToDoTasks/1-A");
Assert.True(Object.ReferenceEquals(t1, t2));

Even though we called `Load<ToDoTask>("ToDoTasks/1-A")` twice, there's only a single remote call to the server and only a single
instance of the `ToDoTask` class. Whenever you load a document, it's added to an internal dictionary that the session
Expand All @@ -663,11 +663,11 @@ obvious when talking about the `Load` operation, but it also applies to `Query`

`Load` can also be used to read more than a single document at a time. For example, if I wanted three documents, I could use:

Dictionary<string, ToDoTask> tasks = session.Load<ToDoTask>(
"ToDoTasks/1-A",
"ToDoTasks/2-A",
"ToDoTasks/3-A"
);
Dictionary<string, ToDoTask> tasks = session.Load<ToDoTask>(
"ToDoTasks/1-A",
"ToDoTasks/2-A",
"ToDoTasks/3-A"
);

This will result in a dictionary with all three documents in it, retrieved in a single remote call from the server. If a
document we tried to load wasn't found on the server, the dictionary will contain null for that document ID.
Expand Down Expand Up @@ -704,22 +704,22 @@ transaction. But we haven't actually worked with anything more complex than a `T
of the amount of complexity we can express. Listing 2.12 lets us add the notion of people who can be assigned tasks to the model.

```{caption="People and Tasks model in RavenDB" .cs}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
public class ToDoTask
{
public string Id { get; set; }
public string Task { get; set; }
public bool Completed { get; set; }
public DateTime DueDate { get; set; }
public string AssignedTo { get; set; }
public string CreatedBy { get; set; }
}
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
}
public class ToDoTask
{
public string Id { get; set; }
public string Task { get; set; }
public bool Completed { get; set; }
public DateTime DueDate { get; set; }
public string AssignedTo { get; set; }
public string CreatedBy { get; set; }
}
```

From looking at the model in Listing 2.12, we can learn a few interesting tidbits. First, we can see that each class stands on
Expand Down Expand Up @@ -800,7 +800,7 @@ using (var session = store.OpenSession())
Console.WriteLine(
$"{task.Id} - {task.Task} by {assignedTo.Name}");
// will print 2
// will print 2
Console.WriteLine(session.Advanced.NumberOfRequests);
}
Expand Down Expand Up @@ -830,14 +830,14 @@ using (var session = store.OpenSession())
ToDoTask task = session
.Include<ToDoTask>(x => x.AssignedTo)
.Load(taskId);
.Load(taskId);
Person assignedTo = session.Load<Person>(task.AssignedTo);
Console.WriteLine(
$"{task.Id} - {task.Task} by {assignedTo.Name}");
// will print 1
// will print 1
Console.WriteLine(session.Advanced.NumberOfRequests);
}
Expand Down Expand Up @@ -882,9 +882,9 @@ but that features is exposed differently (via the `declare function` mode, which
Using multiple `Include`s on the same operation, however, is just fine. Let's load a task, and with it we'll include both the
assigned to person and the one who created the task. This can be done using the following snippet:

ToDoTask task = session.Include<ToDoTask>(x => x.AssignedTo)
.Include(x => x.CreatedBy)
.Load(taskId);
ToDoTask task = session.Include<ToDoTask>(x => x.AssignedTo)
.Include(x => x.CreatedBy)
.Load(taskId);

Now I can load both the `AssignedTo` person and the `CreatedBy` one, and there's still only a single round trip to the server.
What about when both of them are pointing at the same document? RavenDB will return just a single copy of the document, even if
Expand All @@ -905,10 +905,10 @@ about the kind of environment that RavenDB offers to make sense of the choices w
Deleting a document is done through the appropriately named `Delete` method. This method can accept an entity instance or a
document ID. The following are various ways to delete a document:

var task = session.Load<ToDoTask>("ToDoTasks/1-A");
session.Delete(task); // delete by instance
var task = session.Load<ToDoTask>("ToDoTasks/1-A");
session.Delete(task); // delete by instance

session.Delete("ToDoTasks/1-A"); // delete by ID
session.Delete("ToDoTasks/1-A"); // delete by ID

It's important to note that calling `Delete` doesn't actually delete the document. It merely marks that document as deleted
in the session. It's only when `SaveChanges` is called that the document will be deleted.
Expand Down Expand Up @@ -937,7 +937,7 @@ document. We've already seen this method used several times in this chapter, but

var person = new Person
{
Name = "Oscar Arava"
Name = "Oscar Arava"
};
session.Store(person);

Expand Down Expand Up @@ -969,12 +969,12 @@ document updates in `SaveChanges`. An entity is always saved to a document as a

The typical way one would work with the session is:

using (var session = documentStore.OpenSession())
{
// do some work with the session
using (var session = documentStore.OpenSession())
{
// do some work with the session

session.SaveChanges();
}
session.SaveChanges();
}

So `SaveChanges` is usually only called once per session, although there's nothing wrong with calling it multiple times. If
the session detects that there have been no changes to the entities, it will skip calling to the server entirely.
Expand Down Expand Up @@ -1044,17 +1044,17 @@ sync version.
```{caption="Working with the async session" .cs }
using (var session = documentStore.OpenAsyncSession())
{
var person = new Person
var person = new Person
{
Name = "Oscar Arava"
};
await session.StoreAsync(person);
await session.SaveChangesAsync();
await session.SaveChangesAsync();
}

using (var session = documentStore.OpenAsyncSession())
{
var tasksPerDayQuery =
var tasksPerDayQuery =
from t in session.Query<ToDoTask>()
group t by t.DueDate into g
select new
Expand All @@ -1064,7 +1064,7 @@ using (var session = documentStore.OpenAsyncSession())
};
List<ToDoTask> tasksToDo = await tasksPerDayQuery.ToListAsync();

foreach (var task in tasksToDo)
foreach (var task in tasksToDo)
{
Console.WriteLine($"{task.Id} - {task.Task} - {task.DueDate}");
}
Expand Down Expand Up @@ -1119,7 +1119,7 @@ is available as operations that can be invoked in such a manner.
In other cases, you can use an `Operation` to run something that doesn't make sense in the context of a session. For example,
let's say I wanted to delete all of the tasks in the database. I could do it with the following code:

store.Operations.Send(new DeleteByQueryOperation(
store.Operations.Send(new DeleteByQueryOperation(
new IndexQuery { Query = "from ToDoTasks" }
));

Expand Down Expand Up @@ -1610,7 +1610,7 @@ executable. Listing 2.24 shows the implementation of `RavenExecLocator`.
public class RavenExecLocator : RavenTestDriver.Locator
{
public override string ExecutablePath =>
@"d:\RavenDB\Raven.Server.exe";
@"d:\RavenDB\Raven.Server.exe";
}
```

Expand Down
18 changes: 9 additions & 9 deletions Ch03/Ch03.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,22 @@ parents, registrations, etc. On the face of it, this is a pretty simple model. W
```{caption="Simple kindergarten model" .cs}
public class Parent
{
public string Name { get; set; }
public string Name { get; set; }
}
public class Registration
{
public DateTime EnrolledAt { get; set; }
public EnrollmentType Type { get; set; }
public DateTime EnrolledAt { get; set; }
public EnrollmentType Type { get; set; }
}
public class Child
{
public string Name { get; set; }
public DateTime Birthday { get; set; }
public Parent Father { get; set; }
public Parent Mother { get; set; }
public Registration Registration { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public Parent Father { get; set; }
public Parent Mother { get; set; }
public Registration Registration { get; set; }
}
```

Expand All @@ -272,7 +272,7 @@ to our kindergarten.

```{caption="Kindergarten record of Alice in Wonderland" .json}
// children/alice-liddell
{
{
"Name": "Alice Liddell",
"Birthday": "2012-05-04T00:00:00.0000000Z",
"Mother": {
Expand Down
Loading

0 comments on commit a85142e

Please sign in to comment.