Subsonic 3 alpha + ado.net data services any samples
There are several resources on the internet describing previewing 2 subsonics with Astoria:
and a working sample in
http://code.msdn.microsoft.com/SubSonicForADONETDS
I applied all relevant changes to subsonic tt (s), however failed to complete work on MSDN project. After elimination:
a) Astoria didn't like the private DB () {} in QuerySurface.tt, so I blindly made a public constructor
b) Not sure how to create a composite primary key
<# if(EnableForUseWIthAstoria) {
#> [System.Data.Services.Common.DataServiceKey("<#=pk#>")] <# }#>
leads to
[System.Data.Services.Common.DataServiceKey("")]
instead
[System.Data.Services.Common.DataServiceKey("OrderID", "ProductID")]
so the table is just excluded.
Current obstacle
var q = from cust in ctx.Customers
where cust.CustomerID == "ROMEY"
select cust;
Customers c = q.First();
results in an exception: Resource not found for segment "Clients"
Has anyone tried this or knows another latest and greatest sampling existence?
a source to share
Yes, I checked the ss3 version with IUpdatable interface and inherited the database query surface class from the UpdatableDatabase class. I also included a starter test project in it. The nice part is that you can build a DB class with a Uri and start working with the service. But this is not part of the current core, it requires a new class and some changes, as well as minor template changes. I think this is one of the areas where people will continue to invent this thing rather than build on previous work. There are a few changes I wanted to get to the project, but they don't, like multiple databases installed at runtime, ado.net services, etc. I guess I'll have to deploy my own version forever.
This issue has an attachment showing the UpdatableDatabase class.
I added this to SubSonicClasses.ttinclude
:
public string PrimaryKey
{
get { return Utilities.CleanUp(this.Schema.GetTablePrimaryKey(TableSchema, TableNameRaw)); }
}
...
[System.Data.Services.Common.DataServiceKey("<#=PrimaryKey #>")]
I see that I cheated with OrderDetails in Northwind and added a second key by editing the file directly. You can easily write a method like this in DatabaseSchema.ttinclude
:
public string[] GetTablePrimaryKeys(string tableSchema, string tableName)
and create the correct line.
a source to share
I don't know if I can pin the solution and issue it as there is a subsonic license. It is not subsonic and is about 2 months old (outdated). I just ran the project to check that it still works and it does. Here are the steps to do this:
Use the UpdatableDatabase class mentioned above. Then take the DB out of it (put this in a template):
public partial class DB: updated database
UpdatableDatabase.cs must be in the generated classes or it won't work as there must be GetType () for table classes.
A service is just a service project with this class:
using System.Data.Services;
using Northwind;
namespace NorthwindService
{
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class Northwind: DataService<DB>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(IDataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.UseVerboseErrors = true;
}
}
}
The service part of web.config is simple:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
Then, for the test project, add a service link to the service. I took the test code from the astoria project, I think it has been for a while:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WcfClientTest.NorthwindService;
namespace WcfClientTest
{
/// <summary>
/// Summary description for WcfTest
/// To run these tests, load this project, and somehow get a server running at the URI.
/// This can be done by updating the service reference to start the development server.
/// </summary>
[TestClass]
public class WcfTest
{
private string baseURI = "http://127.0.0.1:49649/Northwind.svc";
private DB ctx;
/// <summary>
/// Sets up test.
/// </summary>
[TestInitialize]
public void SetUp()
{
ctx = new DB(new Uri(baseURI));
}
[TestCleanup]
public void Cleanup()
{
}
[TestMethod]
public void Select_Simple_With_Variable()
{
int categoryID = 5;
IQueryable<Product> result = from p in ctx.Products
where p.CategoryID == categoryID
select p;
List<Product> products = result.ToList();
Assert.AreEqual(7, products.Count());
}
[TestMethod]
public void TestAddNew()
{
// add customer
var c = new Customer
{
CustomerID = "XXXXX",
ContactTitle = "Prez",
Country = "USA",
ContactName = "Big Guy",
CompanyName = "Big Guy Company"
};
ctx.AddToCustomers(c);
ctx.SaveChanges();
IQueryable<Customer> qCustomer = from cust in ctx.Customers
where cust.CustomerID == "XXXXX"
select cust;
Customer c2 = qCustomer.FirstOrDefault();
Assert.AreEqual("XXXXX", c2.CustomerID);
if (c2 != null)
{
ctx.DeleteObject(c2);
}
ctx.SaveChanges();
IQueryable<Customer> qCustomer2 = from cust in ctx.Customers
where cust.ContactName == "Big Guy"
select cust;
// Returns null if the row isn't found.
Customer c3 = qCustomer2.SingleOrDefault();
Assert.AreEqual(null, c3);
}
}
}
That's all I have, it's not hard to put together. Now this solution is in search of a problem, but I intend to use it someday. It would be possible to completely bypass the subsonic interface and use the IQToolkit directly, and with some T4 templates have a pretty nice system.
a source to share