patterncsharpMinor
Basic Entity Framework Repository implementation
Viewed 0 times
basicrepositoryimplementationframeworkentity
Problem
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;
}
}
///
/// Getting Outbound list in selected period of dates
///
/// begin date
/// end date
/// Outbound List
public static List GetOutboundsByDate(DateTime dateFrom, DateTime dateTo)
{
var q = from o in Context.wms_Outbound
where o.CreateDT = dateFrom
select new OutboundModel
{
Id = o.Id,
Code = o.Code,
CreationDate = o.CreateDT,
RootQuantity = o.RootQuantity
};
return q.ToList();
}
}Solution
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:
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:
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:
This does what you seem to have had in mind. In your code, you never assign to
And the next thing that startles me is this:
in combination with the fact that your output is not sorted. Why? Using the Type
Instead I'd use
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 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.Code Snippets
if (context == null)
{
return new Entities();
}
else
return context;//if bla bla
else
Logger.log("Context accessed.", Log.DEBUG);
return context;if (context == null)
{
context = new Entities();
}
return context;public static List<OutboundModel> GetOutboundsByDate(DateTime dateFrom, DateTime dateTo)Context
StackExchange Code Review Q#55991, answer score: 8
Revisions (0)
No revisions yet.