Passing Database Name or the Whole Database as Variable

Multi tool use
I am refactoring my code by extracting functions.
Originally I have one huge main function which immediately gets the database using the dbName passed in from outside at the very first line and then does some other things based on the database.
Now I want to make those other things some small functions called by the now-smaller main function.
Those small functions need to access the same database.
Do I -
pass the dbName every time calling a small function and get the database inside it
- this seems somewhat redundant, if I have n small functions then I need to get the database n times
pass the whole database into the small function and use it right away
- would this cause performance issues?
And another question:
I know we can use
var table = database.GetCollection<table>()
to get and store the table into a variable so we can use it instead of getting it again every time we wanna find something.
But does it work when the action I wanna perform is modifying?
If I use table
to insert and then find, can I get the record I just inserted?
I mean, does table
mean the table before I inserted?
c# database linq variables
add a comment |
I am refactoring my code by extracting functions.
Originally I have one huge main function which immediately gets the database using the dbName passed in from outside at the very first line and then does some other things based on the database.
Now I want to make those other things some small functions called by the now-smaller main function.
Those small functions need to access the same database.
Do I -
pass the dbName every time calling a small function and get the database inside it
- this seems somewhat redundant, if I have n small functions then I need to get the database n times
pass the whole database into the small function and use it right away
- would this cause performance issues?
And another question:
I know we can use
var table = database.GetCollection<table>()
to get and store the table into a variable so we can use it instead of getting it again every time we wanna find something.
But does it work when the action I wanna perform is modifying?
If I use table
to insert and then find, can I get the record I just inserted?
I mean, does table
mean the table before I inserted?
c# database linq variables
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
1
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
1
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49
add a comment |
I am refactoring my code by extracting functions.
Originally I have one huge main function which immediately gets the database using the dbName passed in from outside at the very first line and then does some other things based on the database.
Now I want to make those other things some small functions called by the now-smaller main function.
Those small functions need to access the same database.
Do I -
pass the dbName every time calling a small function and get the database inside it
- this seems somewhat redundant, if I have n small functions then I need to get the database n times
pass the whole database into the small function and use it right away
- would this cause performance issues?
And another question:
I know we can use
var table = database.GetCollection<table>()
to get and store the table into a variable so we can use it instead of getting it again every time we wanna find something.
But does it work when the action I wanna perform is modifying?
If I use table
to insert and then find, can I get the record I just inserted?
I mean, does table
mean the table before I inserted?
c# database linq variables
I am refactoring my code by extracting functions.
Originally I have one huge main function which immediately gets the database using the dbName passed in from outside at the very first line and then does some other things based on the database.
Now I want to make those other things some small functions called by the now-smaller main function.
Those small functions need to access the same database.
Do I -
pass the dbName every time calling a small function and get the database inside it
- this seems somewhat redundant, if I have n small functions then I need to get the database n times
pass the whole database into the small function and use it right away
- would this cause performance issues?
And another question:
I know we can use
var table = database.GetCollection<table>()
to get and store the table into a variable so we can use it instead of getting it again every time we wanna find something.
But does it work when the action I wanna perform is modifying?
If I use table
to insert and then find, can I get the record I just inserted?
I mean, does table
mean the table before I inserted?
c# database linq variables
c# database linq variables
edited Dec 28 '18 at 5:03
Dale Burrell
2,86432248
2,86432248
asked Dec 28 '18 at 3:25


anniex
214
214
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
1
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
1
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49
add a comment |
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
1
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
1
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
1
1
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
1
1
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49
add a comment |
1 Answer
1
active
oldest
votes
Your options 2 is most likely the way to go given the information you have provided.
You will not be passing the entire database into the function, just a reference to it as most objects are passed by reference, not by value (unless they are a ValueType
).
table
should be a reference to the table (like DbSet<tablename>
) , not the table in its current state (if you want that you need to call table.ToList()
which should return List<tablename>
and store that instead.)
So calling table.Add(object)
and then table.Find(object.ID)
should return the object as it current is in the database
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953336%2fpassing-database-name-or-the-whole-database-as-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your options 2 is most likely the way to go given the information you have provided.
You will not be passing the entire database into the function, just a reference to it as most objects are passed by reference, not by value (unless they are a ValueType
).
table
should be a reference to the table (like DbSet<tablename>
) , not the table in its current state (if you want that you need to call table.ToList()
which should return List<tablename>
and store that instead.)
So calling table.Add(object)
and then table.Find(object.ID)
should return the object as it current is in the database
add a comment |
Your options 2 is most likely the way to go given the information you have provided.
You will not be passing the entire database into the function, just a reference to it as most objects are passed by reference, not by value (unless they are a ValueType
).
table
should be a reference to the table (like DbSet<tablename>
) , not the table in its current state (if you want that you need to call table.ToList()
which should return List<tablename>
and store that instead.)
So calling table.Add(object)
and then table.Find(object.ID)
should return the object as it current is in the database
add a comment |
Your options 2 is most likely the way to go given the information you have provided.
You will not be passing the entire database into the function, just a reference to it as most objects are passed by reference, not by value (unless they are a ValueType
).
table
should be a reference to the table (like DbSet<tablename>
) , not the table in its current state (if you want that you need to call table.ToList()
which should return List<tablename>
and store that instead.)
So calling table.Add(object)
and then table.Find(object.ID)
should return the object as it current is in the database
Your options 2 is most likely the way to go given the information you have provided.
You will not be passing the entire database into the function, just a reference to it as most objects are passed by reference, not by value (unless they are a ValueType
).
table
should be a reference to the table (like DbSet<tablename>
) , not the table in its current state (if you want that you need to call table.ToList()
which should return List<tablename>
and store that instead.)
So calling table.Add(object)
and then table.Find(object.ID)
should return the object as it current is in the database
edited Dec 28 '18 at 3:48
answered Dec 28 '18 at 3:42
Minijack
178116
178116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953336%2fpassing-database-name-or-the-whole-database-as-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3ud5fgcBah,LzD3dT,AGHnCaXJWBo8Gm,xIFh0CvfsKE
What are you using to access the database? Entity Framework?
– Minijack
Dec 28 '18 at 3:40
1
I think your question doesn't fit well to this site. Here, on StackOverflow, we usually deal with "not working" code (at least "working not like expected"). If your code is actually works and you want a code-review - you may try another site.
– vasily.sib
Dec 28 '18 at 3:44
1
This is a good way to get started dividing your code into areas of functionality. I think you are looking for a Data Access Layer. A separate class with static methods that take care of all communication with the database. The size of your data will dictate how much you can store locally in data sets and data tables.
– Mary
Dec 28 '18 at 3:49