public virtual void Deleted(IDataAccessObjectAdvanced value)
        {
            if (value.IsDeleted)
            {
                return;
            }

            if ((value.ObjectState & DataAccessObjectState.Untracked) == DataAccessObjectState.Untracked)
            {
                return;
            }

            var typeHandle = Type.GetTypeHandle(value);

            IObjectsByIdCache cache;

            if (!this.cachesByType.TryGetValue(typeHandle, out cache))
            {
                cache = CreateCacheForDao(value, this);

                this.cachesByType[typeHandle] = cache;
            }

            cache.Deleted((DataAccessObject)value);
        }
Exemplo n.º 2
0
        public virtual void Deleted(IDataAccessObjectAdvanced value)
        {
            if (value.IsDeleted)
            {
                return;
            }

            var keyType = value.KeyType;

            if (keyType == null && value.NumberOfPrimaryKeys > 1)
            {
                keyType = value.CompositeKeyTypes[0];
            }

            switch (Type.GetTypeCode(keyType))
            {
            case TypeCode.Int32:
                if (cacheByInt == null)
                {
                    cacheByInt = new ObjectsByIdCache <int>(this);
                }
                cacheByInt.Deleted((DataAccessObject <int>)value);
                break;

            case TypeCode.Int64:
                if (cacheByLong == null)
                {
                    cacheByLong = new ObjectsByIdCache <long>(this);
                }
                cacheByLong.Deleted((DataAccessObject <long>)value);
                break;

            case TypeCode.String:
                if (keyType == typeof(string))
                {
                    if (cacheByString == null)
                    {
                        cacheByString = new ObjectsByIdCache <string>(this);
                    }
                    cacheByString.Deleted((DataAccessObject <string>)value);
                }
                break;

            default:
                if (keyType == typeof(Guid))
                {
                    if (cacheByGuid == null)
                    {
                        cacheByGuid = new ObjectsByIdCache <Guid>(this);
                    }
                    cacheByGuid.Deleted((DataAccessObject <Guid>)value);
                }
                break;
            }
        }
Exemplo n.º 3
0
        public RelatedDataAccessObjects(IDataAccessObjectAdvanced relatedDataAccessObject, DataAccessModel dataAccessModel, EntityRelationshipType relationshipType, string propertyName)
            : base(dataAccessModel, null)
        {
            this.PropertyName            = propertyName;
            this.RelatedDataAccessObject = relatedDataAccessObject;
            this.RelationshipType        = relationshipType;
            this.ExtraCondition          = this.GetExtraCondition();
            this.PersistenceQueryProvider.RelatedDataAccessObjectContext = this;

            this.BuildInitializeRelatedMethod();
        }
        public RelatedDataAccessObjects(DataAccessModel dataAccessModel, IDataAccessObjectAdvanced parentDataAccessObject, string parentPropertyName)
            : base(dataAccessModel)
        {
            this.RelatedDataAccessObject = parentDataAccessObject;

            var parentType = this.DataAccessModel.TypeDescriptorProvider.GetTypeDescriptor(this.DataAccessModel.GetDefinitionTypeFromConcreteType(parentDataAccessObject.GetType()));

            this.relationshipInfo = parentType.GetRelationshipInfos().Single(c => c.ReferencingProperty.PropertyName == parentPropertyName);

            this.Condition = CreateJoinCondition(this.relationshipInfo.TargetProperty);
            this.InitializeDataAccessObject = GetInitializeRelatedMethod(parentType, this.relationshipInfo.TargetProperty);
        }
        private static IObjectsByIdCache CreateCacheForDao(IDataAccessObjectAdvanced dao, DataAccessObjectDataContext context)
        {
            Func <DataAccessObjectDataContext, IObjectsByIdCache> func;
            var typeHandle = Type.GetTypeHandle(dao);

            if (!cacheConstructor.TryGetValue(typeHandle, out func))
            {
                var type = dao.GetType();

                var keyType     = dao.NumberOfPrimaryKeys > 1 ? typeof(CompositePrimaryKey) : dao.KeyType;
                var cacheType   = typeof(ObjectsByIdCache <>).MakeGenericType(keyType);
                var constructor = cacheType.GetConstructors().Single();

                Delegate   getIdFunc;
                Expression keyComparer;
                var        getIdFuncType = typeof(Func <,>).MakeGenericType(typeof(DataAccessObject), keyType);

                if (keyType != typeof(CompositePrimaryKey))
                {
                    keyComparer = Expression.Constant(null, typeof(IEqualityComparer <>).MakeGenericType(dao.KeyType ?? dao.CompositeKeyTypes[0]));

                    var param  = Expression.Parameter(typeof(DataAccessObject));
                    var lambda = Expression.Lambda(dao.TypeDescriptor.GetSinglePrimaryKeyExpression(Expression.Convert(param, type)), param);

                    getIdFunc = lambda.Compile();
                }
                else
                {
                    keyComparer = Expression.Constant(CompositePrimaryKeyComparer.Default);
                    getIdFunc   = Delegate.CreateDelegate(getIdFuncType, TypeUtils.GetMethod(() => GetDataAccessObjectCompositeId(default(DataAccessObject))));
                }

                var contextParam = Expression.Parameter(typeof(DataAccessObjectDataContext));

                func = Expression.Lambda <Func <DataAccessObjectDataContext, IObjectsByIdCache> >(Expression.New(constructor, Expression.Constant(dao.GetType()), contextParam, Expression.Constant(getIdFunc, getIdFunc.GetType()), keyComparer), contextParam).Compile();

                cacheConstructor = new Dictionary <RuntimeTypeHandle, Func <DataAccessObjectDataContext, IObjectsByIdCache> >(cacheConstructor)
                {
                    [typeHandle] = func
                };
            }

            return(func(context));
        }
