Class Sharing Across Controls And Pages - c#

This is basicly the design question, Like what would be the best way to structure my code within my web asp.net 4.0 web forms application. My General Design is like this...
Master Page
Has few User Controls
Content Pages
Inherit from Base Class
User Controls within Content Pages
I need to get some setting stuff from database in one CALL & store results into a Class. and then this class will be shared throughout the Pages (in Master Page, Base Class, Content Pages and user controls).
What would be the best place to initialize this class and easily access it throughout the page lifectycle.
Should I Hit Database & initialize the Class in BASE PAGE and then update Master Pages from within the BASE Class..like this
Page.master.findControl("label").Text = myClass.Setting1
and then Content Pages and its user control would need access to this class as well.
Please advise the best practice.

If this is completely shared data common for all users of your application, put it in a data container exposed as a static property of a class, and fill this container with data from database in the static constructor of this class.
public class SettingsData
{
public string Setting1;
public string Setting2;
}
public static class DataHolder
{
public static SettingsData Settings = new SettingsData();
static DataHolder()
{
// open connection to db and query for values
Settings.Setting1 = <value from db>;
Settings.Setting2 = <value from db>;
}
}
then in code
Label1.Text = DataHolder.Settings.Setting1;

Related

How can I pass data to/from a WebBrowser control?

I've got a basic "wrapper" WinForms app which has a few basic controls and a WebBrowser control (System.Windows.Forms.WebBrowser). This links to a web service which does all the actual "work" of my application.
A requirement has arisen to pass some basic data between the applications. Some of this can be achieved using the DocumentCompleted/Navigated events and using the URL property to see what page was loaded, and redirect the request elsewhere.
However, I'm struggling to work out how I can pass data that is not easily encompassed in the URL.
Is there anyway to set cookies in the request, or at least access cookies from the response?
You can pass and get data by calling methods from passed object as ObjectForScripting.
Try this-
[ComVisible(true)]
public class MyScriptingClass{
private string SomeData;
public string GetSomeData(){
return SomeData + " Something";
}
public void SetSomeData(string some){
SomeData = some;
}
}
And set ObjectForScripting property of your webBrowser control -
webBrowser.ObjectForScripting = new MyScriptingClass();
Now, in your javascript code call the methods like this -
var someVarFromJs = window.external.GetSomeData();
window.external.SetSomeData("Something to set");

How can I tell FindsBy to search elements on this particular sites

I've started learning Selenium Webdriver. It seems pretty easy but I'm not sure how to use PageObject Pattern. I understand the idea, I guess, but I don't know how to map many pages in one project. For example: the site contains many subsites (i.e. login page, create an account page) and I'd like to create PageObject for each of them. When I execute my script, I'm getting messages that elements haven't been found. What should I do? Should I create a separete drviers or what?
Seems that you are doing everything correct: create a new object for each page. Create different objects for common elements that are the same on a lot of pages (like footer, header menu, etc.).
Then to combine page objects, first you need to start somewhere. For example from you main page. So use the MainPageObj to interact wit the main page. As soon as you navigate to another page you need to use a method in your MainPageObj class.
This method must return a page object of the new page. So if you open a Login page from the main page then your method OpenLoginPage() should return LoginPageObj.
Example: MainPageObj and LoginPageObj are page object classes.
MainPageObj class has method:
public LoginPageObj OpenLoginPage()
{
LoginButton.Click();
return new LoginPageObj();
}
So use this method in this way:
MainPageObj mainPage = new MainPageObj ();
LoginPageObj loginPage = mainPage.OpenLoginPage();
Simple?)
If you want to make it more beautifyl then add static class Pages and add static methods for each page object you have to this class. Each method must return a new instance of your specific page object.
Example:
public static class RealSites
{
public static class Kinopoisk
{
public static Kinopoisk.MainPage MainPage
{
get { return new Kinopoisk.MainPage(); }
}
}
}
And in your tests use it this way:
Pages.RealSites.Kinopoisk.MainPage.OpenMoviePage(url);
Pages.RealSites.Kinopoisk.MoviePage.DoSomethigElseMethod();
You can refer this link to understand page object model and after this you can also create for your application
http://www.seleniumeasy.com/selenium-tutorials/simple-page-object-model-framework-example
If you want to learn then selenium easy is best site to learn for beginners.

How can I dynamically set connection string in MVC 5 based on incoming request?

