Which design pattern will be best for football match app - c#

I am creating web api for application which support football team in collecting statistic data from matches. I am on implementing phase now. And lets say I am thinking what (if it is needed) type of design patter will be best for something like this:
public class Shot
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool OnGoal { get; set; }
public int GameId { get; set; }
}
and
public class Card
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool IsRed{ get; set; }
public int GameId { get; set; }
}
As You can see some properties are same. It should be implemented with interface, inheritance ( f.e. class Action) or maybe I should use one of Design patterns (which one)? What will be best for Entity Framework to avoid problems in later phases?

Well, both your classes represent some kind of game event - shot and card. There could be some other game events, like a free kick, throw in, substitution, penalty, or corner kick. All of these events should have id, game id, player id, timestamp, and probably comment. So your problem is the duplication of data in several classes. It is easily solved with inheritance. No patterns required:
public abstract class GameEvent
{
public int Id { get; set; }
public int GameId { get; set; }
public int PlayerId { get; set; }
public TimeSpan Time { get; set; }
public string Comment { get; set; }
}
And various specific events
public class Shot : GameEvent
{
public bool OnGoal { get; set; }
}
public class Card : GameEvent
{
public bool IsRed { get; set; }
}
You should also think about saving timestamp of added time because you can get both 46 minutes time span (start of second half) and 45+1 minutes of first half.

Related

.net6 dynamic option choices with Lists?

I am building a character creator for an out-of-print TTRPG system. The issue that I've run into is how to dynamically create dropdown boxes where the user can make skill/weapon choices based on their race/class choice.
For example: If user picks raceOne, he may pick any Lore skill he wants(he can do this twice), and he may choose one of any profession.
Now, Race two has some more interesting choices: He needs to pick between skillId 5 or SkillId 6, he also needs to make a decision between skillId 7 or 8.
My initial thought was to create a multi-dimensional array, and have array[x][] represent the number of choices one would have to make (RaceTwo in this example, has 2 different choices to make.) and choice[0][x] would represent one of the options. and then in my view I could just iterate through the array to create the dropdowns and their options, but it appears that arrays cannot be sized dynamically in c#.
Attached here is my RaceEntity, and the StartingSKills entity. I dont know enough about what I'm doing to know exactly what all code I should post here, but I'm trying to learn.
public class Race
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string? ImagePath { get; set; }
public virtual IList<RaceStartingArmor> RaceStartingArmor { get; set; }
public virtual IList<RaceStartingSkills> RaceStartingSkills { get; set; }
public virtual IList<RaceStartingTalents> RaceStartingTalents { get; set; }
public virtual IList<RaceStartingWeapons> RaceStartingWeapons { get; set; }
public virtual IList<RaceStartingGear> RaceStartingGear { get; set; }
}
public class RaceStartingSkills
{
public RaceStartingSkills(int RaceId, int SkillId , bool isKnown)
{
this.RaceId = RaceId;
this.SkillId = SkillId;
this.isKnown = isKnown;
}
public int Id { get; set; }
[ForeignKey(nameof(Race))]
public int RaceId { get; set; }
public Race Race { get; set; }
[ForeignKey(nameof(Skill))]
public int SkillId { get; set; }
public Skill Skill { get; set; }
public bool isKnown { get; set; }
public bool isTrained { get; set; }
public bool isSkilled { get; set; }
public bool isExpert { get; set; }
}
some more context: After race/class is chosen, the user is taken to a view where both race and class options are to be populated and chosen, this DTO will then be sent to the character sheet view and populated
any information, or video links, or anything just to get me moving in the right direction would be exceptionally helpful.

C# DTO List that consists of 2 other lists

Firstly, apologies if this seems basic, I am new to C#/dotnet and if the answer to this questions is somewhere obvious please point me in the right direction.
I have a DTO class with the following code
public class LessonDetailView : BaseResult
{
public long Id { get; set; }
public string Title { get; set; }
public List<LessonImagesListView> LessonImages { get; set; }
public List<LessonInstructionCardListView> InstructionCards { get; set; }
}
public class LessonImagesListView
{
public long Id { get; set; }
public string Title { get; set; }
public ImageDetailView Image { get; set; }
public LessonImagesListView()
{
Image = new ImageDetailView();
}
}
public class LessonInstructionCardListView
{
public long Id { get; set; }
public string Instructions { get; set; }
}
So I have 2 distinct types of object that I attach to the lesson and send to the frontend.
I will add that in the future I might have 6 different types of object.
These Images, or Instructions are also going to be displayed in a certain order on the front end so instead of sending them all separately I wanted to combine them all and send them in a new List LessonAssetsListView for example.
How can i create Lists in a DTO that combine 2 other lists ?
OR ... is this something I even need to do here ... and can i just do all this in my service.
Help appreciated.
You could simply define a type that composes both your existing and send a List of them
public class LessonAsset
{
public LessonImagesListView Image {get;set; }
public LessonInstructionCardListView Instruction {get;set;}
}
and then
public class LessonDetailView : BaseResult
{
public long Id { get; set; }
public string Title { get; set; }
public List<LessonAsset> LessonAssets { get; set; }
}

