Esempio n. 1
0
        public static dynamic GetQueryableOfDerivedType(this EntityObjectStore.LocalContext context, Type type)
        {
            Type       mostBaseType = context.GetMostBaseType(type);
            MethodInfo mi           = typeof(ObjectContextUtils).GetMethod("GetObjectSetOfType").MakeGenericMethod(type, mostBaseType);

            return(InvokeUtils.InvokeStatic(mi, new object[] { context }));
        }
Esempio n. 2
0
        public static IQueryable <TDerived> GetObjectSetOfType <TDerived, TBase>(this EntityObjectStore.LocalContext context) where TDerived : TBase
        {
            MethodInfo mi = context.WrappedObjectContext.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes).MakeGenericMethod(typeof(TBase));
            var        os = (IQueryable <TBase>)InvokeUtils.Invoke(mi, context.WrappedObjectContext, null);

            ((ObjectQuery)os).MergeOption = context.DefaultMergeOption;
            return(os.OfType <TDerived>());
        }
Esempio n. 3
0
        public static object CreateObject(this EntityObjectStore.LocalContext context, Type type)
        {
            object objectSet = context.GetObjectSet(type);
            var    methods   = objectSet.GetType().GetMethods();
            var    mi        = methods.Single(m => m.Name == "CreateObject" && m.IsGenericMethod).MakeGenericMethod(type);

            return(InvokeUtils.Invoke(mi, objectSet, null));
        }
Esempio n. 4
0
        public static dynamic GetObjectSet(this EntityObjectStore.LocalContext context, Type type)
        {
            Type       mostBaseType = context.GetMostBaseType(type);
            MethodInfo mi           = context.WrappedObjectContext.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes).MakeGenericMethod(mostBaseType);
            dynamic    os           = mi.Invoke(context.WrappedObjectContext, null);

            os.MergeOption = context.DefaultMergeOption;
            return(os);
        }
Esempio n. 5
0
        public static ObjectQuery GetObjectSet(this EntityObjectStore.LocalContext context, Type type)
        {
            var mostBaseType = context.GetMostBaseType(type);
            var mi           = context.WrappedObjectContext.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes).MakeGenericMethod(mostBaseType);
            var os           = (ObjectQuery)mi.Invoke(context.WrappedObjectContext, null);

            os.MergeOption = context.DefaultMergeOption;
            return(os);
        }
Esempio n. 6
0
        private static PropertyInfo[] SafeGetMembers(this EntityObjectStore.LocalContext context, Type type, Func <EntityType, IEnumerable <EdmMember> > getMembers)
        {
            EntityType et = GetEntityType(context, type);

            if (et != null)
            {
                return(type.GetProperties().Join(getMembers(et), pi => pi.Name, em => em.Name, (pi, em) => pi).ToArray());
            }
            return(new PropertyInfo[] {});
        }
Esempio n. 7
0
        public static PropertyInfo[] GetComplexMembers(this EntityObjectStore.LocalContext context, Type type)
        {
            StructuralType st = context.GetStructuralType(type);

            if (st != null)
            {
                IEnumerable <EdmMember> cm = st.Members.Where(m => m.TypeUsage.EdmType is ComplexType);
                return(type.GetProperties().Join(cm, pi => pi.Name, em => em.Name, (pi, em) => pi).ToArray());
            }
            return(new PropertyInfo[] {});
        }
Esempio n. 8
0
        public static object GetNextKey(this EntityObjectStore.LocalContext context, Type type)
        {
            PropertyInfo idMember = context.GetIdMembers(type).Single();
            string       query    = string.Format("max(it.{0}) + 1", idMember.Name);

            dynamic os = context.GetObjectSet(type);
            ObjectQuery <DbDataRecord> results = os.Select(query);
            DbDataRecord result = results.Single();

            return(GetNextKey(type, result.IsDBNull(0) ? 1 : (int)result[0]));
        }
Esempio n. 9
0
        public static bool IdMembersAreIdentity(this EntityObjectStore.LocalContext context, Type type)
        {
            EntityType et = GetEntityType(context, type);

            if (et != null)
            {
                MetadataProperty[] mp = et.KeyMembers.SelectMany(m => m.MetadataProperties).Where(p => p.Name.Contains("StoreGeneratedPattern")).ToArray();
                return(mp.Any() && mp.All(p => p.Value.Equals("Identity")));
            }
            return(false);
        }
Esempio n. 10
0
        public static object ProxyObject(this EntityObjectStore.LocalContext context, object objectToProxy)
        {
            if (TypeUtils.IsProxy(objectToProxy.GetType()))
            {
                return(objectToProxy);
            }

            object newObject = context.GetObjectSet(objectToProxy.GetType()).CreateObject();

            PropertyInfo[] idMembers = context.GetIdMembers(objectToProxy.GetType());

            idMembers.ForEach(pi => newObject.GetType().GetProperty(pi.Name).SetValue(newObject, pi.GetValue(objectToProxy, null), null));

            return(context.GetObjectSet(objectToProxy.GetType()).ApplyCurrentValues((dynamic)newObject));
        }
Esempio n. 11
0
 public static bool CanCreateObjectSet(this EntityObjectStore.LocalContext context, Type type)
 {
     try {
         MethodInfo mi = context.WrappedObjectContext.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes).MakeGenericMethod(type);
         mi.Invoke(context.WrappedObjectContext, null);
         return(true);
     }
     catch (Exception) {
         // expected (but ugly)
         if (EntityObjectStore.RequireExplicitAssociationOfTypes)
         {
             string msg = $"{type} is not explicitly associated with any DbContext, but 'RequireExplicitAssociationOfTypes' has been set on the PersistorInstaller";
             throw new InitialisationException(Log.LogAndReturn(msg));
         }
     }
     return(false);
 }
