问题
I have worked with MS CRM. There we can design our custom entity graphically and then we can also build a visual form to perform CRUD operations on that entity.
This feels so simple from end user's perspective. However I am interested to know how can I develop the similar kind of application where I design my table on the fly and the design UI on the fly.
What I want to know is like how do they achieve all of this dynamically? If I have to create CRUD on one simple table, I need to write good amount of code. How MS achieves everything on the fly? Any pointers, any document would be of great help.
回答1:
I have no idea how they actually do it, but if it were me I'd use attributes and reflection.
Here's how I think it'd probably work / or rougthly how you could do it...
Components
The system would need a bunch of components or sub-systems; they coudl be stuff you write yourself or existing libraries (use existing libraries if you can):
- A CMS of somekind, unless you want to build this into a system you already have.
- A UI component / control that lets users make their "objects". To be done well this would require a fairly advanced UI (SilverLight? HTML5?) - although I guess you could use something really basic.
- Some way of storing the users "objects" - this is bascially data, and you'd want to keep it stored in a "neutral" repository of some kind.
- Something that does the actual CRUD - I'm thinking some sort of ORM tool like Entity Framework, Lightspeed, NHibernate. You'll also need a datasource target for the CRUD operations themselves.
The trick is in points 2 and 3; this is where I'd define a set of Attributes that could be used to define the user created objects. These attributes are what logically joins the process together. Because Attributes can be read at runtime:
- They'll be able to drive the UI that allows users to compose their "objects".
- When ready, you could generate actual classes that physically implement the user defined objects - and then decorate those objects with the appropriate attributes.
- Somehow have the ORM tool know how to map the properties of those objects (based on the attributes) to the datastore, or, write the DAL yourself - maybe you're DAL would wrap an ORM?
Another approach I've used is based pretty much on this but it also makes use of interfaces, and the data is stored as a "blob" of XML - not specific discrete metadata.
来源:https://stackoverflow.com/questions/3766708/how-to-develop-ms-crm-kind-of-application