List Variable in WinForm C# - c#

I'm still learning C# .Net 4 and this is my first WinForms so please be kind.
Continuing in my project, my financial DataFeed is streaming into my application by use of 'Asynchronous Sockets?'. Anyway, the data I am getting is tick per tick data, which is basically 1 order/transaction. So now I need to build bars with this tick by tick data, in particular Range Bars.
My problem is I don't want to go to the database and grab this data, so I am looking to do this in memory, like a list variable. Eventually, this system on the main server will do all the number crunching etc... and will have clients connected via Sockets to interrogate or set their own predefined algos on the in coming data and build their own charts using different ranges and indicators.
I wouldn't want to offload this to the client because I would like to keep the indicators technology proprietary.
How would I go about implementing this?
I already have my class called Tick
class Tick
{
public double Last { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public double BidSize { get; set; }
public double AskSize { get; set; }
public DateTime TimeStampInternal { get; set; }
public int DTNTickID { get; set; }
public int UpdateTypeID { get; set; }
}
I'm thinking of a
Static List<Tick> Ticks
but I don't think this is the way to go because
I need to be able to hold only a certain amount of ticks and as new data comes in, old data gets thrown away, FIFO to keep memory usage down.
I will only be able to hold 1 Static List and I need something dynamic, e.g. have a List for each user that connects which would be identifiable to them only.
Please help me architect this correctly with best practices for speed and efficiency.

Sounds like a circular buffer is what you're looking for.
http://circularbuffer.codeplex.com/
Or perhaps a queue.

I hope that I correctly understand what you want, so here is very pseudocode :
public class User {
private UserTickList<Tick> _userTicks = new UserTickList<Tick>();
public void AddUserTick(Tick t) {
_userTicks.Add(t);
}
/*remove, update if need*/
}
public class UserTickList {
private List<Tick> _list = new List<Tick>();
public void AddTick(Tick tick) {
if(_list.Count == 10){
/*perform what you need*/
}
else
_list.Add(tick);
}
}
I repeat this probabbly will not compile, but just to give an idea what it can look like.
Hope this helps.

Related

Refresh View in MVC asp.net

I have a view which has different sections displaying different type of orders from DB (SQL Server). Now I need to refresh view with updated information each time a new order is submitted through Android Application. Below are code snippets:
ViewModel:
public class KitchenViewModel
{
public List<Orders> DisplayOrders { get; set; }
public List<Orders> PreparedOrders { get; set; }
public List<OrderItem> ProgressItems { get; set; }
public List<OrderItem> QueuedItems { get; set; }
public int DisplayOrdCount { get; set; }
public int PreparedOrdCount { get; set; }
public int QueuedOrdCount { get; set; }
}
Controller:
public ActionResult KitchenOrder()
{
KitchenModel kitchenInstance = new KitchenModel();
List<Orders> orders = kitchenInstance.GetProgOrdersList();
List<OrderItem> progressItems = kitchenInstance.GetItemProgress();
List<OrderItem> queuedItems = kitchenInstance.GetItemQueued();
List<Orders> prepOrders = kitchenInstance.GetPrepOrdersList();
List<Orders> queuedOrders = kitchenInstance.GetQueuedOrdersList();
KitchenViewModel viewModel = new KitchenViewModel();
viewModel.PreparedOrders = prepOrders;
viewModel.ProgressItems = progressItems;
viewModel.DisplayOrders = orders;
viewModel.QueuedItems = queuedItems;
viewModel.DisplayOrdCount = orders.Count;
viewModel.PreparedOrdCount = prepOrders.Count;
viewModel.QueuedOrdCount = queuedOrders.Count;
return View(viewModel);
}
As of now I am auto refreshing view after every 15 seconds which is working perfectly.
But I need to refresh view only when a new order is submitted through Android application and order is inserted in DB. Once a new order is submitted the values for PreparedOrders, Progressitems, DisplayOrders gets changed and need to be fetched again. I have read many posts/tutorials relating to Observer pattern and publisher/subscriber method but unable to get crisp solution which would fit best. Could someone please provide relevant pointer/tutorial to use in such a scenario that could help. Being this my very first project and a total beginner, I m quite confused as in how to proceed.
So if you have to update site on event that fires when something changes in base, as other clients have changed it, you need PUSH based architecture, and not PULL based like you do it now (requests on timer elapsed).
For this purpose you can use SignalR, that implements various modern communication mechanisms. The basic idea is: one time client accessed your site, there is a persistent
connection created pointing to it's browser, and in the moment of notification you just roll over all available clients and notify them. On client side, naturally, event is handled in your case with javascript.
Worth mentioning that this technology has limitations across browser versioning, so refer to documentation to see if supported browser versions set satisfies your requirements.
Here is the link to supported platforms list for SignalR2: Supported platforms

RPG Doors / Events

Heeeey, on my RPG i'm working on i have a working tile engine which you can add layers, i have three layers:
Bottom Layer
Top Layer
Solid Layer
I have collision working and also the character animation working.
But how would i go making doors, that when you walk into them and have a key, it switches to another map?
I tried to add another layer called "EventLayer" but i don't know how to format it properly to work.. And things like Events - For example a falling book, sound effect...
Could someone please help me with this?
Thanks in advance! :)
Events like this will be a lot different based on what exactly you need in your game.
If you want events to only be doors and similar interactable objects, and have NPCs and monsters etc. in their own type, then events are not that tough to make.
You need to include every property you need to have for all your events, such as the position, graphic and trigger type. In your case, you want certain events only to function when certain items are in possession. Add a requirement list, and make sure everything in that list is met before executing the corresponding event.
When programming your event objects, you might want to make them universal, so you can create most (if not all) of your events from this instances of this class. Example of a quick event class mockup:
class Event {
public Texture2D Graphic { get; set; }
public Vector2 TileLocation { get; set; }
public List<Condition> Conditions { get; set; }
public TriggerType Trigger { get; set; }
public List<Command> Commands { get; set; }
public int CommandIndex { get; set; }
public bool Running { get; set; }
public bool Erased { get; set; }
public Event() { Erased = false; }
public void Update(GameTime gameTime){
if(Erased) return;
if(Running){
// continue command execution
}
else // check for triggering
switch(Trigger){ }
}
public void Draw()[
if(Erased) return;
// drawing code
}
}
The command class and the TriggerType enum should not be an issue.
I hope this helps you get what you wanted. It's a bit hard to tell you exactly how you should do it based on such little information. Good luck.

