Showing posts with label Repository Factory. Show all posts
Showing posts with label Repository Factory. Show all posts

Friday, July 25, 2008

Repository Factory for VS 2008

This has been awhile and Thanks Daniel for pointing it out!! Guys, whoever wanting this for a long time, please visit the link below and leave a comment.

Thanks a lot to Ratnakar.

http://ratnakarg.wordpress.com/2008/04/07/repository-factory-for-vs-2008/#comment-52

You may also leave a comment here and I can email it to you.

Cheers

Monday, June 23, 2008

Intergrating Web Client Software Factory and Repository Factory

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 String
1.) Connection String for Repository Factory
Right Click the Repository.Host project, Select Repository Factory -> Add Database Connection

2.) Connection String for Website
Go 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 module
Using 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 Entities
Go 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 Procedure
Go 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 procedure

and replace

sys.objects->sysobjects

Once 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 Classes
Now 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 Layer
Go 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 Presenter
If 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 outcome
1.) 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);
}

Finish
In 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.

Tuesday, June 10, 2008

Repository Factory Error: Object reference not set to an instance of an object.

Problem Statement
If you are using repository factory to create your data access layer and you get error in page showing

"Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object."

And the problem is pointing you at the Repository Object, for example:

Line xx: public sampleRepository()
Line xx: : base()

The problem comes from the connection string. You have not specified a default connection that you are using.

Solution
1.) Open your web.config with your Enterprise Library Editor, and select Data Access Application Block node and open it in Propertise.
2.) In the DefaultDatabase field, select your connection string that you have created.

Compile the project and run again. It should work now.

Creating Data Access Layer using Repository Factory

Repository Factory is a guidance package that automates creation of entity classes that map to database tables and repository classes to read and write those entity classes.

There is an existing blog that has done a good job in demostrating how it works so I dont think I need to rewrite a new one. Please visit http://developer.bracora.com/Articles/DataAccessLayer.aspx

If the link is broken, do let me know and I shall consider writing a new one.

Monday, June 9, 2008

How to make Repository Factory supported in VS 2008?

I believe this has been an issue to alot of developers that have used to build the data access layer using Repository Factory for the past years using VS 2005. Unforturenately, Microsoft has shown no more interest in upgrading the current package to support VS 2008 and .net 3.0 above.

However, the community has taken alot of test to move it to VS 2008. I wish to give a big thank to 2 developers who have provided us a workaround, Jose Escrich and Noel Anderton!

Noel Anderton 's piece
"So we have taken the Source for the Guidance and opened in vs2008 this updated the guidance to vs2008 projects with out incident. We made sure we had the VS 2008 Guidance package for creating guidance packages installed and enabled the package. We then ran the Register guidance recipe which successfully registered the Repository Package Guidance. We opened another version of VS2008 and used the repository guidance to create a solution.. Great. So we tried the installer. We updated the registry paths in the launch conditions of the set up project and after unregistering in vs the guidance package we ran the installer. The installation was ran without error but when we attempted to use the repository guidance package we could not enable it. It did not show up. "

the above content can be found in http://www.codeplex.com/RepositoryFactory/Thread/View.aspx?ThreadId=23088

Jose Escrich's piece
To achieve that you must add/change some arguments in all the custom actions as follow:

/Hive=9.0 /Configuration="[TARGETDIR]MyGP.xml"

the argument that you have to change is /Hive=9.0 and also you can specify something like /Hive=9.0Exp /RANU in case that if you want to register the guidance package into the experimental hive and RANU mode. Note that changes applies just for the installer so, if you want to change the registration hive in design time, that's when you register a GP through Visual Studio, you have to add under the GuidancePackage element a HostData child element as follow:
<HostData>
<RegistrationSettings>/Hive=9.0</RegistrationSettings>
</HostData>


the above content can be found in http://forums.msdn.microsoft.com/en-US/vsgatk/thread/194e5c70-aa51-4de6-96df-cfda1c8832be