Archive

Posts Tagged ‘Design Patterns’

Entity Framework, Repositories, Specifications and Fetching Strategies Part 8.0

February 22nd, 2009 Simon Segal 1 comment

organise_puzzleThis thread of posts is becoming unwieldy in it’s naming convention and difficult to follow if you have come to it very recently. To help rectify or rather improve that situation I have created this linking post which has taken the physical posts that make up the series and logically renamed them here. I will follow in the next couple of days with Part 9.0 which will address strongly typed FetchingIntentions using lambda expressions (at the suggestion of K. Scott Allen). Part 10 will cover dependency injecting the Fetching Strategies by expressing intent via roles (specifying an interface).

Entity Framework, Repositories, Specifications and Fetching Strategies Part 1.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 2.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 3.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 4.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 5.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 6.0

Entity Framework, Repositories, Specifications and Fetching Strategies Part 7.0

Share/Save/Bookmark

Durable Queue’s for Silverlight?

February 16th, 2009 Simon Segal No comments

Ayende created Rhino.Queues.Storage.Disk a while ago and followed with Rhino.Queues and left it to the community to join them together. I would love to have transactional ‘store and forward‘ available as a design choice that incorporates Silverlight and I’m wondering if these two abandoned children of Ayende’s might provide the basis of an answer? Maybe not. I can see that wrapping Isolatedqueue Storage read / write operations with some kind of veneer might be useful however really what’s required here is a solution that enlists in transactions (System.Transaction) thus  making our ’store and forward’ pattern safe and durable. Offline Silverlight with MSMQ? Perhaps, but for now that’s a hack. What about localhost WCF with a database behind it? Sure there are ways and means and somehow this is something I don’t see Microsoft investing in as it’s probably a bit too ‘fringe’ as a requirement, so it’s probably going to be up to the community. Personally I will probably wait till Silverlight is working offline (by design) and reconsider durable storage with Silverlight until then. In the meantime however I am absolutely loving working with XAML based UI in both Silverlight and WPF and going back to Windows Forms is not something likely to ever happen. Oddly, when I open a WinForm application in VS.Net I get the strangest feeling, almost like the one I used to have when opening the VB6 IDE after having developed in .NET for a few years. Must be sign?

Share/Save/Bookmark

The Entity Framework and a unified Lazy and Eager loading framework.

February 11th, 2009 Simon Segal 13 comments

I am in the middle of spiking a solution that aims to unify an approach to lazy and eager loading with the Entity Framework. I was ready to give up on the idea a while back but I am a stubborn PITA. It has been my goal for a while to try get Specifications, Repositories and (finally) Fetching Strategies bundled into a single approach for the Entity Framework. What I want to be able to do is write code like this:

public void Multi_Level_Mixed_Hierarchy_With_Persisting_Test()
{
    EntitiesRepository<Customer, NorthwindEntities> repos =
        new EntitiesRepository<Customer,
            NorthwindEntities>(new NorthwindEntities());

    Specification<Customer> german_cust_spec =
        new Specification<Customer>(c => c.CustomerID == “ALFKI”);

    var german_custs = repos.AllAsIList(“Customers”, german_cust_spec, new
                        MultiLevelMixedStrategy());

    foreach (var cust in german_custs)
    {
        cust.Country = “Italy”;
        Console.WriteLine(“The Customer Name is {0}”, cust.CompanyName);
        foreach (var order in cust.Orders)
        {
            order.ShipName = “The big boat”;
            Console.WriteLine(“\tThe Order ID is : {0}”, order.OrderID);
            foreach (var orderline in order.Order_Details)
            {
                orderline.Discount = 0.13f;
                Console.WriteLine(“\t\tThe value ordered for ” +
		    “Product ID {0} is {1}”,
                    orderline.Product.ProductID,
                    orderline.UnitPrice * orderline.Quantity);
            }
        }
    }

    repos.Session.SaveChanges();
}

The Repository and the Specification aspects to this unified approach have previously been developed and documented but the missing piece that has been outstanding is the Fetching Strategy. You can see above in the call to the repository .AllAsIList() method, that the third argument is the Fetching Strategy of which I speak.

public interface IFetchingStrategy
{
    IList<FetchingIntention> Intentions { get; }
}

public interface IFetchingStrategy<TRole> : IFetchingStrategy { }

And the implementation is:

public class MultiLevelMixedStrategy :
    IFetchingStrategy
{
    private readonly IList<FetchingIntention> _intentions =
        new List<FetchingIntention>();

    public MultiLevelMixedStrategy()
    {
        this.Intentions.Add(new FetchingIntention(“Orders”,
                                FetchMode.Eager));
        this.Intentions.Add(new FetchingIntention(“Orders.Order_Details”,
                                FetchMode.Eager));
        this.Intentions.Add(new FetchingIntention(“Product”,
                                FetchMode.Lazy));
    }

    public IList<FetchingIntention> Intentions
    {
        get { return _intentions; }
    }
}

Where the Entity Model looks like this:

ef_with_box_and_dice

So far the results look promising. I will have more to say on this shortly and with a bit of luck will be releasing the code which rounds out the triad of a Repository, dynamic querying capability via Specifications and Fetching Strategies for a unified approach for lazy and eager loading that does NOT require you ever explicitly call the .Includes(), Load() or IsLoaded methods and property on the EntityCollection<T>, EntityReference<T> or ObjectQuery<T> objects.

Share/Save/Bookmark

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia