/// <summary> /// Creates a new Instance that is bound to <paramref name="type"/> and uses <paramref name="keyGenerator"/ /// > for generation of PrimaryKeys /// Must created inside an DatabaseScope /// </summary> /// <param name="keyGenerator">The Strategy to generate an uniqe PrimaryKey that matches the PrimaryKey Property</param> /// <param name="config">The Config store to use</param> /// <param name="useOrignalObjectInMemory"> /// If enabled the given object referance will be used (Top performance). /// if Disabled each object has to be define an Valid Ado.Net constructor to allow a copy (Can be slow) /// </param> public LocalDbRepository( DbConfig config, bool useOrignalObjectInMemory = false, ILocalDbPrimaryKeyConstraint keyGenerator = null) { Init(typeof(TEntity), keyGenerator, config, useOrignalObjectInMemory); }
/// <summary> /// Initializes a new instance of the <see cref="ConstraintCollection{TEntity}" /> class. /// </summary> /// <param name="localDbRepository">The local database repository.</param> /// <param name="primaryKey">The primary key.</param> internal ConstraintCollection( LocalDbRepository <TEntity> localDbRepository, ILocalDbPrimaryKeyConstraint primaryKey) { _localDbRepository = localDbRepository; PrimaryKey = primaryKey; Unique = new UniqueConstrains <TEntity>(localDbRepository); Default = new DefaultConstraints <TEntity>(localDbRepository); Check = new CheckConstraints <TEntity>(localDbRepository); }
/// <summary> /// Creates a new Instance that is bound to <paramref name="type"/> and uses <paramref name="keyGenerator"/ /// > for generation of PrimaryKeys /// Must created inside an DatabaseScope /// </summary> /// <param name="keyGenerator">The Strategy to generate an uniqe PrimaryKey that matches the PrimaryKey Property</param> /// <param name="containedType">The type that overwrites the Generic type. Must use object as Generic type arugment</param> /// <param name="config">The Config store to use</param> /// <param name="useOrignalObjectInMemory"> /// If enabled the given object referance will be used (Top performance). /// if Disabled each object has to be define an Valid Ado.Net constructor to allow a copy (Can be slow) /// </param> public LocalDbRepository( Type containedType, DbConfig config, bool useOrignalObjectInMemory = false, ILocalDbPrimaryKeyConstraint keyGenerator = null) { if (typeof(TEntity) != typeof(object)) { throw new InvalidOperationException("When using an contained type argument you must use object as generic type"); } Init(containedType, keyGenerator, config, useOrignalObjectInMemory); }
/// <summary> /// Internal Usage /// </summary> /// <param name="type"></param> /// <param name="keyGenerator"></param> /// <param name="config"></param> /// <param name="useOrignalObjectInMemory"></param> private void Init(Type type, ILocalDbPrimaryKeyConstraint keyGenerator, DbConfig config, bool useOrignalObjectInMemory) { if (config == null) { throw new ArgumentNullException("config"); } if (_config != null) { throw new InvalidOperationException("Multibe calls of Init are not supported"); } _keepOriginalObject = useOrignalObjectInMemory; _config = config; _databaseDatabase = LocalDbManager.Scope; if (_databaseDatabase == null) { throw new NotSupportedException("Please define a new DatabaseScope that allows to seperate" + " multibe tables of the same type in the same Application"); } _typeInfo = _config.GetOrCreateClassInfoCache(type); if (_typeInfo.PrimaryKeyProperty == null) { throw new NotSupportedException(string.Format("Entitys without any PrimaryKey are not supported. " + "Type: '{0}'", type.Name)); } TypeKeyInfo = _config.GetOrCreateClassInfoCache(_typeInfo.PrimaryKeyProperty.PropertyType); if (TypeKeyInfo == null) { throw new NotSupportedException(string.Format("Entitys without any PrimaryKey are not supported. " + "Type: '{0}'", type.Name)); } if (!TypeKeyInfo.Type.IsValueType) { throw new NotSupportedException(string.Format("Entitys without any PrimaryKey that is of " + "type of any value-type cannot be used. Type: '{0}'", type.Name)); } if (!_keepOriginalObject) { if (!TypeInfo.FullFactory) { TypeInfo.CreateFactory(config); if (!TypeInfo.FullFactory) { throw new NotSupportedException(string.Format("The given type did not provide a Full ado.net constructor " + "Type: '{0}'", TypeInfo)); } } } ILocalDbPrimaryKeyConstraint primaryKeyConstraint; if (keyGenerator != null) { primaryKeyConstraint = keyGenerator; } else { ILocalDbPrimaryKeyConstraint defaultKeyGen; if (LocalDbManager.DefaultPkProvider.TryGetValue(TypeKeyInfo.Type, out defaultKeyGen)) { primaryKeyConstraint = defaultKeyGen.Clone(); } else { throw new NotSupportedException( string.Format( "You must specify ether an Primary key that is of one of this types " + "({1}) " + "or invoke the ctor with an proper keyGenerator. " + "Type: '{0}'", type.Name, LocalDbManager .DefaultPkProvider .Keys .Select(f => f.Name) .Aggregate((e, f) => e + "," + f))); } } Constraints = new ConstraintCollection <TEntity>(this, primaryKeyConstraint); _triggers = new TriggerForTableCollection <TEntity>(this); _indexes = new IndexCollection <TEntity>(); Base = new ConcurrentDictionary <object, TEntity>(); _databaseDatabase.AddTable(this); _databaseDatabase.SetupDone += DatabaseDatabaseOnSetupDone; }