Almoust all recent posts apply to POCO. Worm has more powerful mechanism to map database entities. It based on predefined base classes. They are implement the following interfaces: IEntity, ICachedEntity and IKeyEntity. We'll talk about the interfaces later. So, what purposes do they serve? It's
- Additional functionality
- Performance
- Persistence in local memory (caching)
- Partial loading
- Lazy loading
- and more
You can see class diagram.
Let's see how base class can affect application performance. We gonna add CachedEntity base class to SalesPerson.
[Entity("Sales", "SalesPerson", "1")]
public class SalesPerson : CachedEntity
{
[EntityProperty("SalesPersonID", Field2DbRelations.PK)]
public int ID { get; set; }
public decimal? SalesQuota { get; set; }
[EntityProperty("TerritoryID")]
public SalesTerritory SalesTerritory { get; set; }
public decimal Bonus { get; set; }
public decimal CommissionPct { get; set; }
public decimal SalesYTD { get; set; }
public decimal SalesLastYear { get; set; }
public DateTime ModifiedDate { get; set; }
[EntityProperty("rowguid", Field2DbRelations.RowVersion)]
public Guid Timestamp { get; protected set; }
public static QueryCmd Query
{
get
{
return new QueryCmd(exam1sharp.Properties.Settings.Default.connString)
.From(typeof(SalesPerson))
.SelectEntity(typeof(SalesPerson));
}
}
}
As you can see no other changes were made. The program to measure the time
static void Main(string[] args)
{
int iterCount = 1000;
DateTime start = DateTime.Now;
for (int i = 0; i < iterCount; i++)
{
foreach (Sales.SalesPerson s in Sales.SalesPerson.Query
.ToPOCOList<Sales.SalesPerson>())
{
string str = string.Format("Store id: {0}, bonus: {1}, territory name: {2}",
s.ID, s.Bonus, s.SalesTerritory != null ? s.SalesTerritory.Name : "no territory");
//Console.WriteLine(str);
}
}
Console.WriteLine("POCO time {0}", DateTime.Now - start);
start = DateTime.Now;
for (int i = 0; i < iterCount; i++)
{
foreach (Entity.SalesPerson s in Entity.SalesPerson.Query
.ToList<Entity.SalesPerson>())
{
string str = string.Format("Store id: {0}, bonus: {1}, territory name: {2}",
s.ID, s.Bonus, s.SalesTerritory != null ? s.SalesTerritory.Name : "no territory");
//Console.WriteLine(str);
}
}
Console.WriteLine("Entity time {0}", DateTime.Now - start);
}
Here is the time:
POCO time 00:00:18.2705521
Entity time 00:00:10.9729305
We have more than 40% performance gain!