//pass 0 = get all properties (for ToObject overload) , pass 1 = get only primitive props, pass 2 = get only ref props public virtual void ToObject(MarshalObject marshalObject, ref object targetObject, int pass, RefreshBehaviorType refreshBehavior) { IClassMap classMap = Context.DomainMap.MustGetClassMap(targetObject.GetType()); IPropertyMap propertyMap; //PropertyStatus propStatus; if (pass == 0 || pass == 1) { foreach (MarshalProperty mp in marshalObject.Properties) { //propStatus = ctx.GetPropertyStatus(targetObject, mp.Name); propertyMap = classMap.MustGetPropertyMap(mp.Name); ToProperty(targetObject, mp, propertyMap, refreshBehavior); } } if (pass == 0 || pass == 2) { foreach (MarshalReference mr in marshalObject.References) { //propStatus = ctx.GetPropertyStatus(targetObject, mp.Name); propertyMap = classMap.MustGetPropertyMap(mr.Name); ToReference(targetObject, mr, propertyMap, refreshBehavior); } } if (targetObject != null) { if (pass == 0 || pass == 1) { this.Context.IdentityMap.RegisterLoadedObject(targetObject); } } }
public void ValidateProperty(object obj, string propertyName, IList exceptions) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); DoValidateProperty(obj, propertyMap, exceptions); }
public virtual TimeToLiveBehavior GetTimeToLiveBehavior(object obj, string propertyName) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); return(GetTimeToLiveBehavior(propertyMap)); }
public virtual ValidationMode GetValidationMode(object obj, string propertyName) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); return(GetValidationMode(classMap, propertyMap)); }
public void ExpandWildcards(NPathSelectQuery query) { ArrayList newSelectFieldList = new ArrayList(); foreach (NPathSelectField field in query.Select.SelectFields) { string fieldName = field.Alias; NPathIdentifier path = field.Expression as NPathIdentifier; if (path != null && path.IsWildcard) { string[] parts = path.Path.Split('.'); NPathClassName className = (NPathClassName)query.From.Classes[0]; IClassMap classMap = Context.DomainMap.MustGetClassMap(className.Name); int i = 0; foreach (string part in parts) { if (i == parts.Length - 1) { break; } IPropertyMap property = classMap.MustGetPropertyMap(part); classMap = Context.DomainMap.MustGetClassMap(property.DataType); i++; } ArrayList properties = classMap.GetAllPropertyMaps(); foreach (PropertyMap property in properties) { if (property.ReferenceType != ReferenceType.None) { continue; } NPathSelectField newField = new NPathSelectField(); newField.Alias = null; NPathIdentifier newPath = new NPathIdentifier(); if (parts.Length > 1) { newPath.Path = string.Join(".", parts, 0, parts.Length - 1) + "."; } newPath.Path += property.Name; newField.Expression = newPath; newSelectFieldList.Add(newField); } } else { newSelectFieldList.Add(field); } } query.Select.SelectFields = newSelectFieldList; }
public virtual IDataSource GetDataSource(object obj, string propertyName) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); ISourceMap sourceMap = propertyMap.GetSourceMap(); string name = sourceMap.Name; return((IDataSource)m_hashDataSources[name]); }
public virtual void LoadProperty(object obj, string propertyName) { this.Context.LogManager.Info(this, "Loading property", "Property: " + propertyName + ", Object Type: " + obj.GetType().ToString()); // do not localize IClassMap classMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); if (propertyMap.DocPropertyMapMode == DocPropertyMapMode.Default || propertyMap.DocPropertyMapMode == DocPropertyMapMode.Inline) { LoadObject(ref obj); } }
public ArrayList GetPathPropertyMaps(string propertyPath, ref string special) { ArrayList propertyMaps = new ArrayList(); IPropertyMap propertyMap = null; IClassMap classMap = sqlEmitter.RootClassMap; bool skip = false; bool first = true; int cnt = 1; IList pathPropertyNames = GetPathPropertyNames(propertyPath); foreach (string name in pathPropertyNames) { if (name.Length > 0) { skip = false; if (first) { if (name.ToLower(CultureInfo.InvariantCulture) == sqlEmitter.RootClassMap.Name.ToLower()) { skip = true; } } first = false; if (!(skip)) { if (!(name.Equals("*"))) { propertyMap = classMap.MustGetPropertyMap(name); propertyMaps.Add(propertyMap); if (cnt < pathPropertyNames.Count) { classMap = propertyMap.MustGetReferencedClassMap(); } } else { //HACK: hmmm * => ID ?? special = name; //special - we trade for the first id property propertyMap = (IPropertyMap)classMap.GetIdentityPropertyMaps()[0]; propertyMaps.Add(propertyMap); } } } cnt++; } return(propertyMaps); }
public void MapPropertyToColumn(string className, string propertyName, string tableName, string columnName) { ILoggingService loggingService = engine.GetService <ILoggingService>(); if (loggingService != null) { loggingService.LogInfo(this, String.Format("Mapping property {0}.{1} to column {2}.{3} in NPersist mapping file.", className, propertyName, tableName, columnName)); } IClassMap classMap = GetDomainMap().MustGetClassMap(className); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); propertyMap.Column = columnName; }
public void SetPropertyMetaData(string className, string propertyName, PropertyMetaData propertyMetaData, object value) { IClassMap classMap = GetDomainMap().MustGetClassMap(className); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); switch (propertyMetaData) { case PropertyMetaData.Identity: SetIdentity(propertyMap, (bool)value); break; case PropertyMetaData.Nullable: SetNullable(propertyMap, (bool)value); break; case PropertyMetaData.SourceAssigned: SetSourceAssigned(propertyMap, (bool)value); break; default: throw new Exception("Unknown property meta data type " + propertyMetaData.ToString()); } }
public void Resolve(ConflictResolution resolution) { IClassMap classMap = this.Context.DomainMap.MustGetClassMap(this.obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(this.propertyName); if (propertyMap.IsCollection) { ResolveList(resolution); } else { switch (resolution) { case ConflictResolution.UseCachedValue: UseCachedValue(); break; case ConflictResolution.UseFreshValue: UseFreshValue(); break; } } this.Context.UnclonedConflicts.Remove(this); }
//[DebuggerStepThrough()] protected virtual void DoNotifyPropertySet(object obj, string propertyName, ref object value, object oldValue, bool hasOldValue, ref bool cancel) { IContext ctx = this.Context; IObjectManager om = ctx.ObjectManager; IPersistenceEngine pe = ctx.PersistenceEngine; PropertyCancelEventArgs e; if (hasOldValue) { e = new PropertyCancelEventArgs(obj, propertyName, value, oldValue, this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } else { e = new PropertyCancelEventArgs(obj, propertyName, value, this.Context.ObjectManager.GetPropertyValue(obj, propertyName), this.Context.ObjectManager.GetNullValueStatus(obj, propertyName)); } this.Context.EventManager.OnWritingProperty(this, e); if (e.Cancel) { cancel = true; return; } value = e.NewValue; IClassMap classMap = ctx.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap; string prevId; string newId; propertyMap = classMap.MustGetPropertyMap(propertyName); if (propertyMap.ReferenceType != ReferenceType.None && value != null) { if (propertyMap.ReferenceType == ReferenceType.OneToMany || propertyMap.ReferenceType == ReferenceType.OneToOne) { //parent object IInterceptable ivalue = value as IInterceptable; if (ivalue == null) { throw new NPersistException(string.Format("Object is not a NPersist managed object, do not use 'new' on Entities. (Property='{0}', Owner={1})", propertyName, obj)); } else { if (ivalue.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("Object does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } ObjectStatus valueObjectStatus = om.GetObjectStatus(value); if (valueObjectStatus == ObjectStatus.UpForDeletion || valueObjectStatus == ObjectStatus.Deleted) { throw new DeletedObjectException(string.Format("Object has been deleted. (Object={0})", value), value); } } } else if (propertyMap.ReferenceType == ReferenceType.ManyToOne || propertyMap.ReferenceType == ReferenceType.ManyToMany) { IInterceptableList ivalue = value as IInterceptableList; if (ivalue == null) { throw new NPersistException(string.Format("List is not a NPersist managed list, do not use 'new' to initialize lists, NPersist does this for you. (Property='{0}', Owner={1})", propertyName, obj)); } else if (ivalue.Interceptable.GetInterceptor().Context != this.Context) { throw new NPersistException(string.Format("List does not belong to the same context object as the property owner. (Property='{0}', Owner={1})", propertyName, obj)); } } } if (propertyMap.IsReadOnly) { //Let read-only inverse properties through if (!(propertyMap.ReferenceType != ReferenceType.None && propertyMap.Inverse.Length > 0 && propertyMap.NoInverseManagement == false)) { //Special - if someone forgot to make their ManyOne read-only, //why bug them about it? (so don't add an "else" with an exception...) if (propertyMap.ReferenceType != ReferenceType.ManyToOne) { throw new ReadOnlyException("Property '" + classMap.Name + "." + propertyName + "' is read-only!"); // do not localize } } } PropertyStatus propStatus = PropertyStatus.Clean; ObjectStatus objStatus = om.GetObjectStatus(obj); bool hasPropertyStatus = false; if (objStatus == ObjectStatus.Deleted) { throw new DeletedObjectException("The object has been deleted!", obj, propertyName); // do not localize } else if (objStatus == ObjectStatus.UpForDeletion) { throw new DeletedObjectException("The object has been registered as up for deletion!", obj, propertyName); // do not localize } this.Context.ObjectCloner.EnsureIsClonedIfEditing(obj); if (objStatus == ObjectStatus.UpForCreation) { } else if (objStatus == ObjectStatus.Clean) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.NotLoaded) { propertyMap = this.Context.DomainMap.MustGetClassMap(obj.GetType()).MustGetPropertyMap(propertyName); if (!(propertyMap.IsIdentity)) { propStatus = this.Context.ObjectManager.GetPropertyStatus(obj, propertyName); hasPropertyStatus = true; //it would be sweet to be able to determine beforehand if this property would be part of the span //that is loaded with LoadObject and only call LoadObject if that is the case.... if (propStatus == PropertyStatus.NotLoaded) { hasPropertyStatus = false; //this.Context.PersistenceEngine.LoadObject(ref obj); this.Context.IdentityMap.LoadObject(ref obj, true); if (obj == null) { throw new ObjectNotFoundException("Object not found!"); // do not localize } } if (!hasPropertyStatus) { propStatus = om.GetPropertyStatus(obj, propertyName); } if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (!(hasOldValue)) { if (!(om.ComparePropertyValues(obj, propertyName, value, om.GetPropertyValue(obj, propertyName)))) { this.Context.UnitOfWork.RegisterDirty(obj); } } } else if (objStatus == ObjectStatus.Dirty) { propStatus = om.GetPropertyStatus(obj, propertyName); if (propStatus == PropertyStatus.NotLoaded) { pe.LoadProperty(obj, propertyName); } } if (propertyMap.IsIdentity) { prevId = om.GetObjectIdentity(obj); newId = om.GetObjectIdentity(obj, propertyMap, value); if (prevId != newId) { ctx.IdentityMap.UpdateIdentity(obj, prevId, newId); } } om.SetNullValueStatus(obj, propertyName, false); om.SetUpdatedStatus(obj, propertyName, true); if (hasOldValue) { ctx.InverseManager.NotifyPropertySet(obj, propertyName, value, oldValue); ctx.UnitOfWork.RegisterDirty(obj); } else { ctx.InverseManager.NotifyPropertySet(obj, propertyName, value); } }
public object LoadProperty(object obj, string propertyName, string domainKey) { if (useCompression && this.compressor != null) { obj = this.compressor.Decompress((string)obj); //propertyName = this.compressor.Decompress(propertyName); //domainKey = this.compressor.Decompress(domainKey); } IContext ctx = contextFactory.GetContext(domainKey); IObjectManager om = ctx.ObjectManager; IMarshalingTransformer transformer = new MarshalingTransformer(ctx); MarshalReference mr = (MarshalReference)formater.Deserialize(obj, typeof(MarshalReference)); string identity = transformer.GetIdentity(mr, mr.Value); IClassMap classMap = ctx.DomainMap.MustGetClassMap(mr.Type); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); Type realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap); //object realObj = ctx.GetObjectById(identity, realType); object realObj = ctx.GetObjectById(identity, realType, true); ctx.LoadProperty(realObj, propertyName); object serialized = null; if (propertyMap.IsCollection) { if (propertyMap.ReferenceType == ReferenceType.None) { // IList list = (IList) om.GetPropertyValue(realObj, propertyName); // MarshalList ml = transformer.FromList(list) ; // serialized = formater.Serialize(ml); } else { //TODO: fix transformer.FromReferenceList //Even better: Add MarshalProperty.Object, OriginalObject, List, OriginalList, ReferenceList and OriginalReferenceList! IList list = (IList)om.GetPropertyValue(realObj, propertyName); MarshalObjectList mol = transformer.FromObjectList(list); serialized = formater.Serialize(mol); } } else { if (propertyMap.ReferenceType == ReferenceType.None) { MarshalProperty mp = transformer.FromProperty(obj, propertyMap); serialized = formater.Serialize(mp); } else { object value = om.GetPropertyValue(realObj, propertyName); if (value != null) { ObjectStatus objectStatus = ctx.GetObjectStatus(value); if (objectStatus == ObjectStatus.NotLoaded) { ctx.PersistenceEngine.LoadObject(ref value); } if (value != null) { MarshalObject mo = transformer.FromObject(value); serialized = formater.Serialize(mo); } } } } if (serialized == null) { serialized = ""; } ctx.Dispose(); if (useCompression && this.compressor != null) { return(this.compressor.Compress((string)serialized)); } else { return(serialized); } }
public override void LoadProperty(object obj, string propertyName) { if (this.url.Length < 1) { throw new NPersistException("You must specify an url to your NPersist Web Service in your WebServiceRemotingEngine!"); } RemotingService rs = new RemotingService(this.Context, url); IMarshalingTransformer transformer = new MarshalingTransformer(Context); IObjectManager om = Context.ObjectManager; IClassMap classMap = Context.DomainMap.MustGetClassMap(obj.GetType()); IPropertyMap propertyMap = classMap.MustGetPropertyMap(propertyName); MarshalReference mr = transformer.FromObjectAsReference(obj); string xmlObject = (string)Formatter.Serialize(mr); if (useCompression && this.compressor != null) { xmlObject = this.compressor.Compress(xmlObject); } bool doUseCompression = this.useCompression; if (this.compressor == null) { doUseCompression = false; } string result = rs.LoadProperty(xmlObject, propertyName, this.domainKey, doUseCompression); if (useCompression && this.compressor != null) { result = this.compressor.Decompress(result); } if (propertyMap.IsCollection) { if (propertyMap.ReferenceType == ReferenceType.None) { } else { //Refresh issues!!! (the objects in the list being deserialized may exist in the cache!!) MarshalObjectList mol = (MarshalObjectList)Formatter.Deserialize(result, typeof(MarshalObjectList)); IList freshList = transformer.ToObjectList(mol, RefreshBehaviorType.DefaultBehavior, new ArrayList()); //Context.IdentityMap.RegisterLoadedObject(obj); IList orgList = (IList)om.GetPropertyValue(obj, propertyName); LoadReferenceList(freshList, orgList, om, obj, propertyMap); this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, orgList); } } else { if (propertyMap.ReferenceType == ReferenceType.None) { MarshalProperty mp = (MarshalProperty)Formatter.Deserialize(result, typeof(MarshalProperty)); transformer.ToProperty(obj, mp, propertyMap, RefreshBehaviorType.DefaultBehavior); } else { if (result.Length > 0) { MarshalObject mo = (MarshalObject)Formatter.Deserialize(result, typeof(MarshalObject)); string identity = transformer.GetIdentity(mo); IClassMap refClassMap = Context.DomainMap.MustGetClassMap(mo.Type); Type refType = Context.AssemblyManager.MustGetTypeFromClassMap(refClassMap); object refObject = Context.GetObjectById(identity, refType, true); if (om.GetObjectStatus(refObject) == ObjectStatus.NotLoaded) { transformer.ToObject(mo, ref refObject); } // object refObject = Context.TryGetObjectById(identity, refType, true); // if (refObject == null) // { // refObject = Context.GetObjectById(identity, refType, true); // transformer.ToObject(mo, ref refObject); // } om.SetPropertyValue(obj, propertyName, refObject); om.SetOriginalPropertyValue(obj, propertyName, refObject); om.SetNullValueStatus(obj, propertyName, false); this.Context.InverseManager.NotifyPropertyLoad(obj, propertyMap, refObject); } else { om.SetPropertyValue(obj, propertyName, null); om.SetOriginalPropertyValue(obj, propertyName, null); om.SetNullValueStatus(obj, propertyName, true); } } } }