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 have a search form that allows users to search real estate listings.
I currently have it set up as a basic html form that posts to a search results page.
On the search results page, I then use raw SQL and query the database and then use a repeater to display the results. I also create session variables on the query so if the user does another search they dont have to fill out the whole search, just edit it.
I was wondering if I should rather create a search class with a search object that gets created and edited with each search. Is this the best practice? or is my method above sufficient?
Thanks!
If there are lots of search parameters then I'd create a class to encapsulate them all and store that in session state, rather than maintaining lots of separate session variables. You might need to decorate this class with the SerializableAttribute depending on how you've got your session state configured, e.g.
[Serializable]
public class SearchOptions
{
public string Foo { get; set; }
public string Bar { get; set; }
}
Related
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 1 year ago.
Improve this question
Hi I'm a unity developer working on a bar manger game and I'm just wondering on the best way to implement a drinks system where it sets the value of the drink, number of servings in the barrel, name of the drink price of the whole barrel.
Here is some code I was working on before:
This is the interface method
public interface IDrinkSystem
{
string SetDrinkName(string nameToSet);
float SetDrinkValue(float drinkValue);
int AmmountOfServingsInBarrel(int Servings);
float PriceOfBarrel(float price);
}
This is the class method of doing it
public class DrinkSystem
{
public void NewDrink(string drinkName, float drinkValue, int barrelServings, float barrelPrice)
{
// Have getters and setters for all values in separate methods
}
}
What is the best way for making it easy to expand and at a push can I make an array of the NewDrink to store all the drinks i have or is there abetter way of doing this.
Use ScriptableObjects, as if they were files.
For every type of drink, have a ScriptableObject which you can fill in from Unity easily (even your artist can do this), and then at runtime, you load them into objects of a single DrinkEntity class which loads these values.
This way you don't end up with dozens or hundreds of "DrinkSystemBeer"/"DrinkSystemWhiskey"/etc classes, but you can still keep all your code clean and pattern-friendly.
IMPORTANT: NEVER operate with the ScriptableObjects directly. Simply load them into a DrinkEntity on its constructor. Treat ScriptableObject as if they were xml or json files from which you read your data.
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 2 years ago.
Improve this question
I am using Windows Forms .NET C# for this project
I am making a school project for a cafe ordering system. And the coffee aspect is a simple custom controller that allows users to pick out a size of the coffee, how many sugars and creams they want. My issue is that I then have to take that user control inputs and store them in the main form list and inside a ListBox as a display of an order.
Does anyone know how to store user control inputs into a main form List<>?
The best way is to create a class to store info about order, let's call it class "Order". Each order will be represented by instance of this class (you pass your control's values as parameters to the constructor). You can store orders in the List<Order>.
To add it to the ListBox, you must override ToString() method:
public override string ToString(){ return "onfo to show" }
This method will show info about order from your class in the way you like.
P.s. for more specific help provide sample code of what you have tried since now.
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 2 years ago.
Improve this question
I am trying to figure out the best way to change an existing class.
So the class is called ExcelReport and it has one method Create(data,headings). This is live and used in many places. Now recently I want to change the method so I can format columns in Excel.
Create(data, headings, columnformats)
So as not to upset my existing programs the best I can come up with is to add another method Create2(data,headings,columnformats) to the class.
I got a lot of suggestions saying I should modify the existing class with a overloaded method, which I did. But does this not break the Open/Close Principle as my existing class was in production?
Should I have created a new class ExcelReport2(and Interface) with the new improved method and passed this into my new program using dependency injection?
OCP
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification";[1] that is, such an entity can allow its behaviour to be extended without modifying its source code.
Your solution
You will most likely want to create more options later on for this.
And since you asked for an open/closed principle answer we need to take that into account (open for extension, closed for change).
A more robust alternative is to create a new overload:
void Create(CreationOptions options);
Looks trivial, right? The thing is that any subclass can introduce their own options like MyPinkThemedFormattedCellsCreationOptions.
So your new option class would look like this as of now:
public class CreationOptions
{
public SomeType Data { get; set; }
public SomeType Headings { get; set; }
public SomeType[] ColumnFormats { get; set; }
}
That's open for extension and closed for change as new features doesn't touch the existing API, since now you only have to create sub classes based on CreationOptions for new features.
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
Say I have a base method GetSong() that fetches songs from a database, and two methods that call this - GetRockSong() and GetPopSong().
Both methods take a string input of the song name, then pass that input to the GetSong() method, along with a genre.
Should the base method be in charge of validating the string input?
I would think the first two methods should, but this would lead to repeating the exact same code (i.e. checking the string isn't empty).
Obviously I have used a hugely simplistic illustration, but the problem is pretty much the same.
Example code:
public Song GetRockSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Rock");
}
public Song GetPopSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Pop");
}
public Song GetSong(string title, string genre)
{
// example validation, if null checking title above
// then could just check genre here
if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(genre))
{
// fetch song logic here
}
// etc
}
I personally think if the validation is the same then do the check in the shared method.
This will allow for you to maintain the code easier and it is always best to have the method that does the heavy lifting also validate the values passed to it.
I have done similar thing and found that the amount of code I needed to write and maintain is half of what it could have been if I had put the validation in each method that called it.
I hope this helps!
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
I've got the following model class:
public class Product
{
public int ProductID {get;set;}
public string ProductName {get;set; ]
public int ActiveOrdersCount {get;set;}
public Category[] Categories {get;set}
//etc...
}
When I load a product from the database, I load all the properties and maybe lazy-load the categories.
Does it make more sense to load all the properties of the object or partial etc?
It depends on how the objects will be accessed at run time. If you want to immediately access the categories collection for all of the products in a collection, then lazy loading will be very chatty.
On the other end of the spectrum if you only want to hit the Categories property for a small subset of the returned values, lazy loading might be beneficial.