|
Dave & Ista -
Thanks. That is exactly what I was looking for. Didn't even recognize the structs as the tool to use but, after reading on them, I believe that is exactly what I need. As for "Globals", you are right. "Constants" is the proper term. None of what I am referring are variables. The constants file will consist of the struct/classes, as discussed, enums and constants that are used through out the program but are not specific to a certain class.
Thanks.
|
|
|
|
|
My code is littered with DataSets and DataAdapters all over the place, with bound controls on various forms (and lots of duplication). I'm trying to create a more organized program by encapulating all the data access stuff in my own classes which will then handle their own data. So if I need, say a customer's name and account number, I can just say currCust.Name, currcust.AcctNum rather than having to go thru all the dataset stuff every place I need the info. I've been researching IBindingList but it looks like that is designed to work with DataGrids. Is that the right direction, though, or am I totally off-base? Is it even possible to do what I want or will I be forced to write all the data access in my classes rather than binding the controls?
|
|
|
|
|
There are many ways of performing this goal. This would fall into ORM (Object Relationship Mapping) which allows to you map data into an object that you can use throughout your program without the danger of the database changing. If this is something you can put off a little while longer, MS is supposed to release a form of ORM in their .NET 2.0. There are several ORM titles out there that you can purchase for the job.
I currently use several different layers of abstraction:
SAL – Server Abstraction Layer
This contains all the DataAdapters and raw access to a SQL server. Nothing server specific goes beyond this layer. This layer can be swapped out for a module that communicates via Web Services or other avenues of data maneuvering without effecting any other part of the program.
DAL – Data Abstraction Layer
This layer moves data. It uses the SAL layer to obtain data in datasets and provides data caching when required. No access to the SAL layer is available beyond this point.
BOL – Business Object Layer
I use these objects throughout the application. They communicate with the DAL layer and at their core is a DataSet which contains the actual data. At the cost of a little extra work, I make a DSD (Data Structure Definition) file that contains the actual field and table names in the DataSets. I access them by their members when I need to pull data from the datasets so that there is no direct reference to the fields inside the application. This means that if a change is made to the database, I would have to change to DLLs. The DataAdapters in the SAL would have to change and the field or table definition in the DSD DLL would change. The rest of the application would remain the same. It is usually best to have a single point of change but for my purposes, the DSD works fine.
To access data in a BOL DataItem derived class I simply use the global DSD such as:
public class DSD
{
public class DSDItem
{
private string dataBaseItemName=null;
private string itemName=null;
public string DataBaseItemName
{
get { return dataBaseItemName; }
}
public string ItemName
{
get { return itemName; }
}
public DSDItem(string dbItem, string Item)
{
dataBaseItemName=dbItem;
itemName=Item;
}
}
public class Books
{
public static DSDItem DataBaseTableName = new DSDItem("Books","Books");
public static DSDItem Book_ID = new DSDItem("Book_ID","Book_ID");
public static DSDItem BookTitle = new DSDItem("BookTitle","BookTitle");
public static DSDItem BookTitle = new DSDItem("BookAuthor","BookAuthor");
}
public class Stores
{
public static DSDItem DataBaseTableName = new DSDItem("Stores","Stores");
public static DSDItem Store_ID = new DSDItem("Store_ID","Store_ID");
public static DSDItem StoreName = new DSDItem("Name","Name");
}
public class Inventory
{
public static DSDItem DataBaseTableName = new DSDItem("Inventory","Inventory");
public static DSDItem Store_ID = new DSDItem("Store_ID","Store_ID");
public static DSDItem Book_ID = new DSDItem("Book_ID","Book_ID");
public static DSDItem Quantity = new DSDItem("Quantity","Quan");
}
}
DSD.Books.DataBaseTableName.ItemName - for access the table name
myDataRow[DSD.Books.Book_ID] – to access the actual fields to the dataset.
If the fields or table names change in the database I change the DSD DatabaseDataItem. Ther reason I have the DatabaseDataItem even included is that the DSD is also used in lower levels when querying the server. Thus, they all rely on the DSD.
If a person does not care about the other functionality, they can use a simple DSD type structure to access data in generic datasets and move all their queries into layer by themselves (similar to the SAL). Then throughout the application call that layer to move data in and out of datasets. When you want data, just use the DSD to access the fields or in data binding. I personally need much more functionality (such as caching and being able to use Web Services and even xml data sources) than that would provide along with having objects that contains the actual business logic.
They normally contain the business logic. Some times though you might want the business logic to be moved into a lower level, but for my purposes, it works well to have the business logic in these objects.
The BOL objects are derived from a DataManager object and a DataItem object.. The DataManagerItem object contains some handy routines to manipulate the data such as conversions to different types, null checking and stuff like that. The DataManager implements a IList and ITypedList interface. In my actual BOL objects, they will override the indexer ([]) and provide a type specific object.
Of course, there is more to it than mentioned, for example I have attributes on my DataItem fields to denote if they are bindable, read only, etc. The caching is provided on several levels so that I do not hit the server when I already have the data or when I want to maintain an array of data throughout the applications (such as State names) which will be used in popups or combos.
It took me the best part of a month (changed many times during that time ) to get mine all tied together and functioning with Caching as I required, but the end result allows the data to be modified or switched to difference sources without a lot of changes (if any) to the application.
The standard ORM's may be a better bet for most, but I am waiting to see what the new ORM in .NET 2.0 actually handles before going down that path. The method I used was strictly built out of need rather than a structured plan. I would rather had a more dynamic approach than fixed sets but time was limited.
Rocky Moore <><
|
|
|
|
|
Thank you very much for the detailed answer. Unfortunately, getting anything purchased around here is next to impossible so I'll have to wing it. Maybe on the next version of my software I'll be able to do something like this.
Thanks again.
|
|
|
|
|
For a ListView with MultiSelect = false how do I find the label of the selected Icon?
I’m hooking the SelectedIndexChanged event. And then it looks like I’m forced to iterate the SelectedItems collection with a foreach loop.
This seems stupid since only one item can be selected when MultiSelect = false.
I would like to do something like:
MyListView.Items(MyListView.SelectedIndex).Name
But there is no SelectedIndex property and does not look like I can use MyListView.Items in that way.
Do I have to iterate the SelectedItems collection for the single selected Item? How do you guys do this?
|
|
|
|
|
This gives access to the "Selected Index".
listView1.FocusedItem.SubItems[0]
|
|
|
|
|
Thanks Draco,
Not sure how I missed that one.
I added this to the click event:
listView1.FocusedItem.Text
|
|
|
|
|
albean wrote:
Not sure how I missed that one.
Because it's not really a standard thing in the framework. Usually it's SelectedIndex or SelectedItem. You could also use the SelectedIndices property...and then the first selected index would be listView1.SelectedIndices[0].
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
How can I write a Datatable to and XML file.
|
|
|
|
|
check out the intellisense its in black and white. Good luck with school
|
|
|
|
|
Hey all,
I'm wondering how I can get the version and build info from AssemblyInfo.cs to display in my program. I'm making an "About Box" and I'd like to be able to display this info on a label in the box. Can anyone help me out?
|
|
|
|
|
Application.ProductVersion
|
|
|
|
|
Is there a way to make it just show the major/minor version? Or only the build number?
|
|
|
|
|
Version ver = new Version(Application.ProductVersion);
string s = String.Format("v. {0:#}.{1:#}.{2:#}.{3:#}", ver.Major, ver.Minor, ver.Build, ver.Revision);
Check out the Version class in Help for more info.
|
|
|
|
|
I have CodeObjectList class in which I implmeneted IBindingList, ITypedList, IComponent, IListSource
When I do this
(ComboBox named cboCode)
(cList is the above class)
cboCode.DataSource = cList;
cboCode.DisplayMember = "Description";
it works fine
but
cboCode.DataBindings.Add("Text", cList, "Description");
the it just shows the first item as text and no drop down menu selections.
How come, how do I modify it to use the data bindings collection?
thanks
nick
|
|
|
|
|
is it possible to compile with VS. NET for the .NET 1.1 and with VS 2003 for .NET 1.0?
if it is possible, how can I use .NET 1.1 with VS.NET? or the upgrade is obligatory
|
|
|
|
|
Zibar wrote:
is it possible to compile with VS. NET for the .NET 1.1 and with VS 2003 for .NET 1.0?
No
Mazy
No sig. available now.
|
|
|
|
|
this is stupid... pushing technology forward is good but forcing people to do it is not.
|
|
|
|
|
Everyone,
I need some advice for a validating a textbox. I need to make sure the textbox is not empty with a custom validation form object. The custom validation is a requirement because I dont want to check the text box if it is not visible. Thanks in advance.
Courtney
|
|
|
|
|
Well, you could derive your own textbox and override the OnValidating method to perform validation...or you could have a normal textbox and subscribe to it's Validating event and perform validation that way.
Basically you have:
Way 1)
public class ValidTexbox : TextBox
{
protected override void OnValidating(CancelEventArgs e)
{
base.OnValidating (e);
if(this.Visible == true && this.Text.Length == 0)
{
e.Cancel = true;
MessageBox.Show("Input invalid");
}
}
}
Or
Way 2)
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.Validating += new System.ComponentModel.CancelEventHandlerthis.textBox1_Validating);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
if(textBox1.Visible == true && textBox1.Text.Length == 0)
{
MessageBox.Show("Input invalid");
}
}
}
Either way, you have to have the CausesValidation property of the textbox set to true. Then it'll work just fine.
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
hello!
I have a problem with the forms designer of VSNet 2003. I created a
user control that loads some pictures located in the working directory
(in my case, it's something like C:\myproj\sources\exe) - I'm using
the Directory.GetCurrentDirectory to get the full pathname. The
control constructor calls a method that loads the pictures and I
override the Paint method, it renders the pictures.
Now, if I add my control in the main form, the designer complains
about the files path : an exception is launched, it says the files
could not be found in C:\myproj\sources. I'd like to know :
- how to force the designer to look for files IN the working directory
or any directory. I looked around in VSnet but couldn't find any
dialog box with such parameters. The designer just doesn't want to
know about the working directory.
- if this solution is not possible, how to avoid the "execution" of
the control in the designer (ie. just a frame with the background
color, not all pictures, that's it...no call to the Paint method)
Thanks if you can point me to some tricks about that.
|
|
|
|
|
nobody had this problem ?
|
|
|
|
|
Using the form's Focused property.
For instance
if(this.Focused==true)
{
}
|
|
|
|
|
Have you tried using the Activated and Deactivate events along with a boolean variable.
|
|
|
|
|
But the definition for a top-most window is a form that overlaps all the other forms even if it is not the active one.
What do you want: The active window or the top-most window?
Inside C# you have the topmost property. Otherwise you can use WIN32 API
|
|
|
|