Beispiel #1
0
        /// <summary>
        /// Gets the MetaModel representing a DataContext and all it's
        /// accessible tables, functions and entities.
        /// </summary>

        public MetaModel GetModel(Type dataContextType)
        {
            if (dataContextType == null)
            {
                throw Error.ArgumentNull(nameof(dataContextType));
            }

            MetaModel model = null;

            if (this.primaryModel == null)
            {
                model = CreateModel(dataContextType);
                Interlocked.CompareExchange <MetaModel>(ref this.primaryModel, model, null);
            }

            // if the primary one matches, use it!

            if (this.primaryModel.ContextType == dataContextType)
            {
                return(this.primaryModel);
            }

            // the rest of this only happens if you are using the mapping source for
            // more than one context type

            // build a map if one is not already defined

            if (this.secondaryModels == null)
            {
                Interlocked.CompareExchange <Dictionary <Type, MetaModel> >(ref this.secondaryModels, new Dictionary <Type, MetaModel>(), null);
            }

            // if we haven't created a read/writer lock, make one now

            if (this.rwlock == null)
            {
                Interlocked.CompareExchange <ReaderWriterLock>(ref this.rwlock, new ReaderWriterLock(), null);
            }

            // lock the map and look inside

            MetaModel foundModel;

            this.rwlock.AcquireReaderLock(Timeout.Infinite);

            try {
                if (this.secondaryModels.TryGetValue(dataContextType, out foundModel))
                {
                    return(foundModel);
                }
            } finally {
                this.rwlock.ReleaseReaderLock();
            }

            // if it wasn't found, lock for write and try again

            this.rwlock.AcquireWriterLock(Timeout.Infinite);

            try {
                if (this.secondaryModels.TryGetValue(dataContextType, out foundModel))
                {
                    return(foundModel);
                }

                if (model == null)
                {
                    model = CreateModel(dataContextType);
                }

                this.secondaryModels.Add(dataContextType, model);
            } finally {
                this.rwlock.ReleaseWriterLock();
            }

            return(model);
        }
 internal UnmappedType(MetaModel model, Type type)
 {
     this.Model = model;
     this.Type  = type;
 }