Click here to Skip to main content
16,013,489 members
Home / Discussions / C#
   

C#

 
GeneralRe: Umm.. Stupid Question Pin
Kannan Kalyanaraman21-Jul-03 20:08
Kannan Kalyanaraman21-Jul-03 20:08 
GeneralPrinting a DataGrid Pin
Mike Osbahr21-Jul-03 11:42
Mike Osbahr21-Jul-03 11:42 
GeneralRecord Types Pin
dbetting21-Jul-03 11:28
dbetting21-Jul-03 11:28 
GeneralRe: Record Types Pin
David Stone21-Jul-03 13:44
sitebuilderDavid Stone21-Jul-03 13:44 
GeneralRe: Record Types Pin
Ista21-Jul-03 17:10
Ista21-Jul-03 17:10 
GeneralRe: Record Types Pin
dbetting22-Jul-03 2:47
dbetting22-Jul-03 2:47 
GeneralEncapsulating Database in custom class Pin
sandman_max21-Jul-03 11:15
sandman_max21-Jul-03 11:15 
GeneralRe: Encapsulating Database in custom class Pin
Rocky Moore21-Jul-03 17:10
Rocky Moore21-Jul-03 17:10 
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 Wink | ;) ) 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 <><
GeneralRe: Encapsulating Database in custom class Pin
sandman_max22-Jul-03 4:43
sandman_max22-Jul-03 4:43 
GeneralListView getting the Selected Item Pin
albean21-Jul-03 9:36
albean21-Jul-03 9:36 
GeneralRe: ListView getting the Selected Item Pin
draco_iii21-Jul-03 9:39
draco_iii21-Jul-03 9:39 
GeneralRe: ListView getting the Selected Item Pin
albean21-Jul-03 11:05
albean21-Jul-03 11:05 
GeneralRe: ListView getting the Selected Item Pin
David Stone21-Jul-03 13:47
sitebuilderDavid Stone21-Jul-03 13:47 
GeneralDatatables and XML Pin
draco_iii21-Jul-03 9:32
draco_iii21-Jul-03 9:32 
GeneralRe: Datatables and XML Pin
Ista21-Jul-03 17:11
Ista21-Jul-03 17:11 
GeneralDisplaying Version and Build info in my program. Pin
frogb0x21-Jul-03 9:21
frogb0x21-Jul-03 9:21 
GeneralRe: Displaying Version and Build info in my program. Pin
Anonymous21-Jul-03 10:42
Anonymous21-Jul-03 10:42 
GeneralRe: Displaying Version and Build info in my program. Pin
frogb0x21-Jul-03 10:45
frogb0x21-Jul-03 10:45 
GeneralRe: Displaying Version and Build info in my program. Pin
sandman_max21-Jul-03 11:07
sandman_max21-Jul-03 11:07 
GeneralData source/Display memeber works but data binding doesnt Pin
Anonymous21-Jul-03 8:11
Anonymous21-Jul-03 8:11 
General.net sdk and visual studio compatability Pin
Zibar21-Jul-03 7:21
sussZibar21-Jul-03 7:21 
GeneralRe: .net sdk and visual studio compatability Pin
Mazdak21-Jul-03 9:45
Mazdak21-Jul-03 9:45 
GeneralRe: .net sdk and visual studio compatability Pin
Zibar22-Jul-03 1:05
sussZibar22-Jul-03 1:05 
GeneralCustom Validation For TextBox Pin
clayne21-Jul-03 7:19
clayne21-Jul-03 7:19 
GeneralRe: Custom Validation For TextBox Pin
David Stone21-Jul-03 14:11
sitebuilderDavid Stone21-Jul-03 14:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.