Design Pattern for Data-Centered Application - c#

I am having a little trouble deciding how to structure an application that I am building.
The application is required to parse through text-based files (one record per line) and load all of the information into memory. There are typically anywhere from 100,000 - 500,000 records. After that, the data must be displayed in the form of tables and very detailed graphs/charts for further analysis.
The user must have the ability to customize the view of this data. For example, there are many different "types" of log records (TypeA_Log, TypeB_Log, etc.) The user should have the ability to select/deselect these types, which will show/hide them from the display. There are several other types of filtering going on (filter by date, etc.)
--
Essentially what I have now is this:
I have a class structure that represents the various types of log records.
There is a Singleton pattern to hold all of the data (almost like a database in memory), which includes Lists<> of log record instances.
Then, I have a "filter" class which contains information about what the user would like to view. The "filter" gets passed into the Data-Singleton, and then returned a subset of the data. I am basically using C# lambda expressions to select data from a List<> of Log Records, similar to how you might pass in a SELECT statement to a SQL database.
--
This is working okay, but I have to think there is a better way of handling this. Right now, anytime the user makes even the smallest change (for example, selects/deselects one of the many types of log records, a new data set has to be retrieved from the Data-Singleton, and then all of the tables and charts have to be refreshed/rebinded/recomputed to reflect this change. All I really want to do, in most cases, is show/hide certain things.
I am using C# .NET 3.5.

You could build some sort of internal notification system, something also called event dispatcher.
Lets say have a record changed in one form, and instead of reloading other dependent forms, you fire the event that some record has changed (attach the record with some custom EventArgs), and let your dependent forms decide on event handling should they include this new record somewhere. You could update the changed data in the collections of the dependent forms.
However, this can create some concurrency problems (when you avoid rooundtrips to the database (text files in your case) )...

Related

Database table with single field

I have a Lightswitch project written in C# with Silverlight. I've been thinking and looking for a professionnal way to deal with single entities/objects that must be modifiable by users, and having those changes be reflected for every other user. Those entities don't belong to any table because they don't share similar fields, and obviously I wouldn't want to create a table for each one of them (about 10-20).
An example would be a multiplying factor for some specific pricing. That factor must be the same for every user, but the users must be able to change it after some time because pricing structure changes.
What would be an efficient way to deal with those ?
More info on the project:
Users will want to be able to go to a specific screen, change the value of the entity in a textbox for example (could be any other UI element though), and then some other entities would use that new value for their computed properties.

Designing app to load, edit, and save hierarchical data

I am writing a GUI that will be integrated with SAP Business One. I'm having a difficult time determine how to load, edit, and save the data in the best way possible (reliable, fast, easy).
I have two tables which are called Structures and StructureRows (not great names). A structure can contain other structures, generics, and specifics. The structures rows hold all of these items and have a type associated with them. The generics are placeholders for specifics and the specifics are an actual item in inventory.
A job will contain job metadata as well as n structures. On the screen where you edit the job, you can add structures and delete structures as well as edit the rows underneath them. For example, if you added Structure 1 to Job 1 and Structure 1 contains Generic 1, the user would be able to swap Generic 1 for a Specific.
I understand how to store the data, but I don't know what the best method to load and save the data is...
I see a few different options:
When someone adds a structure to a job, load the structure, and then recursively load any structures beneath it (the generics and specifics will already be loaded). I would put this all into an Object Model such as List and each Structure object would have List and List. When I save the changes back to the database, I would have to manually loop through the data and persist the changes.
Somehow load the data into a view in SQL and then group and order the datatable/dataset on the client side. Bind the data to a GridView so changes are automatically reflected in the dataset. When you go to save, SQL / ADO.NET could handle this automatically? This seems like the ideal solution, but I don't know how to actually implement it...
The part that throws me off is being able to add a structure to a structure. If it wasn't for this, I would select the Specifics and Generics from the StructureRows table, and group them in the GUI based on the Structure they belong to. I would have them in a DataTable and bind that to the GridView so any changes were persisted automatically to the DataTable and then I could turn around and push them to SQL very easily...
Is loading and saving the data manually via an object model the only option I have? If not, how would you do it? I'm not sure if I'm just making it more complicated then it needs to be or if this is actually difficult to do with C#, ADO.NET and MS SQL.
The HierarchyID datatype was introduced in SQLServer 2008 to handle this kind of thing. Haven't done it myself, but here's a place to start that gives a fair example of how to use it.
That being said, if you aren't wedded to your current tables, and don't need to query out the individual elements (in other words, you are always dealing the job as a whole), I'd be tempted to store the data for each job as XML. (If you were doing a web-based app, you could also go with JSON.) It preserves the hierarchy and there are many tools in .NET for working with XML. There's also a built-in TreeView class for winForms, and doubtless other third party controls available.

.NET Validation pattern

I've never looked much into all what .NET offers for user input validation because to start with I dislike the way they will typically not let you unfocus a control unless you enter the right data (I believe the DataGridView does this).
On the other hand, I found that I often need to validate what I'll describe below and I wonder if sticking to .NET standards here will make it any easier.
I'll typically have a dialog box that among other controls will have two combo boxes: one to select a data table among existing tables, and one to select a column among the columns in the currently selected table. This is easy enough so far, but since this is a dialog, I need to show the values that were selected the last time the dialog was shown if they still exist in the database, or otherwise select some other column if the table still exists, or select another table and column if there is any table and warn the user that his selection has changed, or if there are no tables simply show a message and close the dialog.
Of course this is not the only case. Sometimes it will be a bit more complex and every time I will try to figure out again what's the best way to handle it. I wonder if there is already a pattern, particularly one that .NET offers I can apply to the case I describe above? If so, I'm sure I'll figure out how to apply it to other cases.
The answer will depend quite a bit on your implementation specifics.
However, what we finally settled on for this was to pass the existing display and value values to the method that retrieves the data.
Once the data is retrieved, we check to see if the missing data is present in the retrieved data and, if it is, we add a record to store the display and value values to the collection of data that is returned.
Implementing this functionality at the point of data retrieval allows us to support the same functionality in any client (asp.net, silverlight, etc).
We do go back and forth occasionally on whether it is appropriate to add the logic to the business object, but there are enough exceptions (i.e. web services, simple collections, etc) that we always end up back at the above design.

Web application to allow users to pick and choose objects used as building blocks?

I'm currently developing an application using ASP.NET MVC, and now I need to create an interface (web page) that will allow the users to pick and choose from a set of different objecs, the ones they'd like to use as the building blocks for constructing a more complex object.
My question is supposed to be generic, but to provide the actual example, let's say the application that will allow users to design pieces of furniture, like wardrobes, kitchen cabinets, etc. So, I've created C# classes representing the basic building blocks of furniture design, like basic shapes (pieces of wood that added together form a box, etc), doors, doorknobs, drawers, etc. Each of these classes have some common properties (width, height, length) and some specific properties, but all descend from a basic class called FurnitureItem, so there are ways for them to be 'connected' together, and interchanged. For instance, there are different types of doors that can be used in a wardrobe... like SimpleDoor, SlidingDoor, and so on. The user designing the furniture would have to choose wich type of Door object to apply to the current furniture. Also, there are other items, like dividing panels, shelves, drawers, etc. The resulting model of course would be a complete customized modularly designed wardrobe or kitchen cabinet, for example.
The problem is that while I can easily instantiate all the objects that I need and connect them together using C#, forming a complete furniture item, I need to provide a way for users to do it using a web interface. That means, they would probably have a toolbox or toolbar of some sort, and select (maybe drag and drop) items to a design panel, in the web interface... so, while in the browser I cannot have my C# class implementation... and if I post the selected item to the server (either a form post or using ajax), i need to reconstruct the whole collection of objects that were already previously chosen by the user, so I can fit the newly added item... and calculate it's dimensions, etc. and then finaly return the complete modified set of objects...
I'm trying to think of different ways of caching, or persisting theses objects while the user is still designing (adding and deleting items), since there may be many roundtrips to the server, because the proper calculation of dimentions (width, height, etc of contained objects) is done at the server by methods of my C# classes. It would be nice maybe to store objects for the currrent furniture being designed in a session object or cache object per user... even then I need to be able to provide some type of ID to the object being added and the one being added to, in a parent owner kind of way, so I can identify properly the object instance back in the server where the new instance will be connected to.
I know it's somehow confusing... but I hope this gives one idea of the problem I'm facing... In other words, I need to keep a set of interconnected objects in the server because they are responsible for calculations and applying some constraints, while allowing the users to manipulate each of these objects and how they are connected, adding and deleting them, through a web interface. So at the end, the whole thing can be persisted in a database. Idealy I want even to give user a visual representation or feedback, so they can see what they are designing as they go along...
Finally, the question is more so as to what approach should I take to this problem. Are C# classes enough in the server (encapsulating calculation and maybe generating each one it's own graphical representation back to the client)? Will I need to create similar classes in javascript to allow a more slick user experience? Will it be easier if I manage to keep the objects alive in a session or cache object between requests? Or should I just instantiate all objects that form the whole furniture again on each user interaction (for calculation)? In that case, I would have to post all the objects and all the already customized properties every time?
Any thoughts or ideas on how to best approach this problem are greatly appreciated...
Thanks!
From the way you've described it, here is what I'm envisioning:
It sounds like you do want a slick looking UI so yes, you'll want to divide your logic into two sets; a client-side set for building and a server-side set for validation. I would get heavy on the javascript so that the user can happily build their widget disconnected, and then validate everything once it's posted to the server.
Saving to a session opens a whole can of webfarm worms. If these widgets can be recreated in less than a minute (once they've decided what they like), I would avoid saving partials all together. If it's absolutely necessary though, I would save them to the database.
If the number of objects to construct a widget is reasonable, it could all come down at once. But if there are hundreds of types of 'doors' you're going to want to consider asynchronous calls to load them, with possible paging/sorting.
I'm confused about your last part about instantiating/posting all objects that form the whole furniture. This shouldn't be necessary. I imagine the user would do his construction on his client, and then pass up a single widget object to the server for validation.
That's what I'm thinking anyway... by the way, hello StackOverflow, this is my first post.
You might want to take a look at Backbone.js for this kind of project. It allows you to create client-side models, collections, views and controllers that would be well suited to your problem domain. It includes built in Ajax code for loading/saving those models/collections to/from the server.
As far as storing objects before the complete object is sent to the server, you could utilize localStorage, and store your object data as a JSON string.

Get dependencies from SQL query

I'm trying to do some SQL query validation programatically in C# (without invoking the actual database). Essentially, I'd like a user to be able to enter a view, UDF, or SP and have its dependencies validated immediately. The user would be entering these into a custom tool for defining database objects.
Thus, if a user entered:
CREATE VIEW someView AS SELECT name, address FROM users
I could pull out the dependency of "users" and then check against my database object collections that are stored in memory (e.g., Tables, Views, etc...) to make sure that dependency exists in one of them. Keep in mind the actual views/UDFs/SPs entered into my custom app are very complex and parsing them myself is not desirable.
I'm currently trying to do this using Microsoft.Data.Schema.ScriptDom.Sql.TSql100Parser. This provides a parse method which returns a DOM representation of the query. However, this is a terribly complex DOM and I'm essentially having to write an entire parser just for it.
Any ideas/suggestions? Thanks!
There is a proc that you can use named sp_depends...however it is not failsafe because of deferred name resolution. There really is no safe way to do this, it has gotten a little better in the latest versions but still a pain in the neck
read this Do you depend on sp_depends (no pun intended) to see what I mean

Categories

Resources