Guest User

Untitled

a guest
Jan 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. public class MyObj {
  2. public string Id { get; set; }
  3. [Increment]
  4. public int OtherProp { get; set; }
  5. }
  6.  
  7. public class OtherPropIncrementListener : IDocumentStoreListener {
  8. HiLoKeyGenerator _generator;
  9. IDocumentStore _store;
  10. public BlavenIdStoreListener(IDocumentStore store) {
  11. this._store = store;
  12. _generator = new HiLoKeyGenerator(store.DatabaseCommands, "MyObjs", 1);
  13. }
  14.  
  15. public void AfterStore(string key, object entityInstance, RavenJObject metadata) { }
  16.  
  17. public bool BeforeStore(string key, object entityInstance, RavenJObject metadata, RavenJObject original) {
  18. var myObj = entityInstance as MyObj;
  19. if(myObj != null && myObj.OtherProp == 0) {
  20. string documentKey = _generator.GenerateDocumentKey(_store.Conventions, entityInstance);
  21. myObj.OtherProp = int.Parse(documentKey.Substring(documentKey.IndexOf("/") + 1));
  22. return true;
  23. }
  24. return false;
  25. }
  26. }
  27.  
  28. documentStore.RegisterListener(new BlavenIdStoreListener(documentStore));
Add Comment
Please, Sign In to add comment