How to: Nested Lists in SQLite-Net Extensions

How to: Do nested lists in sqlite-net-extensions
Answer Found: Keeping the question as an example of how to do it.
The problem i encountered was not sqlite-net-extensions related, but i'm keeping the question for context.
[Old Question]
I've got a problem with TwinCoders SQLite-net extensions.
I'm trying to insert a Series object into my database:
I'm using the Db.InsertWithChildren(SelectedSeriesObject,recursive:true) method.
The Series object is added accordingly with it's attributes.
All the Episodes are added as well, no problems there.
The problem is the BaseSeason.
It will only insert one Season object, which is (for some reason) the last Season Object of the list of Seasons in the Series
public class BaseSeries : BaseMedia
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public int ShowId { get; set; }
public string FirstAirDate { get; set; }
public string LastAirDate { get; set; }
public string Status { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseSeason> Seasons { get; set; }
/// <summary>
/// TvShow = 0, Anime = 1
/// </summary>
public int SeriesType { get; set; }
}
public class BaseSeason
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(BaseSeries))]
public int SeasonId { get; set; }
public int SeasonNumber { get; set; }
public int NumberOfEpisodes { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public string AirDate { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseEpisode> Episodes { get; set; }
[ManyToOne]
public BaseSeries BaseSeries { get; set; }
}
public class BaseEpisode
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(BaseSeason))]
public int EpisodeId { get; set; }
public string Title { get; set; }
public string Plot { get; set; }
public string Poster { get; set; } //still path
public string AirDate { get; set; }
public int EpisodeNumber { get; set; }
public int SeasonNumber { get; set; }
public string SeriesName { get; set; }
[ManyToOne]
public BaseSeason BaseSeason { get; set; }
}
Is there anyone with experience regarding nested relationships in sqlite-net-extensions that knows how to make this work or see what i did wrong?
So regarding writing nested lists in sqlite-net-extensions:
My problem turned out the be related to how I handle the creation of these objects, this is by no means related to sqlite-net extensions. So my bad!
Which means that the questions example is valid and works. (I tested it of course)
Setting up the entities for the database:
The example shown in my question, with a Series class, Season class and Episode class, is the correct way of setting it up.
Inserting into the database:
If you're wondering how to insert an object similar to my Series object (with nested lists), use:
db.InsertWitchChildren(yourObject, recursion: true)
Here's an extended example:
public void AddSeries()
{
MediaDB.db.CreateTable<BaseSeries>();
MediaDB.db.CreateTable<BaseSeason>();
MediaDB.db.CreateTable<BaseEpisode>();
MediaDB.db.InsertWithChildren(SelectedSeries, recursion: true);
}
Side Note:
The example uses a static property on class with the connection string. Like so:
public class MediaDB
{
public static SQLiteConnection db => new SQLiteConnection(new SQLitePlatformGeneric(),"Media.db");
}
Refrain from doing this it is not really the best thing to do, since you should use using for the SQLiteConnection, making sure it's disposed once you're done with it.
more info on: sqlite-net-extentions
[UPDATE]: Further expansion of handling nested lists in sqlite-net extensions:
Deleting tables with children:
This is quite simple, but i spent a good hour and half figuring it out anyways.
Just use:
For lists/arrays: db.DeleteAll(yourCollection, recursion: true)
For single objects: db.Delete(yourObject, true);
As an exmaple: here's my implementation of a method that will delete a List
(BaseSeries is the class shown in the original question question):
public static void RemoveCollection<T>(List<T> collection)
{
using (db)
{
if (typeof(T) == typeof(BaseMovie))
{
db.DeleteAll(collection);
}
if (typeof(T) == typeof(BaseSeries))
{
db.DeleteAll(collection, recursion: true);
}
}
}
The BaseMovie class is a simple single entity, recursion is not needed since it holds no children.

Handling many times the same operation

