Before I can start doing some cool wpf smart client stuff, I’m going to need a service to give me data objects. I’m going to use WCF since it’s pretty flexible and it’s ‘hot’.
I’m still crappy at naming these things… but I usually do <Company>.<Domain>.<Application>.<Component> with each solution being an application and component being an assembly (or subfolder within an assembly)
Click Finish on the next window you see to accept the names… it should generate your projects and look like this:
- BusinessEntitites is your Data objects, like employee, person, sale, etc
- BusinessLogic is any operations you’re doing on the objects prior/after you get or send them to/from the data access layer.(like incrementing/decrementing vacation hours on an employee while you’re creating their paycheck)
- DataAccess is a collection of repositories that can get your objects (we’ll look at this first)
- We’ll worry about the projects in the ServiceInterface folder at a later time.
I think in a normal development situation I would create my business logic first (or at least know which objects I’ll need in my business logic, so I can focus on just making those), BUT since I’m using the toolkit, we’re going to dump the whole AdventureWorks database into business entities automatically!
First, let’s create a connection (they create it in a test host project, so all your future deployments will need the same named connection string in the app.config)
In the automation window, choose Data access
Then run the add database connection recipe:
They usually grey out any project you can’t use, we’re going to use the test project as our target for this database connection:
Name it AdventureWorks and hit the “…” to create a connection string:
Choose SqlServer .net provider and hit ok then go find your AdventureWorks database. NOTE: If your using sqlexpress you MUST use localhost as the servername. (orlocalhost\sqlexpress if you installed it on a separate instance)
The guidance shows our history and it gives us recommended next steps:
We’re going to create entities from database, and do them all to get that knocked out, then we never have to come back to it! J (unless our database changes, but let’s hope you’re far enough in your project to know your database won’t change, or it’s a vendor database that you’re adding functionality to via services)
OK NEXT: click on “Create Business entities from database”
Target the business entities project and accept, then you get this:
Choose your adventureworks connection and hit NEXT… now wait a long time(sometimes it appears to be locked up at this point)
Let’s do them all (hell why not, right?)
NOTE: after you select all, it pauses for a bit before the “next” button enables.
Hit next.
You can now rename them, I won’t because I don’t even know what they are!
Hit Finish
Now it goes and generates the entities. I ranbuild to see if it worked. Mine blew up with these errors:
Error 1 The type 'AdventureWorks.Services.Internal.BusinessEntities.SalesvSalesPersonSalesByFiscalYears' already contains a definition for '_Field'c:\projects\AdventureWorks.Services.Internal\Source\Business Logic\AdventureWorks.Services.Internal.BusinessEntities\SalesvSalesPersonSalesByFiscalYears.cs 64 42 AdventureWorks.Services.Internal.BusinessEntities
Looking at the database, it appears the 3 properties named _Field are 2001,2002,2003 respectively, You can't have variables with names starting with numbers... so that's why it bombed.
The fixed constructor:
public SalesvSalesPersonSalesByFiscalYears(Nullable<System.Int32> salesPersonID, System.String fullName, System.String title, System.String salesTerritory, Nullable<System.Decimal> _y2002, Nullable<System.Decimal> _y2003, Nullable<System.Decimal> _y2004)
{
this.salesPersonIDField = salesPersonID;
this.fullNameField = fullName;
this.titleField = title;
this.salesTerritoryField = salesTerritory;
this.y2002_Field = _y2002;
this.y2003_Field = _y2003;
this.y2004_Field = _y2004;
}
The Fixed Properties:
private Nullable<System.Decimal> y2002_Field;
public Nullable<System.Decimal> Y2002
{
get { returnthis.y2002_Field; }
set { this.y2002_Field = value; }
}
private Nullable<System.Decimal> y2003_Field;
public Nullable<System.Decimal> Y2003
{
get { returnthis.y2003_Field; }
set { this.y2003_Field = value; }
}
private Nullable<System.Decimal> y2004_Field;
public Nullable<System.Decimal> Y2004
{
get { returnthis.y2004_Field; }
set { this.y2004_Field = value; }
}
Ok, now it builds. I think that’s enough torture for one day. Next time we’ll figure out what we want to work with first, make stored procedures and data access CRUD commands.