public INakedObjectAdapter CreateAdapter(object domainObject, IOid oid, IVersion version)
        {
            if (domainObject == null)
            {
                return(null);
            }

            if (oid == null)
            {
                var objectSpec = metamodel.GetSpecification(domainObject.GetType());
                if (objectSpec.ContainsFacet(typeof(IComplexTypeFacet)))
                {
                    return(GetAdapterFor(domainObject));
                }

                if (objectSpec.HasNoIdentity)
                {
                    return(AdapterForNoIdentityObject(domainObject));
                }

                return(AdapterForExistingObject(domainObject, objectSpec));
            }

            return(AdapterForExistingObject(domainObject, oid));
        }
        public NakedObjectAdapter(IMetamodelManager metamodel,
                                  ISession session,
                                  IObjectPersistor persistor,
                                  ILifecycleManager lifecycleManager,
                                  INakedObjectManager nakedObjectManager,
                                  object poco,
                                  IOid oid,
                                  ILoggerFactory loggerFactory,
                                  ILogger <NakedObjectAdapter> logger)
        {
            this.metamodel          = metamodel ?? throw new InitialisationException($"{nameof(metamodel)} is null");
            this.session            = session ?? throw new InitialisationException($"{nameof(session)} is null");
            this.persistor          = persistor ?? throw new InitialisationException($"{nameof(persistor)} is null");
            this.nakedObjectManager = nakedObjectManager ?? throw new InitialisationException($"{nameof(nakedObjectManager)} is null");
            this.logger             = logger ?? throw new InitialisationException($"{nameof(logger)} is null");
            this.lifecycleManager   = lifecycleManager ?? throw new InitialisationException($"{nameof(lifecycleManager)} is null");

            if (poco is INakedObjectAdapter)
            {
                throw new AdapterException(logger.LogAndReturn($"Adapter can't be used to adapt an adapter: {poco}"));
            }

            Object       = poco;
            Oid          = oid;
            ResolveState = new ResolveStateMachine(this, session);
            Version      = new NullVersion(loggerFactory.CreateLogger <NullVersion>());
        }
 public INakedObjectAdapter LoadObject(IOid oid, ITypeSpec spec)
 {
     Log.DebugFormat("LoadObject oid: {0} specification: {1}", oid, spec);
     Assert.AssertNotNull("needs an OID", oid);
     Assert.AssertNotNull("needs a specification", spec);
     return(nakedObjectManager.GetKnownAdapter(oid) ?? objectPersistor.LoadObject(oid, (IObjectSpec)spec));
 }
        public OidWrapper(IOid oid)
        {
            SurfaceUtils.AssertNotNull(oid, "Oid is null");


            this.oid = oid;
        }
