Wednesday, October 14, 2009

POCO in Entity Framework 4.0

The new version (4.0) of Entity Framework allows us to use “Plain Old CLR Objects” as entities. In the current version (3.5) every entity must inherit from EntityObject, but in the new version it is possible to use a simple POCO:

public class Order
{
  public Guid Id { get; set; }
  public Product Product { get; set; }
  public int Quantity { get; set; } 
}

In order to use this new feature it is necessary to follow these three steps:
1. Create the edmx file (ModelEntities). 
2. Turn off the code generation removing the value of the “Custom Tool” property.
3. Create the context this way:

public class ModelContext : ObjectContext
{   
  public ModelContext() : base("name=ModelEntities", "ModelEntities")  
  {
  }

  public ObjectSet<Order> Orders
  {
    get { return base.CreateObjectSet<Order>(); }
  }
}

More information:
POCO in the Entity Framework: Part 1 - The Experience
POCO in the Entity Framework: Part 2 – Complex Types, Deferred Loading and Explicit Loading
POCO in the Entity Framework: Part 3 – Change Tracking with POCO

Saturday, October 10, 2009

Default Endpoints in WCF 4.0

One of the hardest things in WCF is its configuration. When you try to host a service without an endpoint declared in the configuration, an exception is thrown (a classic mistake).

But this will change with the new version of the framework (in fact it has already changed in the beta 1).

When you are hosting a service without any endpoint configured, the ServiceHost will read the service’s Uri and will create a default endpoint choosing one of de following bindings:
- http: basicHttpBinding
- net.tcp: netTcpBinding
- net.pipe: netNamedPipeBinding
- net.msmq: netMsmqBinding

More information: A Developer’s Introduction to Windows Communication Foundation (WCF) .NET 4 Beta 1