Sean Collins

sean [at] seanmcollins [dot] com

GPG Key ID: 0xf60f564978913931

sean [at] coreitpro [dot] com

GPG Key ID: 0xA1D7E590

profile for Sean at Stack Overflow, Q&A for professional and enthusiast programmers

Building better business objects with Entity Framework

Topic

The best way to design the business objects

Discussion

An ideal environment for creation of business applications should allow developers to describe the business logic and state of the problem domain which they are modeling with minimum or no “noise” coming from the underlying representation and the infrastructure that supports it.

  • What are the underlying representations?
    • Relational Databases
    • XML/SOAP/JSON
  • What libraries and tools can be used to reduce the “noise” coming from these representations?
    • LINQ
    • ADO.NET Entity Framework
    • Object-Relational Mappers
  • What is LINQ?

The Language-INtegrated Query feature of .NET provides general-purpose query facilities for the .NET runtime, for use with many types of data, including XML, Relational Databases, as well as native data types (Collections, Sets, Arrays, etc…)

Using LINQ, programmers are not forced to switch into the languages of the underlying representations (SQL, XPath, etc), and then build layers of glue code to bring those representations back into the .NET runtime.

LINQ & EF example

// we'll use the order-tracking store
	using(OrderTracking orderTracking = new OrderTracking()) {

	    // find all the pending orders for sales people
	    // in Washington
	    var orders = from order in orderTracking.SalesOrders
			 where order.Status == "Pending Stock Verification" &&
			       order.SalesPerson.State == "WA"
			 select order;

	    foreach(SalesOrder order in orders) {

		// obtain a list of StockAppProduct objects
		// to be used for validation
		List<StockAppProduct> products = new List<StockAppProduct>(
		    from orderLine in order.Lines
		    select new StockAppProduct {
			ProductID = orderLine.Product.ID,
			LocatorCode = ComputeLocatorCode(orderLine.Product)
		    }
		);

		// make sure all products for this order
		// are in stock through the stock management
		// system
		if(StockApp.CheckAvailability(products)) {
		    
		    // mark the order as "shippable"
		    order.Status = "Shippable";
		}
	    }

	    // if we marked one or more orders as shippable, persist
	    // the changes in the store
	    orderTracking.SaveChanges();
	}

Sources

The ADO.NET Entity Framework

Entity Framework, LINQ and Model-First for the Oracle Database

Avoid the LINQ to SQL Data Objects vs. Business Objects Debate