Friday, October 19, 2012

Enhancing Your Entity Repositories

Using a repository pattern for data access gives you clear separation and abstraction in your data layer.  The basic idea of adding repositories for data access is illustrated nicely on MSDN:



In the case of an MVC application, your controllers fit into the "Client Business Logic" area, and your Database resides in the "Data Source" area.  What's left is the repository itself, here is a typical example generated using the MVC Scaffolding project.


namespace DataModel.Models
{
    public class PartyRepository : IPartyRepository
    {
        public IQueryable<Party> All
        {
            get { return _context.Parties; }
        }
        public IQueryable<Party> AllIncluding(params Expression<Func<Party, object>>[] includeProperties)
        {
            IQueryable<Party> query = _context.Parties;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }
        public Party Find(int id){ return _context.Parties.Find(id);}
       
  //Methods removed for brevity
    }
    public interface IPartyRepository : IDisposable
    {
        IQueryable<Party> All { get; }
        IQueryable<Party> AllIncluding(params Expression<Func<Party, object>>[] includeProperties);
        Party Find(int id);
        //Methods removed for brevity
    }
}



The repository itself abstracts the dirty work of dealing with the context, and provides a great element of re-usability in your application (you can inject a repository anywhere you like and it will use the same context code).  Here are some sample use cases for some of the above methods:


_repository.Find(id); //simple lookup
_repository.All; //get everything
_repository.AllIncluding(model => model.Property1, model => model.Property2); //Get all and include some navigation properties
_repository.AllIncluding(<insert ALL navigation properties>); //Full eager fetch



Simple enough, but this structure raises some concerns:

  1. Specifying the navigation properties in your controller actions when using the repositories feels like a violation of your abstractions.  Dealing with relationships between data driven objects should stick to the data layer as much as possible.
  2. There really isn't a good method to do a full eager fetch in this scenario.  You could provide all of the include properties but that creates a maintainability issue when you add a new navigation property and have to change every location you used the including options.

Our solution to this issue was a small and simple re-working of these methods, here is an example from another object:

namespace DataModel.Models
{
    public class DocumentRepository : IDocumentRepository
    {
        private readonly Expression<Func<Document, object>>[] _allIncludes =
            {
                d => d.Department,
                d => d.Organization,
                d => d.DocumentStatus,
                d => d.DocumentType,
                d => d.FavoriteUsers
            };
        public IQueryable<Document> All(params Expression<Func<Document, object>>[] includeProperties)
        {
            IQueryable<Document> query = _context.Documents;
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query.Where(doc => doc.Organization.AccountId == accountId);
        }
        public IQueryable<Document> All(bool eager, params Expression<Func<Document, object>>[] includeProperties)
        {
            var includes = eager ? _allIncludes : includeProperties;
            return All(accountId, includes);
        }
        public Document Find(int id, params Expression<Func<Document, object>>[] includeProperties)
        {
            IQueryable<Document> query = _context.Documents;
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query.SingleOrDefault(doc => doc.Id == id);
        }
        public Document Find(int id, bool eager, params Expression<Func<Document, object>>[] includeProperties)
        {
            var includes = eager ? _allIncludes : includeProperties;
            return Find(id, includes);
        }
  //Methods removed for brevity
    }
    public interface IDocumentRepository
    {
        IQueryable<Document> All(params Expression<Func<Document, object>>[] includeProperties);
        IQueryable<Document> All(bool eager = false, params Expression<Func<Document, object>>[] includeProperties);
        Document Find(int id, params Expression<Func<Document, object>>[] includeProperties);
        Document Find(int id, bool eager = false, params Expression<Func<Document, object>>[] includeProperties);
        //Methods removed for brevity
    }
}

We simply added an eager option and an overload for the Find and the All(converting it from property to method).  Here are the new use cases:

_repository.Find(id); //simple lookup
_repository.All(); //get everything - lazy loaded
_repository.All(model => model.Property1, model => model.Property2); //Get all and include some navigation properties
_repository.All(true); or the more readable: _repository.All(eager:true); //Full eager fetch

With this modification, controlling the type of fetch you want to do is much more clear, and if we add navigation properties to the model, we only need to update the allIncludes property in the repository, not everywhere the repository is used for eager fetching.  We also still preserved the ability to lazy load, as well as specify exactly the properties you want during a fetch.

A side effect though, is we have some strange edge cases that result, for example:

_repository.All(eager:true, model => model.Property1);

In this case, the provided property is ignored and all properties are fetched.  We chose to lay the blame for this sort of issue on the developer as there are easier ways to use the methods to achieve the desired result, whatever that may be.


All code examples taken from our next version of Contract Guardian.

For more information, check out our Web Site.


Tuesday, September 18, 2012

Common 2012 Fall Conference & Expo



This three-day Power Systems educational and networking event will be packed with over 100 educational sessions on a large variety of topics, including vendor-led sessions, and pre-conference workshops. As expected from all COMMON conferences, attendees will have the opportunity to further enhance their education through numerous networking events and meeting with leading solution providers in the tabletop-style expo.
Visit the COMMON Web site for more information.

Best of all: Two of my favorite vendors will be exhibiting at the Conference.  LANSA & VAULT400

Thursday, March 8, 2012

Microsoft Visual Studio 2010

Scott Guthrie's Blog on Microsoft ASP.NET technologies including the new MVC4 update is excellent.  You may want to take a look at this blog because it offers so much information on Microsoft Development technologies.  Scott lives in Seattle and builds products for Microsoft.  See Scott take you through the ASP.NET MVC4 Beta.  Just click here to view the video. It contains a lot of excellent information.

In summary the presentation covers the new features in MVC4 that provide a rich set of enhancements to this tool set that is accessible from within Microsoft Visual Studio 2010.  MVC4 comes built-into VS2011 as well.  It does install side by side with MVC2 and MVC3 in the same Visual Studio installation on your development workstation.
  • Bundling/Minification Support - Improves performance on your website. 100% Automatic
  • Database Migrations - Allows production database schema to be automatically updated.
  • Web APIs - Great support in VS for creating Web APIs. Easily create HTTP Services.
  • Mobile Web -  Improved developer support for developing Mobile applications for the phone and tablet.
  • Real Time Communications - Allows for Client to Server Persistent connections over HTTP using SignalR.
  • Asynchronous Support - Reduces the number of threads and server resources; Increases scaleability.
If you need a web application developed for your business, just contact us by calling 513-977-4544 or click here to learn more.

Tuesday, February 21, 2012

LANSA 2012 User Conference



As many look forward to the release of LANSA 13.0 it is sometimes rewarding to reflect on the history of LANSA.  The early years were filled with concepts like:


  • Data Repository
  • 4th Generation Language
  • Multilingual
  • Templates
  • Database Triggers
You could recap all of the commands/parameters on a simple quick reference card.
Marketing was generally limited to print media.  (Remember printed trade rags?)
Presentations were given with slide projectors.
You actually had to fly somewhere to present.
Yes LANSA and the industry have come a long way.

Fast forward and we look forward to the conference that will feature:
  • Cloud Based Labs (Bring your laptop)
  • Web & Mobile App development
  • Business Objects
  • Some of the famous and familiar speakers in the LANSA community:
    • Diane Joester
    • Mark Duignan
    • Don Nelson
    • Madan Divaker
    • David Brault
    • and many others
  • Opportunity to network
If you have not registered or are just interested.  Click Here.

Tuesday, November 15, 2011

LANSA Powers up Windows Interoperability

I read in System i NETWORK that LANSA has signed up 50 British i users to a free one-day conference on Windows interoperability in December.  Those of us that have used LANSA for many years readily appreciate the ability to work with and among multiple platforms via a single tool set.  Read Article.

Wednesday, July 13, 2011

Mobile Applications....Go Android!

In the past six months, as Android has gained momentum and market share. Last week, comScore reportedthat in the three months ending in May, Android was the top smartphone platform in the U.S. with 38 percent of existing devices. It grew faster than iOS, which reached 26.6 percent, while Research In Motion's BlackBerry platform fell to 24.7 percent, comScore said.   Good News: aXes and LANSA Wams can deliver to both!


Footnote: Is it a just a coincidence that the Android is colored Green like LANSA?

Thursday, May 19, 2011

Cross Browser Compatibility and LANSA aXes


With the new release of aXes version 2.1 (Beta version) all the major browser types are supported.  In prior versions, the aXes product suite only supported the Microsoft (MS) IE browser.  Now aXes supports the following browsers: MS-IE, Firefox, Google Chrome, and Safari.  All of these browsers combined represent a huge user base.  Opening up aXes to all of these types of browsers vastly broadens the user community for who can use aXes and thus, iSeries applications via the web.  The only browser not sanctioned currently is Opera.  It’s officially unsupported but it executes with this new version.  Opera is a Java based browser so it’s very popular on mobile devices.

For more information or to download a free 30 day evaluation copy of aXes, please visit our website.  For further questions on aXes or other LANSA products, contact us at 1-800-887-1834 or request information by clicking here.