问题描述:

We develop the back office application with quite large Db.

It's not reasonable to load everything from DB to memory so when model's proprties are requested we read from DB (via EF)

But many of our UIs are just simple lists of entities with some (!) properties presented to the user.

For example, we just want to show Id, Title and Name.

And later when user select the item and want to perform some actions the whole object is needed. Now we have list of items stored in memory.

Some properties contain large textst, images or other data.

EF works with entities and reading a bunch of large objects degrades performance notably.

As far as I understand, the problem can be solved by creating lightweight entities and using them in appropriate context.

First.

I'm afraid that each view will make us create new LightweightEntity and we eventually will end with bloated object context.

Second. As the Model wraps EF we need to provide methods for various entities.

Third. ViewModels communicate and pass entities to each other.

So I'm stuck with all these considerations and need good architectural design advice.

Any ideas?

网友答案:

For images an large textst you may consider table splitting, which is commonly used to split a table in a lightweight entity and a "heavy" entity.

But I think what you call lightweight "entities" are data transfer objects (DTO's). These are not supplied by the context (so it won't get bloated) but by projection from entities, which is done in a repository or service.

For projection you can use AutoMapper, especially its newer feature that I describe here. This allows you to reduce the number of methods you need to provide "for various entities" (DTO's), because the type to project to can be given in a generic type parameter.

相关阅读:
Top