Click here to Skip to main content
15,916,600 members
Home / Discussions / C#
   

C#

 
AnswerRe: Creating a small project using C# - Please Help Pin
Pete O'Hanlon3-Feb-14 0:18
mvePete O'Hanlon3-Feb-14 0:18 
QuestionRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 1:02
Mohan Subramani3-Feb-14 1:02 
AnswerRe: Creating a small project using C# - Please Help Pin
Richard MacCutchan3-Feb-14 0:22
mveRichard MacCutchan3-Feb-14 0:22 
QuestionRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 1:16
Mohan Subramani3-Feb-14 1:16 
AnswerRe: Creating a small project using C# - Please Help Pin
Richard MacCutchan3-Feb-14 2:18
mveRichard MacCutchan3-Feb-14 2:18 
GeneralRe: Creating a small project using C# - Please Help Pin
Mohan Subramani3-Feb-14 2:48
Mohan Subramani3-Feb-14 2:48 
JokeRe: Creating a small project using C# - Please Help Pin
Ravi Bhavnani3-Feb-14 6:15
professionalRavi Bhavnani3-Feb-14 6:15 
GeneralIssues in creating SQL Server Database with C# windows Application Pin
Mohan Subramani4-Feb-14 3:15
Mohan Subramani4-Feb-14 3:15 
GeneralRe: Issues in creating SQL Server Database with C# windows Application Pin
Ravi Bhavnani4-Feb-14 6:24
professionalRavi Bhavnani4-Feb-14 6:24 
AnswerRe: Creating a small project using C# - Please Help Pin
John D. Sanders4-Feb-14 9:13
John D. Sanders4-Feb-14 9:13 
QuestionArtificial Neural Network Pin
Member 95388301-Feb-14 23:34
Member 95388301-Feb-14 23:34 
AnswerRe: Artificial Neural Network Pin
Kornfeld Eliyahu Peter2-Feb-14 0:06
professionalKornfeld Eliyahu Peter2-Feb-14 0:06 
GeneralRe: Artificial Neural Network Pin
Member 95388302-Feb-14 0:50
Member 95388302-Feb-14 0:50 
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 

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.