Populating many controls from the database (architecture and performance concerns)
So I have this page that collections a lot of information to create a little complicated object. so I collect all the related details together in a div and I have like 7 Divs, and开发者_开发百科 I populate like 20 controls (DropDownListss, CheckBoxLists, Repeaters, etc).
Now I'm just creating methods to be called on Click and Page_Load events. each population process is done separately.
The code is a hassle and I don't except the performance to be good either, because I know that a lot of connections/trips to the database is not good for performance and the load on the database.
so how can I put this functionality in a better design and a better performance, any help here =) ?
Why not load all this data in Page_Load? Then once in memory (whether in objects, data readers, or data sets), populate your controls?
That's a slightly philosophical question. As far as page perf. goes you shouldn't see a problem with only 20 controls. However, you have two key bottlenecks:
1) Get from database You need to try and get all of the data from the database at once. Of course that's not always possible but it will save you a couple roundtrips. You can also cache that object for the current request.
2) Save
if you write to db on each change event on postback, it will take unnecessarily long. I'd suggest you crete a Model
property that gets the model from the db and each change handler changes that model. Once the postbacks have all fired (I can't remember the best event now, but I think Page_Load
is ok) write that model to the database in one single commit.
As far as performance goes, this is probably the least effort for maximum gain. You can also load controls with Ajax to make seem faster but it doesn't solve anything.
NHibernate has very nice feature called NHibernate Futures (but not all databases are supported). You could also get another benefits from it such as caching out of the box, batching write queries and so on. So I recommend to consider this option.
BTW, do you really need all this information at the same time on the screen? You could split your UI layer and create, for example, wizard.
If you write the SQL queries yourself, you can combine most queries so that you only need one query per table.
E.g. you might have table dropdown_choices
, which has dropdown_id
and choice
columns.
You could SELECT all the data that concerns you and then split it up in your code.
SELECT * FROM dropdown_choices WHERE dropdown_id IN (1, 2, 3, 10, ...);
精彩评论