C# OptimalFramework for DOT.NET 2.0
Download OptimalFramework for DOT.NET 2.0 Version 1.0.0.2
10-01-2008: Latest updates/fixes/features
  • Data compression within a client/server situation
  • Data encryption within a client/server situation
  • Transaction support
  • Advanced exception handling
  • Some little bugs fixed
  • Some extra designer featurs 
18-03-2007: Latest updates/fixes/features
  • Some Client/Server synchronization bugs fixed.
  • Better SQL generation.
  • Some little designer bugs fixed.
  • Auto synchronization of identity fields functionality added.
Introduction
"DataSets vs. Objects?" is a frequently asked question for developers who work with databases. Both scenarios offer advantages and disadvantages. After having worked with Delphi (midas) and Visual Studio for a while, I was convinced of the fact that the best of both worlds could be combined in one component set. That's the reason I built a component set that offers an easy way to manipulate your database data in a Visual Studio environment.

OptimalFramework also offers:

- Minimal coding due automatic generated DataClasses/Collections.
- DataClasses which can take care of applying changes to database themselves (no more Fill,  Update on the SqlDataAdapter).
- Auto generated Access Code  (no SqlParameters need to be set to get data from your database anymore).
- Designer which is easy to use with SQL Highlighting (and does not mess up your SQL like the default SqlCommand editor in Visual Studio).
- Posibility to add more than 1 scenario to fetch a DataClass/Collection.
- Transparent Client Server Model (through a webservice).
Objective
This component set is still in a beginning fase. Your opinion is important for further development! Any feedback (suggestions, improvements, experiences, bugs etcetera) will be very much appreciated! Please leave a post at the bottom of this article with your experience with the OptimalFramework. Thanks in advance!
Installation
 
- Just run the OptimalFramework.msi and install...
(a new tab will be added on your component toolbox with the OptimalFramework components)
A "Client only" situation in a nutshell

The most important component of the OptimalFramework is the DataProvider. This DataProvider can be used in both a "Client only" and a "Client/Server" situation. In this basic example we will use the DataProvider on a WindowsApplication. The following steps will show how the DataProvider works.

Note: the OptimalFramework will only work in a C# solution. (VB.NET support perhaps in the future.) The OptimalFramework has been tested on a SQLserver 2000 database.
1) Create a new Windows Application.
- File --> New --> Project --> WindowsApplication

2) Setting up the components
- Add a SqlConnection (System.Data) to your Form1.cs [design] (drag and drop from toolbox).
- Configure the SqlConnection with a valid ConnectionString.
- Add a DataProvider component to your form (drag and drop from toolbox).
- In the properties window, set the Connection property of the dataprovider1 to the SqlConnection1.


3) Configure the DataProvider
- Right click the dataprovider1 component and click "Configure Provider" (or dubbelclick the dataProvider1 component).
- In the displayed designer, add some valid SQL in the SQLTextbox like in screenshot below. (Leave the Scenario1 empty).
(In this case, all Employees will be returned by the query.)
- Click the "Add scenario" button to add an extra scenario to fetch the data through parameters (@) (like screenshots below).
(Note: Parameters can be used in both Main Sql section and Scenarios section.)
- If you expect a single "record" result in a scenario you can uncheck the "Multiple result set" checkbox (not obliged). 
- The "Create access methods" checkbox is irrelevant for a "Client only" situation and must be checked.




- After setting up your SQL, switch to the "Class" tab to set up the remaining configuration.
- Select a project (ComboBox) to specify where the "DataObjectClass" will be created in (for example, this can be a class library for your data definitions). For now choose the current WindowsApplication project.
- If you like, select a folder to create the "DataObjectClass" in (not obliged). If the folder does not exist, it will be made.
- Define the Classname of your "DataObjectClass"  (for instance: Employee).
- Configure your parameters and Classmembers.



- After clicking the OK button, your "DataObjectClass" will be created in the selected Project/Folder (in this example it will be Employee.cs).
- Also the container of the dataprovider1 will be provided with access methods to retreive your Employee/EmployeeCollection.


4) Accessing your Data
- After creating your class, you can call the auto generated code to fetch your data.
Use:
* employee1.ApplyUpdates();   to save the changed data back to the database (SQL will be generated automatically).
* employee1.State;  to retreive the state of the object (insert/edit etc)
* employee1.Info.FirstName to retreive some extra info about the Firstname like IsChanged, OldValue, IsKey etc.
* employee1.Delete(); to delete the employee (when in a collection an ApplyUpdates() of the collection  is needed for committing to Database).

