For those who uses Web Client Software Factory, you may find that it doesnt create a Data Access Layer for you and you might have wasted more than 1 day to write all the data access classes. Thanks to Codeplex, we have a repository factory that handle all them the data access functions for you.
If you are new to Repository Factory, please read the
introduction first.
In this post, I will explain on how to integrate both of them into a web application to speed up your development as well as to cut down your maintenance cost.
First, Create Web Client Software Factory solution, and name it. Let's say "SampleWebApp". WCSF will generate all the default structures and files for ya.
Now, right click on the Solution, and Select Add-> New Project. You need to create 3 Class Library projects and name it as following:-
1.) Repository.Host
2.) Repository.DataAccess
3.) Repository.BusinessEntities
You may want to ask why 3? Good question, usually 1 is
sufficient but there are some bugs with the Connection String. Repository
Factory requires you to use a project to host the connection string and most of
us uses website project to hold the reponsibility, but Connection String couldnt
be found by the Repository Factory wizards when we create Business Modules/Views
using WCSF first before creating data repository class files. So if you are
using 1 Class Library project, make sure you start from repository project
first.
Besides, using 3 Class Library projects are alot easier to
maintain. For example, when you are creating Data Access or Business Entities,
the namespace of the class files will follow the Class Library project name, for
instance "Repository". So it will be very messy if Business Entities and Data
Access files fall into the same namespace. If you want to split them up in the
namespace, it will take some time for you to manually change the namespace of
each file into Repository.DataAccess or
Repository.BusinessEntities.
Next, assign the Reponsibility of each Class Library projects by right click each Class Library projects and select Repository Factory -> specify project responsibility
1.) Repository.Host -> Host
2.) Repository.DataAccess -> Data Access
3.) Repository.BusinessEntities -> Business Entities
Now, setup the database:-
1.) Create database
2.) Create tables
You have done all the necessary setup. You may start with :-
Create and Assign Connection String1.) Connection String for Repository FactoryRight Click the Repository.Host project, Select Repository Factory -> Add Database Connection
2.) Connection String for WebsiteGo to the website node, and search for the master web.config. Right Click and Select Repository Factory -> Add Database Connection to Configuration file. Once you have created it, Right Click the web.config and Select Edit Enterprise Library Configuration. Select Data Access Application Block node and open Properties. Assign the default Database to the Connection String that you just created.
Create a business module and a page under the moduleUsing WCSF, right click Modules section and select Web Client Factory -> Add a Business Module(C#/VB). For example, named it "Member".
Under the website node, you will noticed that a Member folder is created. Right Click and select Web Client Factory -> Add a View(C#/VB). Then an aspx page will be created under Member module. To verify it, you will notice that a presenter class file is created along under the Member module node. For instance, "CreateMember.aspx"
Create Business EntitiesGo to Repository.BusinessEntities project, right click and select Repository Factory->Create business entities from database. It will prompt you a wizard. Follow the wizards and complete the process.
1.) First, select the Host Project, it should be Repository.Host, and the connection string and then Click Next.
2.) Then, select the tables that you want.
3.) View the attributes and Click Finish.
The business entities class files for the tables you want are created under the project.
Create Stored ProcedureGo to Repository.DataAccess project, right click and select Repository Factory->Create CRUD Stored Procedure. Follow through the wizard again.
1.) First, select the Host Project, and the connection string and then click next.
2.) Select the tables that you want.
3.) Then you will notice that the wizard provide 5 common operations for each table. If there is any that you do not need, uncheck the checkbox for the particular operation. Change the SP name if necessary. Then click Next.
4.) In the last screen, input a file name. And click Finish.
Repository Factory has created a SQL file for you to create the stored procedures. Copy and Paste the script into Management Studio and execute it.
Before you execute it, make sure to change all the Alter Procedure to Create Procedure. Click Check and Execute. If there are errors saying procedures' names existed, Change all the Create Procedure back to Alter. Then execute it again.
Note that if you are using SQL Server 2000, there are some syntax taht you need to remove, for example, remove
begin try, end try, begin catch, end catch, RethrowError procedureand replace
sys.objects->sysobjectsOnce you have successfully execute the script, you can find all SP in the database. If you want to create a customized SP, you may need to create them manually in the Management Studio.
Create Data Repository ClassesNow it's time to create Data Repository classes. Right Click Repository.DataAccess and select Repository Factory->Create data repository classes from business entities.
Go through the wizard again.
1.) Select the Host project and connection string and click Next
2.) Select the business entities project and click Next
3.) Select the entity that you want and click Next
4.) Then, in this screen you will find that the wizard has created all the common operations for you except GetBy operations.
Auto Created Operations: Insert, Update, Delete, and GetAll
5.) Now you need to create GetByPK (Get One) by clicking add, select the right stored proc and check the mapping. if correct click finish.
6.)Then you need to create GetByFK (this one usually Get Many) if applicable.
I assume you know how to create them. Once you have done, click finish and all the data repository classes will be created.
If you want to modify the namespace, you may do so. Else save and build it. Make sure you do not have any error.
NOTE: Do not overwrite any repository classes. Must Remember! Delete it before you copy over.Create function to access Data Access LayerGo to Module section, look for Member module (if you are working on member module)
1.) right click References and select Add References.
2.) under the Projects tab, add Repository.BusinessEntities and Repository.DataAccess. (So that you have the reference to those projects and use their fucntions).
3.) you will also need to add reference for Microsoft.Practises.Repository.dll into the module references. you can find it in C:/Program Files/Microsoft Patterns & Practices/Data Access Guidance Package Setup/.
4.) Open the controller file. In this case, MemberController.cs and insert the following codes:
using Repository.BusinessEntities;
using Repository.DataAccess;
using Repository.DataAccess.MemberRepositoryArtifacts;
//In the MemberController Class, insert the following codes:
IMemberRepository _member;
public void CreateMember(member obj)
{
_member = new memberRepository();
_member.Add(obj);
}
5.) Save and Close.
Create a business function for insert member operation in PresenterIf you have created a View/Page under Member module, for e.g. CreateMember.aspx, you will notice a CreateMemberPresenter file in the same module under View folder.
1.) Open it and if the controller object is commented, uncomment it.
2.) Create a function as follow:-
public void OnCreateMember(member obj)
{
_controller.CreateMember(obj);
}
To simulate the outcome1.) create a link button in Default.aspx under Member folder in the website node.
2.) In the code behind, insert the following codes into the link button OnClick event handler.
Response.Redirect("/DevelopmentWebsite/Member/CreateMember.aspx");
Then, Im going to simulate an insert during a page initialize/load.
3.) Open CreateMemberPresenter.cs or vb.
4.) Look for the function OnViewInitialized().
5.) In the function write the following codes:
public override void OnViewInitialized()
{
member mem = new member();
// I did not insert value for "id" because it is set auto-increment in the table.
mem.name = "Chris";
mem.age = 26;
this.OnCreateMember(mem);
}
FinishIn real project, you may want to supply the data using form or by user input. So you may ignore the Simulate section as it is just to verify the result.
Hope it helps.