Stuck at how to proceed unit testing this business logic using tdd

I have a method that I give a list of car movements (Operation class) in a parking lot (Entries,Exits) each movement has a related cars list (OperationVehicle class). This method should result what I called cars log. It lists all cars, at what time entered what time exited and the duration for each one.
I am a bit lost on what things to test first. An answer to this question would be like a tutorial for me.
How can I proceed on unit testing this particular logic using tdd ?
public class Operation
{
public Operation()
{
OperationVehicles = new List<OperationVehicle>();
}
public int OperationId { get; set; }
public DateTime Date { get; set; }
public virtual OperationType OperationType { get; set; }
public virtual List<OperationVehicle> OperationVehicles { get; set; }
}
public class OperationVehicle
{
public int OperationVehicleId { get; set; }
public OperationType OperationType { get; set; }
public virtual Operation Operation { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class CarLog
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Vehicle Vehicle { get; set; }
public int StayLength { get; set; }
}
public IEnumerable<CarLog> GenerateCarsLog(List<Operation> CarStayOperations)
{
// not implemented yet
return new List<CarLog>();
}
What you have done so far is that you have modelled your domain in code (the relationships). But you haven't written much logic, so there's not much to test.
I wouldn't let your tests interact with these classes, at least not to start with. Rather, I would probably start somewhere around the user interface. Simulate the action that the user performs (and with user I don't necessarily mean a human being, but it could be another system). Test the behaviour, and do it as close to the boundaries of your system as possible.
Exactly what tests to write are hard to answer without more knowledge about the context. But I'll give it a try.
So what should the user be able to do with your system? For instance, write a first test where a car enters the parking lot and let the test assert in some way that the car is now in the parking lot.
But how would the user (or another user) know that the car successfully entered the parking lot? Is there, for example, some kind of user interface for the Car Log? Let your test's asserts look at that.
That would, however, be quite a large test to write, both including entering cars and checking the cars log. Perhaps better to start with the car log: just verifying that it is empty. Or perhaps there is an even smaller step you can start with?
My answer is a bit tentative, because that's what it is usually like: you learn and discover as you go.
A design note: Please don't expose lists from your classes (OperationVehicles). It totally breaks encapsulation. Add a Operation.AddOperationVehicle() method instead and return an IEnumerable<OperationVehicle>.