* employeeCollection1.ApplyUpdates();  to save changed data of all containing Employees to the database.
* employeeCollection1.Insert();   to insert a new Employee to your collection (needs ApplyUpdates() to insert in Database).

Check out the events on the DataProvider to control the apply-updates to the database.
"Client/Server" situation (Webservice) in a nutshell

To manage a "Client/Server" model with the OptimalFramework is quite easy. In the folowing image you will see the idea behind the OptimalFramework "Client/Server" model (project setup). This model is not obliged to maintain, but is strongly recommended.



1) Webservice, the server site of your application.
2) ServerBusiness is the project which manages the DataProvider components for your data (also contains the SqlConnection).
3) Shared Data Project. This project contains the autogenerated DataClasses of the DataProviders in the ServerBusiness Project.
4) Client Project (can be of any kind) is the project that only communicates with the Webservice, but must know the definition of the Shared Data Project, so the SharedData.dll lives both on the server and client.
1) Creating the projects
With File --> New --> Project --> Other Project Types --> Visual Studio Solutions --> Blank Solution, create a blanc solution:
- Then add a new Webservice by right clicking the solution (solution explorer window) and Add --> Add new Website --> ASP.NET Webservice (give it a name like MyWebService)
- Then add a new Classlibrary by right clicking the solution and Add --> Add new Project --> ClassLibrary (give it a name like MyServerBusiness)
- Then add a new Classlibrary by right clicking the solution and Add --> Add new Project --> ClassLibrary (give it a name like MyData)
- Then add  a WindowsApplication by right clicking the solution Add --> Add new Project --> WindowsApplication  (give it a name like MyClient)
- If you like, remove Class1.cs from the MyData and MyServerBusiness projects.


2) Setting up MyServerBusiness
- On the MyServerBusiness project add an new Component by right clicking the project an Add --> Component (give it a name like EmployeeBusiness).
- On the created component add a SqlConnection (System.Data) (drag and drop from toolbox).
- Configure the SqlConnection with a valid ConnectionString.
- Also add a DataProvider component (drag and drop from toolbox).
- Set the Connection property of the dataprovider1 to sqlConnection1.
- Configure the provider just as the Client situation by adding Sql etc.
- The only difference is the project in wich the DataObject must be created. Choose the MyData project.
- Because of the server situation, you can uncheck the "Create access methods" if you like (not obliged).
( This will result in not creating the access code on the server. In some cases you want to be able to call these methods
on the server.)


3) Setting up the Webservice
- On the webservice open the Service.cs [design], so you can add components.
- Add a ServerProvider component to Service.cs (drag and drop from toolbox).
- A webmethod (of the OptimalFramework) will be automatically added to your Servive.cs. (see sceenshot).
- Add a reference to MyServerBusiness.dll.
- Build the solution (Ctrl + Shift + B).

4) Setting up the Client
- Open Form1.cs [design] on the Client.
- Add a RemoteConnection component on the form (drag and drop from toolbox).
- Set the Url of the remoteConnection to the webservice (something like: http://localhost/MyWebservice/Service.asmx).
- You can test the remoteConnection by right clicking it and choose "Test Connection".
- Add a ClientProvider component on the form (drag and drop from toolbox).
- Set the Connection property of ClientProvider to the added RemoteConnection.
- Right click the clientProvider1 component and choose "Configure Provider" (or double click the component).
- In the dialog, choose the DataProvider you want to connect to, and choose OK.



- Access code will be generated automatically, just like the normal DataProvider does.
- In case of an ApplyUpdates, the DataObjects will be automatically send to the server where ApplyUpdates will be emitted on the original DataProvider. 


Transactions
Transaction support is available in the client only AND client/server server situations. How does it works? It's possible to start a transaction on a ClientProvider or a DataProvider. The scope of the transaction is based on the ConnectionString of the SqlConnection.



What's next?
Future additions/changes will mostly depend on the reactions on this article.

Some possible future changes/additions might be:

- Some additional Classmember settings, like default value / display format / Max length.
- Code generation for VB.NET.
- Support for Remoting Servers.
- Integration with Visual Studio sourcecontrol.
- Integration with Visual Studio 2008
Post your comment

Move to top