Exemplo n.º 6
0
        public virtual DataAccessObject CreateDataAccessObject <K>(Type type, K primaryKey, PrimaryKeyType primaryKeyType)
        {
            if (!typeof(IDataAccessObjectAdvanced).IsAssignableFrom(type) ||
                !typeof(DataAccessObject <>).IsAssignableFromIgnoreGenericParameters(type))
            {
                throw new ArgumentException("Type must be a DataAccessObjectType", nameof(type));
            }

            var objectPropertyAndValues = GetObjectPropertyValues(type, primaryKey, primaryKeyType);

            if (objectPropertyAndValues.Any(keyValue => keyValue.Value == null))
            {
                throw new MissingOrInvalidPrimaryKeyException();
            }

            var existing = GetCurrentDataContext(false)?.GetObject(GetConcreteTypeFromDefinitionType(type), objectPropertyAndValues);

            if (existing != null)
            {
                IDataAccessObjectAdvanced obj = null;

                ActionUtils.IgnoreExceptions(() => obj = GetReference(type, primaryKey, primaryKeyType));

                throw new ObjectAlreadyExistsException(obj, null, "CreateDataAccessObject");
            }
            else
            {
                var retval = this.RuntimeDataAccessModelInfo.CreateDataAccessObject(type, this, true);

                retval.ToObjectInternal()
                .SetPrimaryKeys(objectPropertyAndValues)
                .FinishedInitializing()
                .SubmitToCache();

                ((IDataAccessModelInternal)this).OnHookCreate(retval);

                return(retval);
            }
        }
 public ObjectAlreadyExistsException(IDataAccessObjectAdvanced obj, Exception innerException, string relatedQuery)
     : base(innerException, relatedQuery)
 {
     this.Object = obj;
 }
		public DeletedDataAccessObjectException(IDataAccessObjectAdvanced dataAccessObject)
			: base(dataAccessObject)
		{	
		}
		public MissingPropertyValueException(IDataAccessObjectAdvanced relatedObject, Exception innerException, string relatedQuery)
			: base(innerException, relatedQuery)
		{
			this.RelatedObject = relatedObject;
		}
		public MissingRelatedDataAccessObjectException(IDataAccessObjectAdvanced missingObject)
			: this(missingObject, null, null, null)
		{
		}
		public MissingRelatedDataAccessObjectException(IDataAccessObjectAdvanced missingObject, IDataAccessObjectAdvanced parentObject, Exception innerException, string relatedQuery)
			: base(missingObject, innerException, relatedQuery)
		{
			this.ParentObject = parentObject;
		}
 public MissingDataAccessObjectException(IDataAccessObjectAdvanced missingObject, Exception innerException, string relatedQuery)
     : base(innerException, relatedQuery)
 {
     this.MissingObject = missingObject;
 }
 public WriteOnlyDataAccessObjectException(IDataAccessObjectAdvanced dataAccessObject)
 {
     this.DataAccessObject = dataAccessObject;
 }
 public MissingRelatedDataAccessObjectException(IDataAccessObjectAdvanced missingObject, IDataAccessObjectAdvanced parentObject, Exception innerException, string relatedQuery)
     : base(missingObject, innerException, relatedQuery)
 {
     this.ParentObject = parentObject;
 }
Exemplo n.º 15
0
 public MissingPropertyValueException(IDataAccessObjectAdvanced relatedObject, Exception innerException, string relatedQuery)
     : base(innerException, relatedQuery)
 {
     this.RelatedObject = relatedObject;
 }
 public MissingObjectIdentifierException(IDataAccessObjectAdvanced dataAccessObject)
 {
     this.DataAccessObject = dataAccessObject;
 }
 public DeletedDataAccessObjectException(IDataAccessObjectAdvanced dataAccessObject)
     : base(dataAccessObject)
 {
 }
		public ObjectAlreadyExistsException(IDataAccessObjectAdvanced obj, Exception innerException, string relatedQuery)
			: base(innerException, relatedQuery)
		{
			this.Object = obj;
		}
		public MissingObjectIdentifierException(IDataAccessObjectAdvanced dataAccessObject)
		{
			this.DataAccessObject = dataAccessObject;
		}
Exemplo n.º 20
0
 public MissingDataAccessObjectException(IDataAccessObjectAdvanced missingObject)
     : this(missingObject, null, null)
 {
 }
Exemplo n.º 21
0
 public WriteOnlyDataAccessObjectException(IDataAccessObjectAdvanced dataAccessObject)
 {
     this.DataAccessObject = dataAccessObject;
 }
Exemplo n.º 22
0
 public MissingDataAccessObjectException(IDataAccessObjectAdvanced missingObject, Exception innerException, string relatedQuery)
     : base(innerException, relatedQuery)
 {
     this.MissingObject = missingObject;
 }