Last active
August 29, 2015 14:13
-
-
Save hyrmn/4d7145853914adf2d204 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Threading.Tasks; | |
| using Raven.Client.Document; | |
| namespace ConsoleApplication1 | |
| { | |
| class FakePersonDocumentThing | |
| { | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var tasks = new Task[2]; | |
| tasks[0] = Task.Factory.StartNew(() => | |
| { | |
| var clientOne = AppDomain.CreateDomain("client1"); | |
| clientOne.DoCallBack(BeTheClient); | |
| AppDomain.Unload(clientOne); | |
| }); | |
| tasks[1] = Task.Factory.StartNew(() => | |
| { | |
| var clientTwo = AppDomain.CreateDomain("client2"); | |
| clientTwo.DoCallBack(AlsoBeTheClient); | |
| AppDomain.Unload(clientTwo); | |
| }); | |
| Task.WaitAll(tasks); | |
| } | |
| static void BeTheClient() | |
| { | |
| var documentStore = new DocumentStore | |
| { | |
| Url = "http://localhost:8080", | |
| Conventions = | |
| { | |
| } | |
| }; | |
| documentStore.Initialize(); | |
| new IdPrinter("one", documentStore).PrintLike(50); | |
| } | |
| static void AlsoBeTheClient() | |
| { | |
| var documentStore = new DocumentStore | |
| { | |
| Url = "http://localhost:8080", | |
| Conventions = | |
| { | |
| } | |
| }; | |
| documentStore.Initialize(); | |
| new IdPrinter("two", documentStore).PrintLike(50); | |
| } | |
| } | |
| class IdPrinter | |
| { | |
| private readonly string _name; | |
| private readonly DocumentStore _docStore; | |
| public IdPrinter(string threadName, DocumentStore docStore) | |
| { | |
| _name = threadName; | |
| _docStore = docStore; | |
| } | |
| public void PrintLike(int num) | |
| { | |
| 1.To(num).Do(i => Console.WriteLine("{0}({1}): {2}", _name, i, _docStore.GenerateIdFor<FakePersonDocumentThing>())); | |
| } | |
| } | |
| public static class FunStuff | |
| { | |
| public static void Do<T>(this IEnumerable<T> collection, Action<T> action) | |
| { | |
| foreach (var item in collection) | |
| { | |
| action(item); | |
| } | |
| } | |
| public static IEnumerable<int> To(this int from, int to) | |
| { | |
| if (from < to) | |
| { | |
| while (from <= to) | |
| { | |
| yield return from++; | |
| } | |
| } | |
| else | |
| { | |
| while (from >= to) | |
| { | |
| yield return from--; | |
| } | |
| } | |
| } | |
| } | |
| public static class RavenFunStuff | |
| { | |
| public static string GenerateIdFor<T>(this DocumentStore docStore) | |
| { | |
| // An entity instance is required to generate a key, but we only have a type. | |
| // We might not have a public constructor, so we must use reflection. | |
| var entity = Activator.CreateInstance(typeof(T), true); | |
| // Generate an ID using the commands and conventions from the current session | |
| var conventions = docStore.Conventions; | |
| string databaseName = null; | |
| var databaseCommands = docStore.DatabaseCommands; | |
| return conventions.GenerateDocumentKey(databaseName, databaseCommands, entity); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome gist, Ben! nice π π π π°