helper classes in C#

public static class Helper
    {
        public static string UploadFile(HttpPostedFileBase file,string targetDirectory="")
        {
            string systemPath = System.Web.Hosting.HostingEnvironment.MapPath("~"); 
            string directoryPath= "/UploadedFiles/"+ targetDirectory+"/" + DateTime.Now.Ticks + "_" + file.FileName;
            string fileName = systemPath + directoryPath;
            file.SaveAs(fileName);
            return directoryPath; 
        }

        public static string AsDateString(object dateTime)
        {
            if (dateTime!=null)
            {
                return Convert.ToDateTime(dateTime).ToString("dd-MM-yyyy");
            }
            return string.Empty;
        }
}
// Static list
public class Dropdowns
    {
        public static List<SelectListItem> Genders
        {
            get
            {
                List<SelectListItem> _lst = new List<SelectListItem>() {
                    new SelectListItem(){Value="Male",Text="Male"},
                    new SelectListItem(){ Value="Female",Text="Female"}
                    ,new SelectListItem(){ Value="Other",Text="Other"}
                };
                return _lst;
            }
        }
}
Generic Class
 public class VirtualCrud<T> : IDisposable where T : new()
    {
        bool disposed = false;
        SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
        protected HmsContext db = new HmsContext();
        public virtual List<T> SelectAll(out OutParamModel outparam)
        {
            var toReturn = new List<T>();
            outparam = new OutParamModel();
            return toReturn;
        }
        public virtual T SelectByID(int ID, out OutParamModel outparam)
        {
            var obj = new T();
            outparam = new OutParamModel();
            return obj;
        }
        public virtual T SelectByID(string ID, out OutParamModel outparam)
        {
            var obj = new T();
            outparam = new OutParamModel();
            return obj;
        }
        public virtual T Save(T entity, out OutParamModel outparam) 
        { 
            var obj = new T(); 
            outparam = new OutParamModel();
            return obj;
        }
        //public virtual T Save(T entity)
        //{
        //    var obj = new T();
        //    return obj;
        //}
        public virtual T Update(T entity, out OutParamModel outparam) 
        { 
            var obj = new T(); 
            outparam = new OutParamModel(); 
            return obj; 
        }
        public virtual T Delete(int ID, out OutParamModel outparam) 
        { 
            var obj = new T(); 
            outparam = new OutParamModel();
            return obj; 
        }
     
        //-- Database connection 
        public void OpenConnection()
        {
            var Connection = db.Database.Connection;
            if (Connection.State == System.Data.ConnectionState.Closed)
                Connection.Open();
            //return Connection;
        }
        public void CloseConnection()
        {
            var Connection = db.Database.Connection;
            if (Connection.State == System.Data.ConnectionState.Open)
                Connection.Close();
            //return Connection;
        }
        public DbConnection GetDbConnection
        {
            get
            {
                return db.Database.Connection;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        public virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;
            if (disposing)
            {
                handle.Dispose();
            }
            disposed = true;
        }

    }

Post a Comment

0 Comments