Example #5
0
        internal ProgrammableNakedObject(object poco, INakedObjectSpecification specification, IOid oid) {
            this.poco = poco;
            this.specification = specification;
            this.oid = oid;

            resolveState = new ResolveStateMachine(this);
        }
 public virtual void ConvertTransientToPersistentOid(IOid oid) {
     lock (this) {
         Assert.AssertTrue(oid is SerialOid);
         var serialOid = (SerialOid) oid;
         serialOid.MakePersistent(persistentSerialNumber++);
     }
 }
 public void ConvertTransientToPersistentOid(IOid oid) {
     var entityOid = oid as EntityOid;
     if (entityOid != null) {
         entityOid.MakePersistent();
     }
     Log.DebugFormat("Converted transient OID to persistent {0}", oid);
 }
        /// <summary>
        ///     Remove the reference for an instance from the list of all instances
        /// </summary>
        protected internal virtual void RemoveInstance(IOid oid, string type)
        {
            IList <IOid> instances = LoadInstances(type).ToList();

            instances.Remove(oid);
            WriteInstanceFile(type, instances);
        }
 public virtual void Add(IOid oid, INakedObject adapter) {
     lock (adapters) {
         adapters[oid] = adapter;
     }
     // log after so that adapter is in map if required by ToString
     Log.DebugFormat("Add {0} as {1}", oid, adapter);
 }
 public AggregateOid(IMetamodelManager metamodel, IOid oid, string id, string typeName)
 {
     this.metamodel = metamodel ?? throw new InitialisationException($"{nameof(metamodel)} is null");
     ParentOid      = oid ?? throw new InitialisationException($"{nameof(oid)} is null");
     FieldName      = id ?? throw new InitialisationException($"{nameof(id)} is null");
     this.typeName  = typeName ?? throw new InitialisationException($"{nameof(typeName)} is null");
 }
        // TODO move this onto lifecycle manager
        public static INakedObjectAdapter GetNakedObjectFromId(this INakedObjectsFramework framework, string encodedId)
        {
            if (string.IsNullOrEmpty(encodedId))
            {
                return(null);
            }

            IOid oid = framework.LifecycleManager.RestoreOid(encodedId.Split(';'));

            if (oid is ICollectionMemento)
            {
                return(RestoreCollection(oid as ICollectionMemento));
            }

            if (oid is IAggregateOid)
            {
                return(framework.RestoreInline(oid as IAggregateOid));
            }

            if (oid is IViewModelOid)
            {
                return(framework.RestoreViewModel(oid as IViewModelOid));
            }

            return(framework.RestoreObject(oid));
        }
        public void Add(IOid oid, INakedObjectAdapter adapter)
        {
            adapters[oid] = adapter;

            // log after so that adapter is in map if required by ToString
            Log.DebugFormat("Add {0} as {1}", oid, adapter);
        }
        public INakedObjectAdapter LoadObject(IOid oid, IObjectSpec spec)
        {
            Assert.AssertNotNull("needs an OID", oid);
            Assert.AssertNotNull("needs a specification", spec);

            return(objectStore.GetObject(oid, spec));
        }
Example #14
0
 public INakedObjectAdapter GetKnownAdapter(IOid oid)
 {
     if (identityMap.IsIdentityKnown(oid))
     {
         return(GetAdapterFor(oid));
     }
     return(null);
 }
        private static INakedObjectAdapter RestoreInline(this INakedObjectsFramework framework, IAggregateOid aggregateOid)
        {
            IOid parentOid             = aggregateOid.ParentOid;
            INakedObjectAdapter parent = framework.RestoreObject(parentOid);
            IAssociationSpec    assoc  = parent.GetObjectSpec().Properties.Where((p => p.Id == aggregateOid.FieldName)).Single();

            return(assoc.GetNakedObject(parent));
        }
Example #16
0
        private INakedObjectAdapter CreateAdapterForNewObject(object domainObject)
        {
            IOid transientOid           = oidGenerator.CreateTransientOid(domainObject);
            INakedObjectAdapter adapter = NewAdapterForKnownObject(domainObject, transientOid);

            identityMap.AddAdapter(adapter);
            return(adapter);
        }
Example #17
0
        private static void InitInlineObjectSetupReference(INakedObject nakedObject, ObjectData data, INakedObjectAssociation field)
        {
            var          fieldData    = (Data)data.GetField(field.Id);
            IOid         referenceOid = fieldData.Oid;
            INakedObject reference    = SetupReference(field, referenceOid, fieldData, nakedObject);

            Persistor.InitInlineObject(nakedObject.Object, reference.Object);
        }
 public AggregateOid(string[] strings) {
     var helper = new StringDecoderHelper(strings);
     typeName = helper.GetNextString();
     fieldName = helper.GetNextString();
     if (helper.HasNext) {
         parentOid = (IOid) helper.GetNextEncodedToStrings();
     }
 }
        public void Remove(IOid oid)
        {
            Data   data = LoadData(oid);
            string type = data.ClassName;

            RemoveInstance(oid, type);
            DeleteData(oid, type);
        }
