Page 1 of 1
Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 2:01 pm
by ZLA
I'm sure this is a truly newbie question but I haven't implemented a Object Data Source and generated data access layers before. I've reviewed the topic
viewtopic.php?f=47&t=6889&hilit=namespa ... cess+layer but for me, it assumes several things.
I've got MyGeneration installed and working and can generate the code files. But now I don't know what to do with them. I think I have to add them to the application Data Source but I'm not sure how to do that. One article pointed me towards AbleCommerce.mdf but when I try to open it, I get a message Connections to .mdf files require SQL Server Express 2005. I'm using SQL Server 2005 Developer Edition.
Can someone help me out? Thanks.
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 2:21 pm
by jmestep
If you are going to use them in your store and not create a separate dll, you can put the code into a .cs file in the App_Code folder.
If you had more than one table involved, you will need to reformat the code some. There will be extra namespace and using sections with each section of code for the table involved.
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 3:34 pm
by ZLA
Thank you for the info Judy. After generating my files, I'm wondering if I've got the right templates or if MyGeneration isn't configured right for my computer. I grabbed the templates from the first attachment in this post:
viewtopic.php?f=47&t=9530&hilit=data+access+layer. In
viewtopic.php?f=47&t=6889&hilit=namespa ... cess+layer, they give an example of the generated collection class as:
Code: Select all
namespace CommerceBuilder.Marketing
{
using System;
using CommerceBuilder.Common;
/// <summary>
/// This class implements a PersistentCollection of Affiliate objects.
/// </summary>
public partial class AffiliateCollection : PersistentCollection<Affiliate>
{
/// <summary>
/// Gets the index of the CatalogNode object in this collection whose primary key
/// matches the given value.
/// </summary>
/// <param name="affiliateId">Value of AffiliateId of the required object.</param>
/// <returns>Index of the required object.</returns>
public int IndexOf(Int32 affiliateId)
{
for (int i = 0; i < this.Count; i++)
{
if (affiliateId == this[i].AffiliateId) return i;
}
return -1;
}
}
}
but my generated code came out like this:
Code: Select all
using System;
using CommerceBuilder.Common;
namespace YourNamespace
{
public partial class ShipIntervalsCollection : PersistentCollection<ShipIntervals>
{
public int IndexOf()
{
for (int i = 0; i < this.Count; i++)
{
i ) return i;
}
return -1;
}
}
}
Overall, there were several things off:
- no comment lines in any generated file.
- IndexOf won't compile and doesn't seem similar to the one in AffiliateCollection.Generated.cs
- the ShipIntervals default constructor is repeated twice in ShipIntervals.Generated.cs
- MyGeneration didn't create the output files automatically nor did it seem to remember the template default folder location (which I set to something other than the installation directory).
The MyGeneration issues are minor for now. It's installed on the C: drive but my templates are stored on my D: Drive.
Please let me know if I've missed a step somewhere. Thanks.
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 3:50 pm
by jmestep
You might not have had a primary key on the table. Here's one I did-- you do have to clean them up some, but it is so much faster than writing it all by hand.
Code: Select all
using System;
using CommerceBuilder.Common;
using CommerceBuilder.Data;
namespace W2MCustomClasses
{
public partial class ReferralOrdersCollection : PersistentCollection<ReferralOrders>
{
public int IndexOf(Int32 pOrderId)
{
for (int i = 0; i < this.Count; i++)
{
if( ( pOrderId == this[i].OrderId) ) return i;
}
return -1;
}
}
}
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 4:04 pm
by ZLA
Bingo! That fixed the weird duplications and compile issues.
Should there be comments in the generated files? Do I need to customize the templates for that or add them by hand?
Also, what are the best sources of documentation for MyGeneration? Like most help, it suffers from a deficient index.
Lastly, I assume I only need to split the generated files if I need to customize the class. If the default works for me, then I'll just have three class files, correct?
Thanks.
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 4:23 pm
by jmestep
I've added comments to my generated files by hand and I've also split them up for each "object" (table). mazhar also recommended naming them something like xxxgenerated.cs. That is what Able did with their code so that they can regenerate them easily without worrying about overwriting their custom stuff they couldn't generate. For example, they have an order.cs and an ordergenerated.cs in their source code-- How cool is that?
If you add comments, you can also come up with some cool help files using Sandcastle Help File builder- see image attached.
Help.JPG
Re: Need Help on using Generated DAL code
Posted: Tue Jun 02, 2009 4:53 pm
by ZLA
Thanks Judy.
I added a custom table which cross references vendors and ship methods. It doesn't contain a StoreId field since vendors and shipmethods do. If I want LoadForStore method(s) in my table's datasource class, I assume I have to copy and modify the LoadForCriteria method. Of course, I'll have to split the file in case of future regenerations.
Are all custom LoadForXxx methods done manually or do any templates generate that code based on foreign key fields in the underlying table?
Re: Need Help on using Generated DAL code
Posted: Wed Jun 03, 2009 4:23 am
by mazhar
No currently templates do not generate load methods for foreign key references you need to write that manually.