コード例 #1
0
ファイル: Session.cs プロジェクト: dbones-labs/arm-chair
        public virtual void Add <T>(T instance) where T : class
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            var type  = typeof(T);
            var value = _idAccessor.GetId(instance);
            var key   = _idManager.GetFromId(typeof(T), value);

            if (key == null)
            {
                key = _idManager.GenerateId(type);
                _idAccessor.SetId(instance, key.Id);
            }

            var entry = new SessionEntry()
            {
                Action   = ActionType.Add,
                Instance = instance,
                Key      = key
            };

            _sessionCache.Attach(entry);
        }
コード例 #2
0
        protected virtual async Task <IEnumerable <T> > Load <T>(IEnumerable ids, ISessionCache sessionCache,
                                                                 ITrackingProvider tracking, IAction <IEnumerable <LoadContext> > loadTask) where T : class
        {
            //setup the pipeline
            var pipe = new Middleware <IEnumerable <LoadContext> >();

            pipe.Use(new TrackingAction <LoadContext>(tracking));
            pipe.Use(new SessionAction <LoadContext>(sessionCache));
            pipe.Use(loadTask);

            //setup the load context
            var type         = typeof(T);
            var loadContexts = ids.Cast <object>().Select(id =>
            {
                var idKey = _idManager.GetFromId(type, id);
                return(new LoadContext()
                {
                    Key = idKey,
                    Type = type
                });
            }).ToList();


            await pipe.Execute(loadContexts);

            return(loadContexts.Select(x => x.Entity).Where(x => x != null).Cast <T>());
        }
コード例 #3
0
        public Task <IEnumerable <LoadContext> > Execute(QueryContext context,
                                                         Next <QueryContext, IEnumerable <LoadContext> > next)
        {
            var ctx   = context;
            var q     = ctx.Query;
            var query = new MongoQueryRequest
            {
                Selector = q.Selector,
                Limit    = q.Limit,
                Skip     = q.Skip,
                Sort     = q.Sort
            };

            var allDocs = _couchDb.MongoQuery(query);

            //using the loaded docs, update the context entries.
            var results =
                (from entity in allDocs.Docs
                 let id = _idAccessor.GetId(entity)
                          let key = _idManager.GetFromId(entity.GetType(), id)
                                    select new LoadContext()
            {
                Entity = entity,
                Key = key,
                Type = entity.GetType()
            }).ToList();

            return(Task.FromResult((IEnumerable <LoadContext>)results));
        }
コード例 #4
0
        public Task Execute(IEnumerable <LoadContext> context, Next <IEnumerable <LoadContext> > next)
        {
            var items = context.ToList(); //ensure 1 iteration over list. (tasks to run once)

            //we do not want to load items which have been loaded from the cache
            var toLoad  = items.Where(x => !x.LoadedFromCache).ToDictionary(loadContext => loadContext.Key.CouchDbId);
            var allDocs = _couchDb.LoadAllEntities(new AllDocsRequest()
            {
                Keys = toLoad.Keys
            });

            //using the loaded docs, update the context entries.
            foreach (var entity in allDocs.Rows.Select(x => x.Doc))
            {
                var id  = _idAccessor.GetId(entity);
                var key = _idManager.GetFromId(entity.GetType(), id);
                toLoad[key.CouchDbId].Entity = entity;
            }

            return(Task.CompletedTask);
        }