Basic Entity Framework Repository implementation Here is simple repository pattern that I use to access data in my database. I would like some advice on how to improve this code. Commenting on my code quality is also welcome too! 

public static class ProdRepository
{
    private static Entities context = null;

    public static Entities Context
    {
        get
        {
            if (context == null)
            {
                return new Entities();
            }
            else
                return context;

        }
    }

    /// &lt;summary&gt;
    /// Getting Outbound list in selected period of dates
    /// &lt;/summary&gt;
    /// &lt;param name="dateFrom"&gt;begin date&lt;/param&gt;
    /// &lt;param name="dateTo"&gt;end date&lt;/param&gt;
    /// &lt;returns&gt;Outbound List&lt;/returns&gt;
    public static List&lt;OutboundModel&gt; GetOutboundsByDate(DateTime dateFrom, DateTime dateTo)
    {
        var q = from o in Context.wms_Outbound
                where o.CreateDT &lt;= dateTo &amp;&amp; o.CreateDT &gt;= dateFrom
                select new OutboundModel
                {
                    Id = o.Id,
                    Code = o.Code,
                    CreationDate = o.CreateDT,
                    RootQuantity = o.RootQuantity
                };

        return q.ToList();

    }

}
Very nice and comprehensive. Your names are clear and short, your code is obvious in it's intent. I just see two things that startle me a bit:


if (context == null)
{
   return new Entities();
}
else
   return context;



what you do here is ... amusing. This code doesn't do what you think it does ;)

Both blocks are single line-statements, yet you put the first one in brackets and the second one you don't. This is inconsistent and prone to bugs. Let's say you want to log when someone accessed the context without a new instance created.

The "logical", but wrong code would be:

//if bla bla
else
    Logger.log("Context accessed.", Log.DEBUG);
    return context;


While this would work as planned if your if did what you seem to think it does, the actual flow of code is incorrect.

That whole thing should be rewritten as:

if (context == null)
{
    context = new Entities();
}
return context;


This does what you seem to have had in mind. In your code, you never assign to context, which remains null until all eternity. With each call to the getter you just return a new instance of Entities().

And the next thing that startles me is this:

public static List&lt;OutboundModel&gt; GetOutboundsByDate(DateTime dateFrom, DateTime dateTo)


in combination with the fact that your output is not sorted. Why? Using the Type List , especially in combination with the name, IMO implies a sorting of the data set. In this case your sorting is the Id, but for GetOutboundsByDate, I expect a data set that is ordered by date (and not by Id)

Instead I'd use IEnumerable&lt;T&gt;.