Exemple #1
0
        /// <summary>
        /// Generic method to insert a new data in the table.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="input">Input object that will be inserted in the table.</param>
        /// <returns>Inserted object.</returns>
        public TOuput Add <TInput, TOuput>(TInput input)
        {
            var addobject = MappingConfiguration.MapObjects <TInput, TObject>(input);

            _context.Set <TObject>().Add(addobject);
            _context.SaveChanges();
            var result = MappingConfiguration.MapObjects <TObject, TOuput>(addobject);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Generic asychronous method for deleting an object in the database.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <param name="input">Input object that will be deleted in the table.</param>
        /// <returns>true or false</returns>
        public async Task <Boolean> DeleteAsync <TInput>(TInput input)
        {
            var entityObj = MappingConfiguration.MapObjects <TInput, TObject>(input);
            var entities  = _context.Set <TObject>().Attach(entityObj);

            _context.Entry(entities).State = EntityState.Deleted;
            await _context.SaveChangesAsync();

            return(true);
        }
Exemple #3
0
        /// <summary>
        /// Generic method to update an object in the database.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="input">Input object that will be updated in the table.</param>
        /// <returns>Updated Object</returns>
        public TOutPut Update <TOutPut, TInput>(TInput input)
        {
            var entityObj = MappingConfiguration.MapObjects <TInput, TObject>(input);
            var entities  = _context.Set <TObject>().Attach(entityObj);

            _context.Entry(entities).State = EntityState.Modified;
            _context.SaveChanges();
            var result = MappingConfiguration.MapObjects <TObject, TOutPut>(entityObj);

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Generic asynchronous method to insert a new data in the table.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="input">Input object that will be inserted in the table.</param>
        /// <returns>Inserted object.</returns>
        public async Task <TOuput> AddAsync <TInput, TOuput>(TInput input)
        {
            var addobject = MappingConfiguration.MapObjects <TInput, TObject>(input);

            _context.Set <TObject>().Add(addobject);
            await _context.SaveChangesAsync();

            var result = MappingConfiguration.MapObjects <TObject, TOuput>(addobject);

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Generic method for getting a single row in table based on its ID.
        /// </summary>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="id">Integer parameted for the ID</param>
        /// <returns>Output object</returns>
        public TOuput FindID <TOuput>(int id)
        {
            var entity = _context.Set <TObject>().Find(id);
            var result = MappingConfiguration.MapObjects <TObject, TOuput>(entity);

            if (entity != null)
            {
                _context.Entry(entity).State = EntityState.Detached;
            }
            return(result);
        }
Exemple #6
0
        /// <summary>
        /// Generic method for getting a single row in the table which corresponds with the query string.
        /// </summary>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="strQuery">A string object which contains the query condition for the search.</param>
        /// <param name="objValues">An array of object values which will be referenced during the search.</param>
        /// <returns>Output object</returns>
        public TOuput Get <TOuput>(String strQuery, object[] objValues)
        {
            var entities = _context.Set <TObject>().Where(strQuery, objValues).FirstOrDefault();
            var result   = MappingConfiguration.MapObjects <TObject, TOuput>(entities);

            if (entities != null)
            {
                _context.Entry(entities).State = EntityState.Detached;
            }
            return(result);
        }
Exemple #7
0
        /// <summary>
        /// Generic asynchronous method for getting a single row in table based on its ID.
        /// </summary>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="id">Integer parameted for the ID</param>
        /// <returns>Output object</returns>
        public async Task <TOuput> FindIDAsync <TOuput>(int id)
        {
            var entity = await _context.Set <TObject>().FindAsync(id);

            var result = MappingConfiguration.MapObjects <TObject, TOuput>(entity);

            if (entity != null)
            {
                _context.Entry(entity).State = EntityState.Detached;
            }
            return(result);
        }
Exemple #8
0
        /// <summary>
        /// Generic method for getting a single row in the table which will match the filter.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="filter">A lambda expression that will filter the list.</param>
        /// <returns>Output object</returns>
        public TOuput Get <TInput, TOuput>(Expression <Func <TObject, bool> > filter)
        {
            //var where = AutoMapperExtensions.GetMappedSelector<TInput, TObject>(filter);
            var entity = _context.Set <TObject>().FirstOrDefault(filter);
            var result = MappingConfiguration.MapObjects <TObject, TOuput>(entity);

            if (entity != null)
            {
                _context.Entry(entity).State = EntityState.Detached;
            }
            return(result);
        }
Exemple #9
0
        /// <summary>
        /// Generic method to update specific properties of an object in the database.
        /// </summary>
        /// <typeparam name="TInput">Object type of the input data.</typeparam>
        /// <typeparam name="TOuput">Object type of the output data.</typeparam>
        /// <param name="input">Input object that will be updated in the table.</param>
        /// <param name="properties">List of string which contains the property of string to update</param>
        /// <returns>Updated Object</returns>
        public TOutPut UpdateProperties <TOutPut, TInput>(TInput input, List <String> properties)
        {
            var entityObj = MappingConfiguration.MapObjects <TInput, TObject>(input);
            var entities  = _context.Set <TObject>().Attach(entityObj);

            foreach (var property in properties)
            {
                _context.Entry(entities).Property(property).IsModified = true;
            }
            _context.SaveChanges();
            var result = MappingConfiguration.MapObjects <TObject, TOutPut>(entityObj);

            return(result);
        }