I'm testing PetaPoco as an alternative to Fluent NHibernate and I added the extension StaTypPocoQueries.PetaPoco to use lambda expressions to build queries, just like Fluent NHibernate.
I don't want to create an instance of PetaPoco's Database class, so I made a Configuration Manager class to configure it when the program starts.
public class PetaPocoSessionManager
{
public static string connectionString;
public PetaPocoSessionManager()
{
connectionString = null;
}
public IDatabase CreateSession()
{
return DatabaseConfiguration.Build()
.UsingProvider<SqlServerDatabaseProvider>()
.UsingConnectionString(connectionString)
.UsingDefaultMapper<ConventionMapper>()
.WithAutoSelect()
.Create();
}
}
And now in my repository class I build a method to query by some specific field.
[TableName("co")]
public class CustomerOrder
{
public string OrderNumber { get; set; }
public string CustomerNumber { get; set; }
public decimal Price { get; set; }
public DateTime OrderDate { get; set; }
public string OrderType { get; set; }
public string Status { get; set; }
public string Warehouse { get; set; }
public string OriginSite { get; set; }
public string TakenBy { get; set; }
public string ExportType { get; set; }
public string Contact { get; set; }
public string CurrencyCode { get; set; }
public string TaxCode { get; set; }
}
public class CustomerOrderBL
{
private IDatabase session;
public CustomerOrderBL(IDatabase session)
{
this.session = session;
}
public CustomerOrder GetByCoNum(string coNum)
{
return session.FirstOrDefault<CustomerOrder>(x => x.CustomerNumber == coNum);
}
}
However I got an error in the lambda expression that says "cannot convert lambda expression to type 'string' because it is not a delegate type". The thing is that I don't have that error if I use the classic PetaPoco configuration instead of fluent (this is, creating a new Database object per request).
Am I doing something wrong? Or StaTypPocoQueries is not compatible with fluent configuration?
Thanks in advance.
The extension method is for type Database
and not IDatabase
. I think this is why it's not resolving correctly.
Allow me to explain.
Because in your CustomerOrderBL class, you're referencing IDatabase
and not Database
, this is causing session.FirstOrDefault<CustomerOrder>(x => x.CustomerNumber == coNum)
to resolve to T SingleOrDefault<T>(object primaryKey);
, and not the extension method defined by the StaTypePocoQueries.PetaPoco package.
You could easily test this by using the following class
public class CustomerOrderBL
{
private Database session;
public CustomerOrderBL(Database session)
{
this.session = session;
}
public CustomerOrder GetByCoNum(string coNum)
{
return session.FirstOrDefault<CustomerOrder>(x => x.CustomerNumber == coNum);
}
}
If this, as I have predicted, is the case the author Aaron Sherber of StaTypPocoQueries.PetaPoco helps out with PetaPoco a fair bit and is very active on GH. I'd open an issue to discuss it with him, as maybe he should be providing extensions for both Database and IDatabase