Skip to content

Instantly share code, notes, and snippets.

View FransBouma's full-sized avatar

Frans Bouma FransBouma

View GitHub Profile
@FransBouma
FransBouma / gist:5362167
Last active December 16, 2015 02:19
100% test coverage, yet a bug. Can you spot it? Don't have too much confidence in 100% test coverage. For some reason the code below can return an old value for a field, even if the new value is read back from the DB.
protected object GetValue(int fieldIndex, bool returnDefaultIfNull)
{
if(_fields == null)
{
return null;
}
if(_fields.State == EntityState.Deleted)
{
throw new ORMEntityIsDeletedException("This entity is deleted from the database and can't be used in logic.", this);
@FransBouma
FransBouma / Code
Created May 14, 2013 14:36
First LLBLGen Pro v4.0 async api results. :)
public static string ConnectionString = @"data source=zeusVM\SQLSERVER2005;initial catalog=AdventureWorks;integrated security=SSPI;persist security info=False;packet size=4096";
static void Main(string[] args)
{
Console.WriteLine("Starting");
FetchEntityCollectionAsync();
Console.WriteLine("Fetch called. Press enter to quit");
Console.ReadLine();
}
@FransBouma
FransBouma / gist:7958092
Created December 14, 2013 11:20
Optimization for closing a SqlDataReader as it otherwise can be rather slow with pulling statistics and non-read rows before it actually closes the reader
/// <summary>
/// Cleans up the data reader (closes it if necessary and disposes it)
/// </summary>
/// <param name="reader">The reader.</param>
/// <param name="queryExecuted">The query executed.</param>
/// <param name="disposeReader">if set to <c>true</c> [dispose reader].</param>
internal static void CleanupDataReader(IDataReader reader, IQuery queryExecuted, bool disposeReader)
{
if(reader != null)
{
@FransBouma
FransBouma / gist:8933631
Created February 11, 2014 12:02
How silly the Telerik DataAccess wizard is.
// in the generated massive file for fluent mappings.
public partial class FluentModel : OpenAccessContext, IFluentModelUnitOfWork
{
private static string connectionStringName = @"AdventureWorksConnection";
private static BackendConfiguration backend = GetBackendConfiguration();
// The line below is generated by the Telerik 'wizard' (which should actually be called 'dumbass') and causes the code to recreate the mapping model
// every time.
@FransBouma
FransBouma / gist:9736633
Created March 24, 2014 08:53
Even with 20+ years of development experience, Monday mornings are hard...
object newValueInType = newValue;
string newValueAsString = newValue as string;
if(type == typeof(DateTimeOffset))
{
newValueInType = DateTimeOffset.Parse(newValueAsString);
}
if(type == typeof(Guid))
{
newValueInType = Guid.Parse(newValueAsString);
}
@FransBouma
FransBouma / gist:10393394
Last active August 29, 2015 13:58
LLBLGen Pro typed list generated as poco + query (Linq or QuerySpec) (v4.2)
// Generated linq queries:
/// <summary>Gets the query to fetch the typed list OneToManyPocoLinq</summary>
public IQueryable<Northwind.Adapter.TypedListClasses.OneToManyPocoLinqRow> GetOneToManyPocoLinqTypedList()
{
var current0 = this.Customer;
var current1 = from c in current0
join o in this.Order on c.CustomerId equals o.CustomerId
join od in this.OrderDetails on o.OrderId equals od.OrderId
select new {o, c, od };
@FransBouma
FransBouma / gist:10972512
Last active August 29, 2015 13:59
Creating Expression<Func<T>> at runtime.
// these lambda's are used in e.g. projections in typed queries to materialize object arrays into typed objects.
// running this 100000 times at runtime takes 20551ms
System.Linq.Expressions.Expression<Func<OrderPocoQsRow>> l = () => new NW26.Adapter.TypedViewClasses.OrderPocoQsRow()
{
CustomerId = OrderPocoQsFields.CustomerId.ToValue<System.String>(),
EmployeeId = OrderPocoQsFields.EmployeeId.ToValue<Nullable<System.Int32>>(),
Freight = OrderPocoQsFields.Freight.ToValue<Nullable<System.Decimal>>(),
OrderDate = OrderPocoQsFields.OrderDate.ToValue<Nullable<System.DateTime>>(),
OrderId = OrderPocoQsFields.OrderId.ToValue<System.Int32>(),
RequiredDate = OrderPocoQsFields.RequiredDate.ToValue<Nullable<System.DateTime>>(),
@FransBouma
FransBouma / gist:11211671
Created April 23, 2014 11:30
First Typed View (read only poco, mapped onto view) tests succeeding in LLBLGen Pro Linq Provider:
[Test]
public void TypedViewPocoTest1()
{
using(var adapter = new DataAccessAdapter())
{
var metaData = new LinqMetaData(adapter);
var q = (from i in metaData.InvoicesPocoLinq
where i.Country == "USA"
select i)
Non-change tracking individual fetches (100 elements, 10 runs), no caching
------------------------------------------------------------------------------
Handcoded materializer using DbDataReader : 0,79ms per individual fetch
PetaPoco Fast v4.0.3 : 0,90ms per individual fetch
LLBLGen Pro v4.2.0.0 (v4.2.14.0424), Poco typed view with QuerySpec : 0,95ms per individual fetch
LLBLGen Pro v4.2.0.0 (v4.2.14.0424), typed view : 1,21ms per individual fetch
Linq to Sql v4.0.0.0 (v4.0.30319.33440) : 2,53ms per individual fetch
LLBLGen Pro v4.2.0.0 (v4.2.14.0424), Poco typed view with Linq : 6,50ms per individual fetch
@FransBouma
FransBouma / gist:5e7031fe557df4b5b688
Created June 12, 2014 08:53
Do Contains calls in chunks. Original by Marc Gravell. Fixed for LLBLGen Pro
public static class QueryableChunked
{
public static IEnumerable<T> InRange<T, TValue>(this IQueryable<T> source,
System.Linq.Expressions.Expression<Func<T, TValue>> selector, int blockSize,
IEnumerable<TValue> values)
{
MethodInfo method = null;
foreach(MethodInfo tmp in typeof(Enumerable).GetMethods(BindingFlags.Public | BindingFlags.Static))
{
if(tmp.Name == "Contains" && tmp.IsGenericMethodDefinition && tmp.GetParameters().Length == 2)