Esempio n. 12
0
        public static dynamic CreateQuery(this EntityObjectStore.LocalContext context, Type type, string queryString, params ObjectParameter[] parameters)
        {
            Type       mostBaseType = context.GetMostBaseType(type);
            MethodInfo mi           = context.WrappedObjectContext.GetType().GetMethod("CreateQuery").MakeGenericMethod(mostBaseType);
            var        parms        = new List <object> {
                queryString, new ObjectParameter[] {}
            };

            dynamic os = mi.Invoke(context.WrappedObjectContext, parms.ToArray());

            if (type != mostBaseType)
            {
                dynamic ot = os.GetType().GetMethod("OfType").MakeGenericMethod(type);
                os = ot.Invoke(os, null);
            }

            return(os);
        }
Esempio n. 13
0
        public static bool ContextKnowsType(this EntityObjectStore.LocalContext context, Type type)
        {
            // problem is that OSpace is not populated until an object set is created.
            // and there seems to be no way of navigating to the OSpace type from the CSpace.
            // for the moment then workaround by attempting to create an object set.

            // For complex types this will only work if the parent is queried first
            if (context.WrappedObjectContext.IsTypeInOSpace(type))
            {
                return(true);
            }

            if (context.CanCreateObjectSet(type))
            {
                return(true);
            }
            return(false);
        }
Esempio n. 14
0
 public static bool CanCreateObjectSet(this EntityObjectStore.LocalContext context, Type type)
 {
     try {
         MethodInfo mi = context.WrappedObjectContext.GetType().GetMethod("CreateObjectSet", Type.EmptyTypes).MakeGenericMethod(type);
         mi.Invoke(context.WrappedObjectContext, null);
         return(true);
     }
     catch (Exception e) {
         // expected (but ugly)
         Log.DebugFormat("Context {0} did not recognise type {1} and threw {2}", context.Name, type.FullName, e.Message);
         if (EntityObjectStore.RequireExplicitAssociationOfTypes)
         {
             string msg = string.Format("{0} is not explicitly associated with any DbContext, but 'RequireExplicitAssociationOfTypes' has been set on the PersistorInstaller", type);
             throw new InitialisationException(msg);
         }
     }
     return(false);
 }
Esempio n. 15
0
 public static PropertyInfo[] GetNavigationMembers(this EntityObjectStore.LocalContext context, Type type) => context.SafeGetMembers(type, et => et.NavigationProperties);
Esempio n. 16
0
 private static EntityType GetEntityType(this EntityObjectStore.LocalContext context, Type type)
 {
     return(context.GetStructuralType(type) as EntityType);
 }
Esempio n. 17
0
 public static object[] GetKey(this EntityObjectStore.LocalContext context, INakedObject nakedObject)
 {
     return(context.GetIdMembers(nakedObject.GetDomainObject().GetEntityProxiedType()).Select(x => x.GetValue(nakedObject.GetDomainObject(), null)).ToArray());
 }
Esempio n. 18
0
 public static PropertyInfo[] GetIdMembers(this EntityObjectStore.LocalContext context, Type type) => context.SafeGetMembers(type, et => et.KeyMembers);
Esempio n. 19
0
 public static object GetQueryableOfDerivedType <T>(this EntityObjectStore.LocalContext context) => context.GetQueryableOfDerivedType(typeof(T));
Esempio n. 20
0
 public static dynamic GetQueryableOfDerivedType <T>(this EntityObjectStore.LocalContext context)
 {
     return(context.GetQueryableOfDerivedType(typeof(T)));
 }
Esempio n. 21
0
 public static object[] GetKey(this EntityObjectStore.LocalContext context, object domainObject) => context.GetIdMembers(domainObject.GetEntityProxiedType()).Select(x => x.GetValue(domainObject, null)).ToArray();
Esempio n. 22
0
        // problem is that OSpace is not populated until an object set is created.
        // and there seems to be no way of navigating to the OSpace type from the CSpace.
        // for the moment then workaround by attempting to create an object set.

        // For complex types this will only work if the parent is queried first
        public static bool ContextKnowsType(this EntityObjectStore.LocalContext context, Type type) =>
        context.WrappedObjectContext.IsTypeInOSpace(type) || context.CanCreateObjectSet(type);
Esempio n. 23
0
 public static PropertyInfo[] GetReferenceMembers(this EntityObjectStore.LocalContext context, Type type) => context.GetNavigationMembers(type).Where(x => !CollectionUtils.IsCollection(x.PropertyType)).ToArray();
Esempio n. 24
0
 public static PropertyInfo[] GetNonIdMembers(this EntityObjectStore.LocalContext context, Type type)
 {
     return(context.GetMembers(type).Where(x => !context.GetIdMembers(type).Contains(x)).ToArray());
 }
Esempio n. 25
0
 public static PropertyInfo[] GetAllMembers(this EntityObjectStore.LocalContext context, Type type)
 {
     return(context.GetMembers(type).Union(context.GetNavigationMembers(type)).ToArray());
 }
Esempio n. 26
0
 public static PropertyInfo[] GetCollectionMembers(this EntityObjectStore.LocalContext context, Type type)
 {
     return(context.GetNavigationMembers(type).Where(x => CollectionUtils.IsCollection(x.PropertyType)).ToArray());
 }
Esempio n. 27
0
 public static PropertyInfo[] GetMembers(this EntityObjectStore.LocalContext context, Type type)
 {
     return(context.SafeGetMembers(type, et => et.Properties));
 }