I have an MVC 5 application that is currently single tenant only, but that is becoming unmanageable as our client-base grows.
I would like to turn this application into a multi-tenant app, accessing a distinct database for each client.
The client's are easily distinguishable by their domain, which I can access through Request.Url.Host or other methods, but once I have this property, I am having trouble setting it in the Business logic project within the context of that request only.
The Business logic project is referenced by the main Web project, but that project is blink to the Request object, so I do not know how to get that session information dynamically each time the baseDataAccess (which contains the connection strings) object is instantiated.
I've spent many many hours on this, and have tried using Ninject to resolve the dependency with no success. I just can't seem to figure out how to get the dynamic Request object or any of its properties to assign transiently to the baseDataAccess object.
The last thing I tried was using and IActionFilter using the OnActionExecuting method, but I am still unable to figure out how to set the value of the URL for each request dynamically in a project that has no context for the current request.
This is the class I want to set dynamically. the _connectionString is the dynamic property. I have methods to build the connection string based on the url, so if I can set the _domain property dynamically and transiently I'll be able to get the connection string.
public class baseDataAccess : IDataAccess
{
private static IClientConnection _clientConnection;
private static string _connectionString;
public static string _domain;
...
This is the last attempt I made, adding the OnActionExecuting method to each controller. How can I set these properties dynamically and transiently?
public void OnActionExecuting(ActionExecutedContext filterContext)
{
IClientConnection client = new ClientConnectionFactory(Request.Url.Host);
IDataAccess dataAccess = new baseDataAccess(client);
}
I'm have the same need for my application. I have to set the connection String with the user information depending on the user login. I added a reference on System.Web to get the Session object in my business layer.
The main idea is, to store the current user in the Session and let the business logic access this Session with HttpContext.Current.Session now my database classes can read the connection string dynamically on every single access to the database.
And as the user session is available in any library that has the System.Web reference, it can be used application wide. So there is no problem setting the connection string on the Session_Start event depending on the request from the user.

How to send debug text from C# class to ASP.NET page

I am working on a C# class that is a part of my ASP.Net Web Site.
Is there a simple way to output some log/debugging text to the top of the page. My class does NOT inherit from Page. I want to display variable values, etc.
The class represents an Exam object that I use in some of my aspx pages. The variables that I want to display are private and therefore inaccessible to my aspx pages.
Anywhere in the context of an http request you can reach the current executing page as follows.
Page page = HttpContext.Current.Handler as Page;
You are free to cast to your own page type. So you can write debug info to labels, textboxes etc.
you can just do this
HttpContext.Current.Response.Write("your message goes here");
or write a helper method
public static void writeOut(string message) {
HttpContext.Current.Response.Write(message);
}
You can try with Response.Write method
var pathOfYourLog = "";
var log = File.ReadAllLines(pathOfYourLog);
YourHttpContext.Response.Write(log);

Access session in class library.

I am developing an application architecture that uses 2 sub projects:
a) asp.net web application (it covers user interface and business logic) and
b) class library. (it covers data access layer)
After system user successfully logs in , the user information is stored in a session object.
The problem I am facing is when I try to access that session object in class library project(data access layer), it always returns null.
I need to access the session object in class library project because, in my case each user has their own username and password for database access(for security reasons);
So, How how do i read and write from/to session object in class library project
Use the System.Web.HttpContext.Current.Session object.
First of all, as Peri correctly noticed - you need to think again if having separate database logins for each user is a good idea - because you loose connection pooling (different users won't be able to reuse existing connections - and creating a new sql connection is quite expensive).
If you really wish to keep separate database users, I would create interface to abstract session from data access:
public interface ILoginDataService
{
LoginData Current { get; }
}
And implementation would pass login data from session. In such way you won't have session dependency to session in your data access logic - so it will be more testable, also you'll separate concerns.
Here is the code I used within a library to get session information.
public static string Entity()
{
string entity = "";
HttpContext httpContext = HttpContext.Current;
if (httpContext.ApplicationInstance.Session.Count > 0)
entity = httpContext.ApplicationInstance.Session["EntityCode"].ToString();
return entity;
}
I am having an ASP.Net application which uses session. I am able to access it in my app_code files using [WebMethod(EnableSession = true)] for the function. I am not sure whether this is your problem. I also faced session value as null when I removed (EnableSession = true) on the method.
using System.Web;
namespace ClassNameSpace
{
public class Class1 : IRequiresSessionState
{
private string sessionValue => HttpContext.Current.Session["sessionKey"].ToString();
}
}

Categories

Resources