[.net] C# Database access and (abstract) data management

Started by
4 comments, last by slack 17 years, 4 months ago
One of the things they decide not to go into during my SE classes at college is how to proceed when building Database Access Management layers for an application. Right now, I'm up to the point where I can start adding database access to my application, and actually have it do something useful with the database. However, I don't want to access the database from within my business layer code(*shudder*). My application is going to expand coming year a lot, perhaps even get a 2nd developer, so having raw queries in my code isn't going to make it more maintainable. Probably less. I have no idea how to proceed now. Does anyone know a few good sources of on how to design/build a DAM layer for my app? Toolmaker

Advertisement
The first thing I would look into is using the Data Access Application Block.

If that's still a bit general for you, I'd look into .netTiers, which uses CodeSmith to generate a fully featured database access layer. If you think you might buy the tool, just download the trial and give it a whirl ... otherwise, you can download the freeware version of codesmith ... and somewhere on the net, you can find a version of nettiers that works with the free version. I found it once, but couldn't find it again today.
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]
Quote:Original post by joelmartinez
The first thing I would look into is using the Data Access Application Block.

If that's still a bit general for you, I'd look into .netTiers, which uses CodeSmith to generate a fully featured database access layer. If you think you might buy the tool, just download the trial and give it a whirl ... otherwise, you can download the freeware version of codesmith ... and somewhere on the net, you can find a version of nettiers that works with the free version. I found it once, but couldn't find it again today.


rate += 12; for that.

I already found a similar system earlier today: .NET DataSets. It basically creates and updates all the DAL/DAM code for you. Probably what I'm looking for.

Right now I'm designing the database layout, and tomorrow I'm going to spend some time brushing up my SQL skills. I noticed a lot of it got lost over time, since I didn't use much of it(such as joins, foreign keys, etc.). I'll look up some books on the subject at a library, perhaps even bring a long my school books on SQL.

Toolmaker

I have experience with writing databases and their security application layers.

Since you are probably using a third party database like SQL, or MySQL or whatever, you can rest assured that the data access layer has been created for you to take advantage of. Just make usage of it during initialization when your user logs in. (If the user doesn't have a login, fake it somehow, so that each user session has a unique login, and then provide the same level of access for all requests that meet your fake user criterea.

If you are using your own database, or you want to code something like this up, then you can think of it in sort of an n-tier design.

Layer 1.) Everything calls this first -- check for access granted
Layer 2.) Call any base methods that need to do common tasks for your given database call
Layer 3.) Perform whatever database action is necessary
Layer 4.) Perform Bussiness logic

How might this look in real life....

public string user;public string password;//Create a wrapper API for each call that you typically make to the database//Api exposed to the outside (maybe a webservice, or in a library)public DataSet GetUserStatistics(){  DataSet result = null;  if (this.IsAuthenticated(this.user))  {    //Everything in these API methods is exactly the same execpt for the code in this part of the if block    result = backend.GetUserStatistics();    result = business.AdjustUserStatistics(result);  }  else  {    //Might want to throw an exception here.     //I don't like exceptions because they hurt performance a lot.  }  return result;}


There are better ways of formatting the code with Objects and inheritance, and a myriad of other techniques. So, choose what works best for you.

My advice is to just use a third party database, and let it take care of authentication for you if that is possible. It will be easier that way.

I like this approach because it is easy to quickly see what is going on. It also gives me the infinite amount of flexibility that I require.
The application I am developing at the moment I have in a few tiers, I know this is not your exact question but I will tell you anyway ;)

DataAccessLayer
BusinessLogicLayer
PresentationLayer

The thing I wanted to tell you about which might help is the communication between my DAL and my BLL.

In the BLL I have class declarations and private fields littered with runtime datamapping attributes, such as MapTable() and MapColumn().

In my BLL layer I make small calls to retrieve data.

cut a long story short, my BLL mainly contains POCOs (Plain Old CLR Objects) which have basic CRUD functionality as well as more specific logic, private fields (which are datamapped that my DAL layer understands) and public properties.

Basically it is a bit like NHibernate http://www.hibernate.org/343.html

Here is a quick example of a BLL class.

	[MapTable("Address", "_ID", "ID")]	public class Address : DataMappedObject 	{		[MapColumn("ID")]		private int _ID;		[MapColumn("AddressLine1")]		private string _Line1;		[MapColumn("AddressLine2")]		private string _Line2;		[MapColumn("AddressLine3")]		private string _Line3;		[MapColumn("City")]		private string _City;		[MapColumn("ProvinceID")]		private int _ProvinceID;		[MapComplex("ProvinceID", "Province")]		private Province _Province;		[MapColumn("PostCode")]		private string _PostCode;		[MapColumn("CountryID")]		private int _CountryID;		[MapComplex("CountryID", "Country")]		private Country _Country;		public Address()		{		}		public virtual int ID 		{			get			{				return _ID;			}			set			{				_ID = value;			}		}		public string Line1 		{			get			{				return _Line1;			}			set			{				_Line1 = value;			}		}		public string Line2 		{			get			{				return _Line2;			}			set			{				_Line2 = value;			}		}		public string Line3 		{			get			{				return _Line3;			}			set			{				_Line3 = value;			}		}		public string City 		{			get			{				return _City;			}			set			{				_City = value;			}		}		public int CountryID		{			get			{				return _CountryID;			}			set			{				_CountryID = value;			}		}		public Country Country 		{			get			{				if(_Country == null && _CountryID > 0)				{                    			_Country = Country.Find(_CountryID);				}				return _Country;			}		}		public string PostCode 		{			get			{				return _PostCode;			}			set			{				_PostCode = value;			}		}		public int ProvinceID		{			get			{				return _ProvinceID;			}			set			{				_ProvinceID = value;			}		}		public Province Province 		{			get			{				if(_Province == null && _ProvinceID > 0)				{					_Province = Province.Find(_ProvinceID);				}				return _Province;			}		}		public static Address Find(int id)		{			DalQuery q = new DalQuery(DalQueryType.Find, typeof(Address));			q.Criteria = "ID == {0}";			q.AddParameter(id);			return (Address) DalQueryAgent.Query(q);		}		public static List<Address> List()		{			DalQuery q = new DalQuery(DalQueryType.List, typeof(Address));            		return (List<Address>)DalQueryAgent.Query(q);		}	}


This can be taken a step further by generating a bulk of the code for basic CRUD by maybe using codesmith or a custom built code generator.
A wise man can learn more from a foolish question, than a fool can learn from a wise answer - Bruce Lee
I recently started using a nice framework called DevForce. It has a free express version. It's rediculously easy to do O/R mapping through a tool that integrates nicely into VS. Read through the documentation and go through the tutorial in the beginning of the Developer's Guide.

This topic is closed to new replies.

Advertisement