Example #20
0
            public virtual void Execute(IExecutionContext context)
            {
                Log.DebugFormat("Destroy object {0}", nakedObject);
                IOid oid = nakedObject.Oid;

                xmlStore.dataManager.Remove(oid);
                nakedObject.OptimisticLock = new NullVersion();
            }
 public virtual INakedObject GetAdapter(IOid oid) {
     lock (adapters) {
         if (adapters.ContainsKey(oid)) {
             return adapters[oid];
         }
         return null;
     }
 }
 public INakedObjectAdapter GetAdapter(IOid oid)
 {
     if (adapters.ContainsKey(oid))
     {
         return(adapters[oid]);
     }
     return(null);
 }
 public PocoAdapter(object poco, IOid oid) {
     if (poco is INakedObject) {
         throw new AdapterException("Adapter can't be used to adapt an adapter: " + poco);
     }
     this.poco = poco;
     this.oid = oid;
     ResolveState = new ResolveStateMachine(this);
     version = new NullVersion();
 }
        public INakedObjectAdapter GetAdapterFor(IOid oid)
        {
            if (oid == null)
            {
                throw new AdapterException(logger.LogAndReturn("must have an OID"));
            }

            return(identityMap.GetAdapterFor(oid));
        }
Example #25
0
        private INakedObject CreateAdapterForNewObject(object domainObject)
        {
            IOid         transientOid = oidGenerator.CreateTransientOid(domainObject);
            INakedObject adapter      = NewAdapterForKnownObject(domainObject, transientOid);

            Log.DebugFormat("Creating adapter (transient) {0}", adapter);
            identityMap.AddAdapter(adapter);
            return(adapter);
        }
 public void CopyFrom(IOid oid) {
     Assert.AssertTrue("Copy from Oid must be Entity Oid", oid is EntityOid);
     var from = (EntityOid) oid;
     Key = from.Key;
     TypeName = from.TypeName;
     EntityKey = from.EntityKey;
     IsTransient = from.IsTransient;
     CacheState();
 }
Example #27
0
        public INakedObjectAdapter CreateAdapter(object obj, IOid oid)
        {
            if (!isInitialized)
            {
                throw new InitialisationException("NakedObjectFactory not initialized");
            }

            return(new NakedObjectAdapter(metamodelManager, session, persistor, lifecycleManager, nakedObjectManager, obj, oid, loggerFactory, loggerFactory.CreateLogger <NakedObjectAdapter>()));
        }
        public void SetATransientOid(IOid newOid)
        {
            if (!newOid.IsTransient)
            {
                throw new NakedObjectSystemException("New Oid must be transient");
            }

            Oid = newOid;
        }
        public INakedObject LoadObject(IOid oid, IObjectSpec spec)
        {
            Log.DebugFormat("LoadObject oid: {0} specification: {1}", oid, spec);

            Assert.AssertNotNull("needs an OID", oid);
            Assert.AssertNotNull("needs a specification", spec);

            return(objectStore.GetObject(oid, spec));
        }
 public AggregateOid(IMetamodelManager metamodel, string[] strings) {
     Assert.AssertNotNull(metamodel);
     this.metamodel = metamodel;
     var helper = new StringDecoderHelper(metamodel, strings);
     typeName = helper.GetNextString();
     fieldName = helper.GetNextString();
     if (helper.HasNext) {
         parentOid = (IOid) helper.GetNextEncodedToStrings();
     }
 }
        private static string OidOrHashCode(INakedObjectAdapter nakedObjectAdapter)
        {
            IOid oid = nakedObjectAdapter.Oid;

            if (oid == null)
            {
                return("" + nakedObjectAdapter.GetHashCode());
            }
            return(oid.ToString());
        }
        public void ConvertTransientToPersistentOid(IOid oid)
        {
            var entityOid = oid as EntityOid;

            if (entityOid != null)
            {
                entityOid.MakePersistent();
            }
            Log.DebugFormat("Converted transient OID to persistent {0}", oid);
        }
        public void CopyFrom(IOid oid)
        {
            var from = oid as EntityOid ?? throw new NakedObjectSystemException("Copy from Oid must be Entity Oid");

            Key         = from.Key;
            TypeName    = from.TypeName;
            EntityKey   = from.EntityKey;
            IsTransient = from.IsTransient;
            CacheState();
        }
