Beispiel #1
0
        public static void Insert(XpoDatabase database, IEnumerable <TDTOEntity> items, int savingFlags, Func <TDTOEntity, Exception[]> validateInsert, out TKey[] keyValues)
        {
            // Validate your input here !!!!
            if (validateInsert != null)
            {
                foreach (var item in items)
                {
                    Exception[] validationResult = validateInsert(item);
                    if (validationResult != null)
                    {
                        throw new Exception(String.Format("Insert Validation of {0} item failed:\n{1}", typeof(TDTOEntity).FullName, item.Key,
                                                          String.Join("\n", (from e in validationResult select e.Message))));
                    }
                }
            }

            TKey[] ids = null;

            using (UnitOfWork wrk = database.GetUnitOfWork())
            {
                wrk.ObjectsSaved += (sender, e) =>
                {
                    ids = (from n in e.Objects.OfType <TXPOEntity>()
                           select n.Key).ToArray();
                };
                foreach (var item in items)
                {
                    TXPOEntity dbItem = Activator.CreateInstance(typeof(TXPOEntity), wrk) as TXPOEntity;
                    dbItem.Assign(item, savingFlags);
                }
                wrk.CommitChanges();

                keyValues = ids;
            }
        }
Beispiel #2
0
        public static List <TDTOEntity> Select(XpoDatabase database, CriteriaOperator criteria = null, int pageSize = -1, int pageIndex = -1, params SortProperty[] sortProperties)
        {
            List <TDTOEntity> result = new List <TDTOEntity>();

            using (UnitOfWork wrk = database.GetUnitOfWork())
            {
                XPCollection <TXPOEntity> items = new XPCollection <TXPOEntity>(wrk, criteria, sortProperties);
                if (pageIndex > -1)
                {
                    if (pageSize > -1)
                    {
                        items.SkipReturnedObjects = pageSize * pageIndex;
                    }
                    items.TopReturnedObjects = pageSize;
                }
                foreach (TXPOEntity item in items)
                {
                    TDTOEntity dto = Activator.CreateInstance(typeof(TDTOEntity), (item as TXPOEntity)) as TDTOEntity;
                    if (dto != null)
                    {
                        result.Add(dto);
                    }
                }
            }

            return(result);
        }
Beispiel #3
0
 public void Insert(XpoDatabase database, TDTOEntity items, int savingFlags, Func <TDTOEntity, Exception[]> validateInsert, out TKey keyValue)
 {
     TKey[] result;
     Insert(database, new TDTOEntity[] { items }, savingFlags, validateInsert, out result);
     if ((result != null) && (result.Length > 0))
     {
         keyValue = result[0];
     }
     else
     {
         keyValue = default(TKey);
     }
 }
Beispiel #4
0
        public static IQueryable <TDTOEntity> Select(XpoDatabase database, Expression <Func <TXPOEntity, bool> > filter = null, int pageSize = -1, int pageIndex = -1, params SortProperty[] sortProperties)
        {
            IQueryable <TDTOEntity> result;

            using (UnitOfWork wrk = database.GetUnitOfWork())
            {
                var tmp = wrk.Query <TXPOEntity>().Where(filter);
                if ((pageIndex > -1) && (pageSize > 0))
                {
                    tmp = tmp.Skip(pageSize * pageIndex).Take(pageSize);
                }
                result = tmp.Select((xpo, i) => Activator.CreateInstance(typeof(TDTOEntity), (xpo as TXPOEntity)) as TDTOEntity);
            }

            return(result);
        }
Beispiel #5
0
        public static TDTOEntity Select(XpoDatabase database, CriteriaOperator criteria, int loadingFlags = 0, bool raiseExceptionNotFound = false)
        {
            TDTOEntity result = null;

            using (UnitOfWork wrk = database.GetUnitOfWork())
            {
                TXPOEntity item = wrk.FindObject <TXPOEntity>(criteria);
                if ((item == null) && raiseExceptionNotFound)
                {
                    throw new Exception(String.Format("{0} Item not found on '{1}' not found", typeof(TXPOEntity).FullName, criteria.ToString()));
                }

                result = Activator.CreateInstance(typeof(TDTOEntity), (item as TXPOEntity), loadingFlags) as TDTOEntity;
            }
            return(result);
        }
