|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionJohnKenedy Library is a .NET 3.5 library. This library is an
alternative approach to create n-tiers database application with ADO.NET and
minimize the dependency of data access created by .NET. The file consist of two projects -
JohnKenedy.DataAccess
(Main Library) -
JohnKenedy.DataAccessModule
(Windows Application) Main Features
- Automatically create T-SQL Command
created for all tables - T-SQL Command for tables that
contain Identity column can be adjusted to suit the them and easily retrieve
the identity value generated by database - T-SQL Command for tables that
contain not nullable column can be adjusted so that when programmer does not
fill a value for not nullable column, library can automatically fill a default
value for those columns - T-SQL Command for tables that
contain string, Library will truncate string value if it exceeds the maximum
character length - T-SQL Command for tables that
contain fix string (char/varchar) Library will adjust value length to the
length required.
ConceptsJohnKenedy.DataAccessModule can generate entity classes
that are used directly from user interface for data operations or can be
wrapped in Business Objects created by user. JohnKenedy.DataAccess is the main
library called by entity classes or can be called directly from Business
Object, all crucial operations done in JohnKenedy.DataAccess are logged as
audit trail and stored in Database. The audit process hides from programmer.
BackgroundI often felt that, I have rebuilt the same database model again and again and writing codes for it again and again, that's why, I think there should be some way that we can actually create a tool to list all database models that we had created and then the tools can generate the same code for use. Using the codeTo use the code you should read the full documentation, in short here is the way we use the library : Initialize using JohnKenedy.DataAccess; DataAccessLayer _dal = null; protected void Page_Load(object sender, EventArgs e) { _dal = new DataAccessLayer(SqlServerType.MSSQL, System.Configuration.ConfigurationManager. ConnectionStrings["connection"].ConnectionString); if (DataAccessLayer.Manager == null) DataAccessLayer.Manager = _dal.GetDataAccessTableManager(); else DataAccessLayer.Manager.DALLayer = _dal; if (DataAccessLayer.Audit == null) DataAccessLayer.Audit = _dal.GetAudit(); } You see an DataAccessLayer object is created, this is the main object of this library, the object is created by passing two parameters, first is an enumeration that tells which database type, the second is the connection string which in this example is retrieve from web.config. Insert/Update/Delete Operations Suppose we have a table look like this
JohnKenedy Library enables user to insert to this table, update, and delete without creating any SQL command, the SQL command will be generated by libraries automatically, and it has several automation features as described in Introduction section
_dal.OpenConnection(); DataAccessTableFiller _filler = DataAccessLayer.Manager["MFood"].GetFiller(); _Filler.AddColumnValue("Name", "Chicken Soup"); _Filler.AddColumnValue("Price", 1000); _Filler.AddDefaultColumnValueForOtherNotNullableColumn(); IDbCommand _command = _filler.GetInsertStatementFilterIdentity(); long _value2 = DataAccessLayer.Manager["MFood"]. ExecuteNonQueryInsertIdentityValueFromCommand(_command); _dal.CloseConnection(); The _value2 will contain the NoFood identity value. If the Name length is more than 50 characters, the text will be truncated to 50 characters length before insert to prevent error. Although we don’t supply a value for Status, the library will automatically add default value for column Status, the default value for int is 0, that’s why the code will not error. The truncate and automatically add default value will be recorded in Audit Trail, read Preparing Audit Trail section to prepare Audit Trail for library in the Full Documentation (JohnKenedy DAL.pdf)> The example above uses DataAccess directly, in fact Programmer can use JohnKenedy.DataAccessModule tool to generate entity class and entity collection code. The code for using the Entity and Entity Collection class, is Suppose we have MFood.cs and MFoodCollection.cs generates from JohnKenedy.DataAccessModule MFood _food = new MFood(); _food.Filler.AddColumnValue("Name", "Chicken Soup"); _food.Filler.AddColumnValue("Price", 1000); _food.Filler.AddDefaultColumnValueForOtherNotNullableColumn(); long _value2 = _food.InsertIdentity(); Points of InterestFeel free to use this library and please let me know if there is bugs, or suggestions, thank you. HistoryThis is the first release of this application, If there is changes in the library I will update this article.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||