Example #34
0
        public void SetATransientOid(IOid newOid)
        {
            if (Oid != null)
            {
                Log.InfoFormat("Overwriting oid {0} with {1}", Oid, newOid);
            }

            Assert.AssertTrue("New Oid must be transient", newOid.IsTransient);
            Oid = newOid;
        }
        public INakedObjectAdapter GetAdapterFor(IOid oid)
        {
            if (oid == null)
            {
                throw new NakedObjectSystemException("OID should not be null");
            }

            ProcessChangedOid(oid);
            return(identityAdapterMap.GetAdapter(oid));
        }
        public bool IsIdentityKnown(IOid oid)
        {
            if (oid == null)
            {
                throw new NakedObjectSystemException("OID should not be null");
            }

            ProcessChangedOid(oid);
            return(identityAdapterMap.IsIdentityKnown(oid));
        }
        public virtual void AddElement(string fieldId, IOid elementOid) {
            lock (fields) {
                if (!fields.ContainsKey(fieldId)) {
                    throw new InvalidDataException("Field " + fieldId + " not found  in dictionary");
                }

                var v = (IList<IOid>) fields[fieldId];
                v.Add(elementOid);
            }
        }
 public INakedObjectAdapter RecreateInstance(IOid oid, ITypeSpec spec) {
     INakedObjectAdapter adapter = nakedObjectManager.GetAdapterFor(oid);
     if (adapter != null) {
         if (!adapter.Spec.Equals(spec)) {
             throw new AdapterException(Log.LogAndReturn($"Mapped adapter is for a different type of object: {spec.FullName}; {adapter}"));
         }
         return adapter;
     }
     object obj = CreateObject(spec);
     return nakedObjectManager.AdapterForExistingObject(obj, oid);
 }
        public AggregateOid(IMetamodelManager metamodel, IOid oid, string id, string typeName) {
            Assert.AssertNotNull(metamodel);
            Assert.AssertNotNull(oid);
            Assert.AssertNotNull(id);
            Assert.AssertNotNull(typeName);

            this.metamodel = metamodel;
            parentOid = oid;
            fieldName = id;
            this.typeName = typeName;
        }
Example #40
0
        /// <summary>
        /// Gets the Oid from a DataRow. The DataRow is a row of the DataTable.
        /// </summary>
        /// <param name="dataTable">DataTable the Oid is given from.</param>
        /// <param name="row">Row the Oid is requested.</param>
        /// <param name="alternateKeyName">Name of the alternate key, if proceed.</param>
        /// <returns>Returns the Oid corresponding to the DataRow.</returns>
        public static Oid GetOid(DataTable dataTable, DataRow row, string alternateKeyName)
        {
            Oid resultOid = null;
            List <DataColumn> oidFields = GetOidColumns(dataTable);

            if (oidFields != null)
            {
                resultOid = Oid.Create(dataTable.TableName);
                resultOid.Fields.Clear();
                foreach (DataColumn lDataColumn in oidFields)
                {
                    ModelType type     = (ModelType)lDataColumn.ExtendedProperties[DataTableProperties.DataColumnProperties.ModelType];
                    object    rowValue = row[lDataColumn.Ordinal];
                    if (rowValue == null || rowValue == DBNull.Value)
                    {
                        return(null);
                    }

                    if (type == ModelType.String && rowValue.ToString().Trim().Length == 0)
                    {
                        return(null);
                    }

                    IOidField oidField = FieldList.CreateField(string.Empty, type);
                    oidField.Value = rowValue;
                    resultOid.Fields.Add(oidField);
                }

                string lAlternateKeyName = alternateKeyName;
                if (lAlternateKeyName == string.Empty)
                {
                    // If no alternateKeyName is specified, ask to the current oid.
                    lAlternateKeyName = resultOid.AlternateKeyName;
                }

                if (lAlternateKeyName != string.Empty)
                {
                    // Try to load the alternate key fields with the values contained in the row.
                    IOid auxAlternateKey = resultOid.GetAlternateKey(lAlternateKeyName);
                    if ((auxAlternateKey as AlternateKey) != null)
                    {
                        foreach (IOidField oidField in auxAlternateKey.Fields)
                        {
                            // It is not guaranteed if the alternate key field is in the datatable.
                            if (dataTable.Columns.Contains(oidField.Name))
                            {
                                oidField.Value = row[oidField.Name];
                            }
                        }
                    }
                }
            }
            return(resultOid);
        }