Beispiel #6
0
 public static void Update(XpoDatabase database, IEnumerable <TDTOEntity> items, int savingFlags, Func <TDTOEntity, Exception[]> validateUpdate,
                           bool insertOnNotFound = false, bool raiseExceptionOnNotFound = false)
 {
     // Validate your input here !!!!
     if (validateUpdate != null)
     {
         foreach (var item in items)
         {
             Exception[] validationResult = validateUpdate(item);
             if (validationResult != null)
             {
                 throw new Exception(String.Format("Update Validation of {0} item({1}) failed:\n{2}", typeof(TDTOEntity).FullName, item.Key,
                                                   String.Join("\n", (from e in validationResult select e.Message))));
             }
         }
     }
     using (UnitOfWork wrk = database.GetUnitOfWork())
     {
         foreach (var item in items)
         {
             TXPOEntity dbItem = wrk.GetObjectByKey <TXPOEntity>(item.Key);
             if (dbItem == null)
             {
                 if (insertOnNotFound)
                 {
                     dbItem = Activator.CreateInstance(typeof(TXPOEntity), wrk) as TXPOEntity;
                 }
                 else if (raiseExceptionOnNotFound)
                 {
                     throw new Exception(String.Format("{0} Item with Id = '{1}' not found for updating", typeof(TXPOEntity).FullName, item.Key));
                 }
             }
             if (dbItem != null)
             {
                 dbItem.Assign(item, savingFlags);
             }
         }
         wrk.CommitChanges();
     }
 }
Beispiel #7
0
        public static void Delete(XpoDatabase database, IEnumerable <TDTOEntity> items, Func <TDTOEntity, Exception[]> validateDelete)
        {
            // Validate your input here !!!!
            if (validateDelete != null)
            {
                foreach (var item in items)
                {
                    Exception[] validationResult = validateDelete(item);
                    if (validationResult != null)
                    {
                        throw new Exception(String.Format("Delete Validation of {0} item({1}) failed:\n{2}", typeof(TDTOEntity).FullName, item.Key,
                                                          String.Join("\n", (from e in validationResult select e.Message))));
                    }
                }
            }

            using (UnitOfWork wrk = database.GetUnitOfWork())
            {
                wrk.Delete(wrk.GetObjectsByKey(wrk.GetClassInfo <TXPOEntity>(), (from o in items select o.Key).ToArray(), false));
                wrk.CommitChanges();
            }
        }
 public XPRoleStore(XpoDatabase database) :
     base(database)
 {
 }
Beispiel #9
0
 public XPPagedDataStore(XpoDatabase db,
                         IXPDataMapper <TKey, TModel, TXPOClass> mapper,
                         IXPDataStoreValidator <TKey, TModel, TXPOClass> validator)
     : base(db, mapper, validator)
 {
 }
Beispiel #10
0
        private static void AddStores <TKey, TUser, TXPOUser, TRole, TXPORole>(IServiceCollection services, string connectionName,
                                                                               XPDataMapper <TKey, TUser, TXPOUser> userMapper, XPDataMapper <TKey, TRole, TXPORole> roleMapper,
                                                                               XPUserStoreValidator <TKey, TUser, TXPOUser> userValidator,
                                                                               XPRoleStoreValidator <TKey, TRole, TXPORole> roleValidator,
                                                                               Type userType, Type roleType,
                                                                               Type xpoUserType, Type xpoUserLoginType, Type xpoUserClaimType, Type xpoUserTokenType,
                                                                               Type xpoRoleType, Type xpoRoleClaimType)
            where TKey : IEquatable <TKey>
            where TUser : class, IXPUser <TKey>, new()
            where TXPOUser : XPBaseObject, IXPUser <TKey>
            where TRole : class, IXPRole <TKey>, new()
            where TXPORole : XPBaseObject, IXPRole <TKey>
        {
            // no roles is not supported
            if (userType == null)
            {
                throw new ArgumentNullException("userType");
            }

            if (roleType == null)
            {
                throw new ArgumentNullException("roleType");
            }

            if (xpoUserType == null)
            {
                throw new ArgumentNullException("xpoUserType");
            }
            if (xpoUserLoginType == null)
            {
                throw new ArgumentNullException("xpoUserLoginType");
            }
            if (xpoUserClaimType == null)
            {
                throw new ArgumentNullException("xpoUserClaimType");
            }
            if (xpoUserTokenType == null)
            {
                throw new ArgumentNullException("xpoUserTokenType");
            }

            var identityUserType = FindGenericBaseType(userType, typeof(XPIdentityUser <, , ,>));

            if (identityUserType == null)
            {
                throw new InvalidOperationException(Resources.NotIdentityUser);
            }

            var keyType          = identityUserType.GenericTypeArguments[0];
            var identityRoleType = FindGenericBaseType(roleType, typeof(XPIdentityRole <>));

            if (identityRoleType == null)
            {
                throw new InvalidOperationException(Resources.NotIdentityRole);
            }
            var xpoRoleClaimTpe = xpoRoleClaimType ?? identityRoleType.GenericTypeArguments[2];

            Type userStoreType = typeof(XPUserStore <, , , , , ,>).MakeGenericType(
                keyType, userType, xpoUserType, xpoRoleType, xpoUserLoginType, xpoUserClaimType, xpoUserTokenType);
            Type roleStoreType = typeof(XPRoleStore <, , ,>).MakeGenericType(
                keyType, roleType, xpoRoleType, xpoRoleClaimTpe);

            Type defaultUserMapperType = typeof(XPUserMapper <, ,>).MakeGenericType(keyType, userType, xpoUserType);
            Type defaultRoleMapperType = typeof(XPRoleMapper <, ,>).MakeGenericType(keyType, roleType, xpoRoleType);

            //XPUserMapper<identityUserType, >

            services.TryAddScoped(typeof(IUserStore <>).MakeGenericType(userType /*, xpoUserType*/),
                                  (sp) =>
            {
                if (string.IsNullOrEmpty(connectionName))
                {
                    var db = sp.GetRequiredService(typeof(XpoDatabase)) as XpoDatabase;
                    if (db == null)
                    {
                        throw new NullReferenceException("XpoDatabase service could not return an instance for IUserStore<>");
                    }
                    return(Activator.CreateInstance(userStoreType,
                                                    db,
                                                    userMapper ?? Activator.CreateInstance(defaultUserMapperType),
                                                    userValidator));
                }
                else
                {
                    IConfiguration cfg = sp.GetRequiredService <IConfiguration>();
                    XpoDatabase db     = sp.GetRequiredService <XpoDatabase>();
                    if (db == null || db.DataLayerName != connectionName)
                    {
                        db = new XpoDatabase((o) => {
                            o.ConnectionString = cfg.GetConnectionString(connectionName);
                            o.Name             = connectionName;
                        });
                    }

                    return(Activator.CreateInstance(userStoreType,
                                                    db,
                                                    userMapper ?? Activator.CreateInstance(defaultUserMapperType),
                                                    userValidator));
                }
            });
            services.TryAddScoped(typeof(IRoleStore <>).MakeGenericType(roleType /*, xpoRoleType*/),
                                  (sp) =>
            {
                if (string.IsNullOrEmpty(connectionName))
                {
                    var db = sp.GetRequiredService(typeof(XpoDatabase)) as XpoDatabase;
                    if (db == null)
                    {
                        throw new NullReferenceException("XpoDatabase service could not return an instance for IUserStore<>");
                    }
                    return(Activator.CreateInstance(roleStoreType, db, roleMapper ?? Activator.CreateInstance(defaultRoleMapperType), roleValidator));
                }
                else
                {
                    IConfiguration cfg = sp.GetRequiredService <IConfiguration>();
                    XpoDatabase db     = sp.GetRequiredService <XpoDatabase>();
                    if (db == null || db.DataLayerName != connectionName)
                    {
                        db = new XpoDatabase((o) => {
                            o.ConnectionString = cfg.GetConnectionString(connectionName);
                            o.Name             = connectionName;
                        });
                    }
                    return(Activator.CreateInstance(roleStoreType,
                                                    db,
                                                    roleMapper ?? Activator.CreateInstance(defaultRoleMapperType),
                                                    roleValidator));
                }
            });
        }