I'm looking for a way to "divide" multiple operations that needs to be done so as to gain efficiency.
Let me put an example to explain my need.
Say that I have this object:
public class CardInfo
{
public int mCardID { get; set; }
public string mCardName { get; set; }
public int? mCardNumber { get; set; }
public string mCardColor { get; set; }
(...)
}
Each objects may be linked to a price list object:
public class PriceListInfo
{
public int mPriceListId { get; set; }
public int mPriceProviderID { get; set; }
public PriceProviderInfo mPriceListProvider { get; set; }
public int mCardID { get; set; }
public CardInfo mCard { get; set; }
public decimal? mPriceLow { get; set; }
public decimal? mPriceMid { get; set; }
public decimal? mPriceHigh { get; set; }
}
Now I have a process which task is to actually get a list of cards and build the price list of each cards, like this:
List<CardInfo> listCards = mCardManager.ListCardsForCardSet(_cardSetID);
foreach(var card in listCards)
{
List<PriceListInfo> listPrices = mPriceManager.ListPricesForCar(card);
}
Something quite simple, however if the list of cards contains more than 500 items, it will take a while. I am wondering (and since I don't know much about Asp.Net, it is why I am asking the question) if there's a way to divide the list by an [X] number and then launch many times the List<PriceListInfo> listPrices = mPriceManager.ListPricesForCar(card); line? Or is there a way to make these operation asynchronously? (please pardon my ignorance, thank you.) Can anybody explain how could I proceed, and if you could provide an example, I'd be more than happy.

How to change a class without re-coding?

I am creating a drivers license object in my project, that employees will all have one of their own linked to their unique clock number. I have a separate table in my database for the driving license but in the future more types of vehicles will need to be added, is there anyway to do this without re-coding?
the columns in my database are the same as the attributes for the class below
public class LicenseDTO
{
public int ClockNo { get; set; }
public bool CBalance { get; set; }
public bool MR16 { get; set; }
public bool OrderPicker { get; set; }
public bool Reach { get; set; }
public bool Pedestrian { get; set; }
public bool Lorry { get; set; }
public bool Sweeper { get; set; }
public bool Washer { get; set; }
}
EDIT
I have tried to create this the best I could but I feel like it's really long winded and can be done a more efficient way. Here's an updated version of my code.
public class LicenseDTO
{
public int ClockNo { get; set; }
public List<Common.VehicleTypeDTO> Vehicles { get; set; }
}
public class VehicleTypeDTO
{
public string VehicleType { get; set; }
public bool Allowed { get; set; }
}
private void btnClockCardIn_Click(object sender, EventArgs e)
{
Common.LicenseDTO License = new Common.LicenseDTO();
List<Common.VehicleTypeDTO> Vehicles = new List<Common.VehicleTypeDTO>();
Common.VehicleTypeDTO CBalance = new Common.VehicleTypeDTO();
Common.VehicleTypeDTO MR16 = new Common.VehicleTypeDTO();
License.Vehicles = Vehicles;
CBalance.VehicleType = "CBalance";
CBalance.Allowed = true;
MR16.VehicleType = "MR16";
MR16.Allowed = false;
License.Vehicles.Add(CBalance);
License.Vehicles.Add(MR16);
foreach (Common.VehicleTypeDTO Vehicle in License.Vehicles)
{
MessageBox.Show(Vehicle.VehicleType + " " + Vehicle.Allowed);
}
}
Why not to create a table with the types of vehicles? In the future you can access to your table and insert more types.
public class VehicleTypeDTO
{
public int Id{ get; set; }
public string Name{ get; set; }
}
public class LicenseDTO
{
public int Id { get; set; }
public List<VehicleTypeDTO> VehicleTypes { get; set; }
}
You should have made an entity LicenseDTO with the attributes ClockNo and CBalance alongside an array of the type Vehicle. which will be an interface. the interface Vehicle can define any common methods the vehicles have. and all future vehicles will have to implement the interface. that way you dont have to change any code. Your current code cannot be "changed" without editing. You could try to extend your LicenseDTO class with another entityclass which implements the above interface. but there isnt much more you can do without editing.
If you want maintainability use interfaces, repository patterns, abstract classes and dependency injection to start with.
Instead of having multiple bit columns in your database to indicate different types of vehicles, have a single VehicleType table. Then you can add as many different vehicle types as you like and use the VehicleTypeID to uniquely identify them. You can then add more and more vehicle types to the table without having to write more code.
VehicleType
VehicleTypeID int
VehicleTypeName varchar(50)
public class LicenseDTO
{
public int ClockNo { get; set; }
public int VehicleTypeID { get; set; }
}
If you want to have multiple types of vehicles against a single ClockNo then use a list of int:
public class LicenseDTO
{
public int ClockNo { get; set; }
public List<int> VehicleTypes { get; set; }
}
Alternately you could have a reference to the VehicleType objects instead of just the ID's.

Categories