Example #41
0
        private INakedObject CreateAndInitNakedObject(ObjectData instanceData)
        {
            Log.DebugFormat("Instance data {0}", instanceData);

            IOid oid = instanceData.Oid;
            INakedObjectSpecification spec = SpecFor(instanceData);
            INakedObject instance          = PersistorUtils.RecreateInstance(oid, spec);

            InitObject(instance, instanceData);
            return(instance);
        }
        public void CopyFrom(IOid oid)
        {
            Assert.AssertTrue("Copy from Oid must be Entity Oid", oid is EntityOid);
            var from = (EntityOid)oid;

            Key         = from.Key;
            TypeName    = from.TypeName;
            EntityKey   = from.EntityKey;
            IsTransient = from.IsTransient;
            CacheState();
        }
 public INakedObjectAdapter CreateAdapter(object domainObject, IOid oid, IVersion version) {
     if (domainObject == null) {
         return null;
     }
     if (oid == null) {
         ITypeSpec objectSpec = metamodel.GetSpecification(domainObject.GetType());
         if (objectSpec.ContainsFacet(typeof (IComplexTypeFacet))) {
             return GetAdapterFor(domainObject);
         }
         if (objectSpec.HasNoIdentity) {
             return AdapterForNoIdentityObject(domainObject);
         }
         return AdapterForExistingObject(domainObject, objectSpec);
     }
     return AdapterForExistingObject(domainObject, oid);
 }
        public virtual Data LoadData(IOid oid) {
            XDocument doc = XDocument.Load(XmlFile.GetFile(Filename(oid)).FullName);

            CollectionData collection = null;
            ObjectData objectData = null;
            string fieldName = "";

            foreach (XElement element in doc.Descendants()) {
                LoadNode(element, ref collection, ref objectData, ref fieldName);
            }

            if (objectData != null) {
                return objectData;
            }
            if (collection != null) {
                return collection;
            }
            throw new FindObjectException("No data found for " + oid + " (possible missing file)");
        }
 public virtual void RegisterService(string name, IOid oid) {
     services.Add(new ServiceElement((SerialOid) oid, name));
     SaveServices();
 }
 private static INakedObject CreateAdapter(object domainObject, IOid oid, IVersion version) {
     return NakedObjectsContext.ObjectPersistor.CreateAdapter(domainObject, oid, version);
 }
 public static INakedObject CreateAdapter(IOid oid, object domainObject) {
     return CreateAdapter(domainObject, oid, null);
 }
 public virtual Data LoadData(IOid oid) {
     return null;
 }
 public virtual void Remove(IOid oid) {
     Log.DebugFormat("Remove {0}", oid);
     lock (adapters) {
         adapters.Remove(oid);
     }
 }
 public virtual ObjectData LoadObjectData(IOid oid) {
     return null;
 }
 public virtual CollectionData LoadCollectionData(IOid oid) {
     return null;
 }
 public virtual void RemoveElement(IOid elementOid) {
     elements.Remove(elementOid);
 }
 public virtual void AddElement(IOid elementOid) {
     elements.Add(elementOid);
 }
 public static INakedObject RecreateInstance(IOid oid, INakedObjectSpecification specification) {
     return NakedObjectsContext.ObjectPersistor.RecreateInstance(oid, specification);
 }
 public virtual void ConvertPersistentToTransientOid(IOid oid) {
     throw new UnexpectedCallException();
 }
Example #56
0
 public void SetupOid(IOid newOid) {
     oid = newOid;
 }
 public virtual void Remove(IOid oid) {}
 public virtual bool IsIdentityKnown(IOid oid) {
     lock (adapters) {
         return adapters.ContainsKey(oid);
     }
 }
Example #59
0
 public void SetATransientOid(IOid oid) {
     throw new NotImplementedException();
 }
 public CollectionData(INakedObjectSpecification type, IOid oid, IVersion version)
     : base(type, oid, version) {}