Click here to Skip to main content
15,884,353 members
Home / Discussions / C#
   

C#

 
QuestionDiamond Program with few requirements Pin
balaramanN1-Feb-14 19:47
balaramanN1-Feb-14 19:47 
AnswerRe: Diamond Program with few requirements Pin
Mycroft Holmes1-Feb-14 20:20
professionalMycroft Holmes1-Feb-14 20:20 
AnswerRe: Diamond Program with few requirements Pin
Richard MacCutchan1-Feb-14 21:32
mveRichard MacCutchan1-Feb-14 21:32 
Questionproblem in crystal report Pin
Shuaib wasif khan1-Feb-14 10:31
Shuaib wasif khan1-Feb-14 10:31 
AnswerRe: problem in crystal report Pin
Mycroft Holmes1-Feb-14 20:19
professionalMycroft Holmes1-Feb-14 20:19 
AnswerRe: problem in crystal report Pin
John D. Sanders4-Feb-14 9:15
John D. Sanders4-Feb-14 9:15 
QuestionBest practise for using EF Entities Pin
Shady George1-Feb-14 10:13
Shady George1-Feb-14 10:13 
AnswerRe: Best practise for using EF Entities Pin
Dave Kreskowiak1-Feb-14 13:07
mveDave Kreskowiak1-Feb-14 13:07 
There simply isn't enough code here to make any real judgment of what you're doing. I can see some smallish things but nothing about any patterns you're using.

1) Your method is called "Return..." something. Personally, I always name methods that return something "Get...". Return is a bit more vague as to what it does. Does "Return" refer to retrieving a database object, or does it refer to some business rule method. It's the concept of "self documenting code". Method names should describe exactly what they do.

2) Entities (or DbContext) classes should be Disposed when you're done with them. It should be something like this:
C#
internal static Purchase GetPurchaseItem(int purchaseId)
{
    Purchase purchase = null;

    using (var context = new RegulusEntities())
    {
        return context.Purchases.Find(purchaseId);
    }
}


3) If you have a primary key Id for an object, you don't need an entire Linq query to find it. You just need to call the Find method to retrieve it. Find() will return the object with the specified key. If it's not found, it'll throw a InvalidOperationException. You can replace it with FirstOrDefault() if you want the "Get..." method to return a null of the specified type instead of throwing.

Other than that, a couple of rules I follow:

I don't have my database code return any ViewModel objects, not does it accept any. My database layer will accept and return Entity objects only. If these objects need to be translated into ViewModel objects, I can have another layer above this that does the translation, such as in an MVC Controller class or what I call a "Services" class.

I never expose my database layer directly to UI code.

GeneralMessage Closed Pin
2-Feb-14 9:51
cseder2-Feb-14 9:51 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak2-Feb-14 11:46
mveDave Kreskowiak2-Feb-14 11:46 
RantRe: Best practise for using EF Entities Pin
cseder2-Feb-14 15:19
cseder2-Feb-14 15:19 
GeneralRe: Best practise for using EF Entities Pin
Shady George2-Feb-14 15:50
Shady George2-Feb-14 15:50 
GeneralRe: Best practise for using EF Entities Pin
Shady George2-Feb-14 17:05
Shady George2-Feb-14 17:05 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak2-Feb-14 18:52
mveDave Kreskowiak2-Feb-14 18:52 
GeneralRe: Best practise for using EF Entities Pin
Shady George4-Feb-14 8:28
Shady George4-Feb-14 8:28 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak4-Feb-14 10:11
mveDave Kreskowiak4-Feb-14 10:11 
GeneralRe: Best practise for using EF Entities Pin
Shady George4-Feb-14 10:18
Shady George4-Feb-14 10:18 
GeneralRe: Best practise for using EF Entities Pin
Shady George5-Feb-14 10:07
Shady George5-Feb-14 10:07 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak5-Feb-14 10:49
mveDave Kreskowiak5-Feb-14 10:49 
GeneralRe: Best practise for using EF Entities Pin
Shady George5-Feb-14 10:59
Shady George5-Feb-14 10:59 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak5-Feb-14 15:17
mveDave Kreskowiak5-Feb-14 15:17 
GeneralRe: Best practise for using EF Entities Pin
Shady George6-Feb-14 9:38
Shady George6-Feb-14 9:38 
GeneralRe: Best practise for using EF Entities Pin
Dave Kreskowiak6-Feb-14 9:53
mveDave Kreskowiak6-Feb-14 9:53 
GeneralRe: Best practise for using EF Entities Pin
Shady George6-Feb-14 10:17
Shady George6-Feb-14 10:17 
GeneralRe: Best practise for using EF Entities Pin
Shady George6-Feb-14 11:50
Shady George6-Feb-14 11:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.