Beispiel #11
0
 public XpoDtoDatasource(XpoDatabase database)
     : this()
 {
     _XpoDatabase = database;
 }
Beispiel #12
0
 public XPRoleStore(XpoDatabase db) : base(db)
 {
 }
Beispiel #13
0
 public void Delete(XpoDatabase database, TDTOEntity item, Func <TDTOEntity, Exception[]> validateDelete)
 {
     Delete(database, new TDTOEntity[] { item }, validateDelete);
 }
Beispiel #14
0
 public void Update(XpoDatabase database, TDTOEntity item, int savingFlags, Func <TDTOEntity, Exception[]> validateUpdate,
                    bool insertOnNotFound = false, bool raiseExceptionOnNotFound = false)
 {
     Update(database, new TDTOEntity[] { item }, savingFlags, validateUpdate, insertOnNotFound, raiseExceptionOnNotFound);
 }
Beispiel #15
0
 public static TDTOEntity Select(XpoDatabase database, TKey keyValue, int loadingFlags = 0, bool raiseExceptionNotFound = false)
 {
     return(Select(database, CriteriaOperator.Parse("[Id]==?", keyValue), loadingFlags, raiseExceptionNotFound));
 }
Beispiel #16
0
        //public XPRoleStore(string connectionName, XPDataMapper<TKey, TRole, TXPORole> mapper, XPDataValidator<TKey, TRole, TXPORole> validator)
        //	: this(new XpoDatabase(connectionName), mapper, validator)
        //{

        //}
        public XPRoleStore(XpoDatabase db, XPDataMapper <TKey, TRole, TXPORole> mapper, XPDataValidator <TKey, TRole, TXPORole> validator)
            : base(db, mapper, validator)
        {
        }
Beispiel #17
0
 public XPRoleStore(XpoDatabase db) : base(db, new XPRoleStoreValidator <TKey, TRole, TXPORole>())
 {
 }
Beispiel #18
0
 public XpoDtoDatasource(string connectionStringName)
     : this()
 {
     _XpoDatabase = new XpoDatabase(connectionStringName);
 }
Beispiel #19
0
 public XPRoleStore(XpoDatabase db, XPDataValidator <string, TRole, TXPORole> validator) : base(db, validator)
 {
 }
Beispiel #20
0
 public XPUserStore(XpoDatabase database) :
     base(database)
 {
 }