How to model entities to enforce a data models constraints at compile time?

I have the below data model that constrains ItemTypes with a subset of Events. Each ItemType has a valid set of Events, this is constrained in the ItemEvent table. For example, a Video can be { played, stopped, paused }, an Image can be { resized, saved, or shared }, and so on.
What is the best way to reflect this constraint in the Entity model so that I can get compile time assurance that an Event used is valid for a particular Item? Specifically, I am refactoring the AddItemEventLog method:
public void AddItemEventLog(Item item, string ItemEvent)
{
//
}
Obviously, this is a contrived example, just trying illustrate-- it allows a developer to pass in any ItemEvent string they desire. Even if I create an enumeration based on ItemEvent resultset, there isnt anything in the entity model to prevent a developer from passing in ItemEvent.Resize with an Item of type Video.
I have Item as the base class of Video, and I have tried to override an enum but now know that is not possible. I am less interested in checking for the validity of the Event at runtime, as I already will throw an exception when the DB raises a FK violation. I want to nip it in the bud at coding time if possible :)
Currently have classes modeled like this but open to any modifications:
//enums.cs
public enum ItemType : byte
{
Video = 1,
Image = 2,
Document = 3
}
//item.cs
public class Item : BaseModel
{
public int ItemId { get; set; }
public ItemTypeLookup.ItemType ItemType { get; set; }
public string ItemName { get; set; }
}
//video.cs
public class Video : Item
{
public string Width { get; set; }
public string Height { get; set; }
public string Thumb { get; set; }
}
I think that Code Contracts may be the only way to enforce something like this at compile time. Outside of compile time checks, writing unit tests to ensure the correct functionality is the next best thing!

javascript Highcharts object as a C# class

I've been working with javascript Highcharts and I made a basic 'Chart Builder' app. One of my goals is to have the user create and modify as many options as they like and save those to the db. The main problem I'm having is trying to convert the Highcharts object to a c# class. I've been building it slowly(ie manually) with the parts I need, as I need them, but to eventually get the whole thing converted will take a long time.
Ideally, I'd like to create and setup the whole highcharts options object server side and just send it 100% complete to highcharts
Is there any easy way to do this? Has anyone already done this?
Here is the Highcharts reference page: http://www.highcharts.com/ref/
and this is what I've done so far.
public class Highchart
{
public title title { get; set; }
public plotOptions plotOptions { get; set; }
}
public class title
{
public string text { get; set; }
}
public class plotOptions
{
public series series { get; set; }
}
public class series
{
public string stacking { get; set; }
public string borderColor { get; set; }
public bool shadow { get; set; }
public int borderWidth { get; set; }
}
As you can see, I just started ^_^
Update : The Highcharts .Net library has been updated in December, and is nearly feature complete as per V2.1.9 of the Javascript library.
The .Net library currently has support for multiple axes, point objects, viewstate management after postbacks, click events for points, series etc, and a built in implementation of an AJAX datasource ;) You don't need to write a single line of JS code unless you want to handle click events; you simply code in C#, and the appropriate JS is rendered automatically for you..
Click here to view the Live Demo

Categories

Resources