Business Logic In Application Or Database Level [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I want to determine where to write the business logic in application level or database server level. For example if I have simple procedure which sum the total price.What is the difference between to call the procedure with the logic to sum all of them and in application level to iterate each entity and make total sum after calling select *
Database level:
SELECT
SUM(sod.UnitPrice)
FROM Sales.SalesOrderDetail AS sod
OR
Application level:
foreach (var person IN context.GetAllPpl())
{
price += person.price;
}
What is the best approach for performance view of point ?

If your only goal here is to get the sum of UnitPrice, it's much more efficient to do the summation in your SQL query. This will always select exactly one row and one column, rather than every row and every column.
SELECT
SUM(sod.UnitPrice)
FROM Sales.SalesOrderDetail AS sod

Related

Filter database date in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In my database the date and time appear together with the data that was placed, but if I filter by date in C# as it has the time together I can't filter by date. Do I have to create a field for the date and another for the time?
...
WHERE med_date > '2021-06-08'
will get you all values that are after midnight at the start of today.
If you want to compute daily statistics then use CAST(... AS date) such as
SELECT CAST(med_date AS date) AS med_date, COUNT(*) AS n
FROM TheTable
GROUP BY CAST(med_date AS date)
ORDER BY 1
If you're using entity framework and LINQ to SQL queries, you can filter for date this way. Is that what you looking for?
dbcontext.DbSet<YouerEntity>.Where(x=>x.med_data.Date >= Date)

best method to if/else potentially large condition [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Working on an MVC application. I have hundreds of users. Currently I'm trying to give some of our top users (maybe 10) a discount if they log-in through their assigned work-place e-mails. The price break is shown in the Search result, Product page, Shopping Cart, and Checkout page. I had to work on this quickly to hack it for the holiday season, so currently the way I am doing this is something like this:
ProductDetails.aspx
if (user == "at#at.com") {
Product. Price * 20
}
else {
Product.Price
}
As you can see, this works for now, however I'd have to do this for all 4 pages, and as our discounted users increase, this may become too long and mundane. I'm looking at a way to go around having such a long if/else statement, and was wondering if it makes sense to use a stored procedure instead or a method?
I would suggest to add a discount field to the user table or any appropriate table in the database and save the discount info there. You can also create a new table that holds the discount information tied up to the different users and manage to return the discount value instead of going through an if/else statement.

Entity Framework - Assign parent object to object [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Let's suppose we have 2 tables:
Person
ID
Name
Nationality_ID (FK)
Nationality
ID
Name
With EF, in what contexts does it make sense and is correct to use each of the options below to add a nationality to a person? What is the different between them? What is the faster and the slower?
Option 1:
TheNationality.persons.Add(ThePerson);
Option 2:
ThePerson.nationality_id = TheNationality.id;
Option 3:
ThePerson.nationality = TheNationality;
If Person is the root of your aggregate and the focus of your application, most likely option 2 and option 3 make sense. Of those, option 3 is the more useful if you need to do additional domain logic based upon information in your nationality. Option 1 makes sense if the focus of your application is about nationality.
None of these methods is mutually exclusive. If you query and manipulate the objects from both perspectives, you can use options 1/3 or 1/2.
The resulting insert/update would be the same in all cases. Unless it is necessary to get TheNationality entity for some other reason, you could skip the read to obtain that and just assign the ID if you have it:
ThePerson.nationality_id = someNationalityIDVariable;

How can I show the total value of a database in a label? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database table called expenses. They are multiple columns such as food, gas, fuel etc all with numeric values. How can I generate the value of all the records added together and then to show the total in a label?
Thanks for any help!
I think you mean letting the database perform the sum for you. If that's the case then a query like this should do:
SELECT Food + Gas + Fuel AS Costs FROM Expenses
Should you want the total cost for all records? Then use SUM:
SELECT SUM(Food + Gas + Fuel) AS TotalCosts FROM Expenses

Unit Testing with ForEach [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Assuming:
"personList" is a list of Person objects
"agent" is an object that can provide activity about Shop objects (a property of Person)
"GetShopActivity" returns a list of shopping activity objects for a Person
I have this line in a test project:
personList.ForEach(p => new List<Person>(p.Shops)
.ForEach(t=> Assert.IsNotNull(agent.GetShopActivity(t, startDate, endDate))));
How can I make it better?
There is probably a lot you could do, but the first thing to do would be to make it more readable. Perhaps something like this:
var nullActivities =
from p in partnerList
from t in p.Tenants
let activity = agent.GetShopActivity(t, startDate, endDate)
where activity == null
select activity;
Assert.Empty(nullActivities);
Moreover:
you may want to think about
a test should be simple (i.e. it should have a Cyclomatic Complexity of 1).
it should be immediately evident to a person reading the test what scenario and behaviour is being tested (in case the values of startDate and endDate are significant it might be beneficial to give them less generic names).
prefer having only a single assertion as that makes it easy to know where the test failed when it fails.

Categories