/// <summary> /// Instantiates the class using default components /// </summary> /// <param name="dbContext">Context in which to perform the merge operation</param> /// <param name="modelsNamespace">Namespace in which all context entities are defined (all models must be within /// the same namespace. If model type is in a different namespace, an exception will be thrown when instantiating /// the type. This is, hopefully, a temporary workaround, until I find a good solution for this)</param> /// <param name="modelAssemblyName">Name of assembly which contains all context enties. Again, just like with the /// namespace parameter, this is a temporary workaround</param> public DefaultEntityChangesMerger(DbContext dbContext, string modelsNamespace, string modelAssemblyName) { DbContext = dbContext; reflector = new DbContextReflector(DbContext, modelsNamespace, modelAssemblyName); scalarUpdater = new ScalarPropertyUpdater(DbContext, reflector); entityComparer = new EntityComparerByNonForeignKeys(); collectionMerger = new CollectionMerger(entityComparer); navUpdater = new NavigationPropertyUpdater(DbContext, scalarUpdater, reflector); }
// TODO: might add a method with lambda expression that selects model property public void UpdateCollectionProperty(IDbContextReflector reflector, EntityNavigationPropertyInfo property, TModel newModel, bool isNew, IEntityUpdater entityUpdater) { var genericArguments = property.PropertyInfo.PropertyType.GetGenericArguments(); var genericType = typeof(ICollectionPropertyUpdater <>).MakeGenericType(typeof(TModel)); GenericMethodInvoker.InvokeGenericMethod(genericType, nameof(ICollectionPropertyUpdater <TModel> .UpdateCollection), genericArguments, GenericMethodInvoker.DefaultPublicInstanceBindingFlags, new object[] { reflector, property, newModel, isNew, entityUpdater }, this.CollectionPropertyUpdater); }
/*public void UpdateCollection(Expression<Func<TModel, IList<TCollectionEntry>>> property, object sourceModel) * { * MemberExpression mex = (MemberExpression)property.Body; * UpdateCollection((PropertyInfo)mex.Member, sourceModel); * }*/ public void UpdateCollection <TCollectionEntry>(IDbContextReflector reflector, NavigationPropertyInfo property, object sourceModel, bool modelIsNew, IRecursiveEntityUpdater entityUpdater) where TCollectionEntry : class { Reflector = reflector; Property = property; EntityUpdater = entityUpdater; if (modelIsNew) { AddCollection((IList <TCollectionEntry>)property.PropertyInfo.GetValue(sourceModel)); } else { UpdateCollection((IList <TCollectionEntry>)property.PropertyInfo.GetValue(Model), (IList <TCollectionEntry>)property.PropertyInfo.GetValue(sourceModel)); } }
public EntityUpdater(DbContext dbContext, IDbContextReflector reflector, IEntityUpdater scalarPropertyUpdater, ICollectionMerger collectionMerger) : base(dbContext, scalarPropertyUpdater) { Reflector = reflector; CollectionMerger = collectionMerger; }
public ScalarPropertyUpdater(DbContext dbContext, IDbContextReflector reflector) { this.DbContext = dbContext; this.Reflector = reflector; }
public NavigationPropertyUpdater(DbContext dbContext, IEntityUpdater scalarEntityUpdater, IDbContextReflector reflector) : base(dbContext, scalarEntityUpdater) { Reflector = reflector; }
public void Init() { dbContext = new ReleaseContext(); reflector = new DbContextReflector(dbContext, "RecordLabel.TheContext", "TheContext"); }