I'm making a Silverlight 4 application with WCF RIA Services.
On the server side (the *.Web project), I have an Entity Model that's auto-generated from a SQL Server database.
On the client side, I have the domain service and proxy objects that are generated by Visual Studio for use in Silverlight assemblies.
I want to add custom properties to the model (preferably on the server side).
Say I have Contact, Company, and Address tables, which are linked by foreign keys (but not necessarily actual foreign key constraints). I want to add a property that will return a Contact's Company's Address object.
I have been attempting to do this by making a partial class to extend the Contact class, and adding a CompanyAddress { get; } property. But I have no idea what I need to do with the new property in order to make it propagate to the auto-generated code on the client side. Are there specific attributes I have to add to the property? Do I have to register it somewhere so that the code generator will know about it?
Does this have to be a Navigation Property or can it be something simpler?
And is this even the best way to do things, or should I give up on extending the server-side model and just do it on the client side? (If I do it on the client side, I face the problem of not having access to the context object inside the individual Entity-derived classes.)
I have never used Silverlight or RIA services but I guess it will be quite similar. When you create EF model and you have entities related by foreign key (there has to be relation), each entity related to other entity will contain something called navigation property. So in your scenario Contact should contain property named Company and Company shoud contain property called Address. You can isntruct EF to load those navigation properties by using Include on ObjectSet or by lazy loading (not good idea in WCF). Than if you send Contact by WCF to the client, Company and Address will be send as well.
Your approach has one big problem. Your property contains only getter - such property is not serialized.
Related
In an MVVM architecture, where should the model be? Does the client have to have a seperate model (which we might call client model) or can we consider the web API the client is communicating with as the "M" in MVVM.
It might be more helpful to explain it over an example. Let's say we have a WPF application for shopping (which is tightly integrated to the server as opposed to a calculator like application). When a customer adds a product to the cart, "AddProductToCart(product)" is called via Web API and returns "OK" on successful operation completion. Then, should we keep a cart object at the client side as well and add the newly added product object to cart? Or rather than keeping such a state, should we just update the UI, notifying the customer of the successful aoperation and when the user navigates to another page that requires the knowledge of products in the cart, the client queries the web API and get the fresh data?
If such a model does not exist at the client, then server can return DTO's which can be used as the view model. But otherwise, I guess I will need to keep some kind of a modified version of the business model at the client side(since it would not be right to use the business model entities for the client model). Would that be more in line with the MVVM architecture? If so, is there a best practice for the formation of that model? Because I guess it will need flattening and will requre holding some view specific fields, too.
i am developing an application on MEAN STACK. I want to show the data from an existing Collection in mongodb . But i am not getting the clear idea how to attach this Collection with mean stack.I have seen Article example and sign up process but little bit confuse.
How to set up new Collection with meanjs.
The most used tool for this is Mongoose. A mongoose model should be created on server side and in the server controllers you expose the required operations (Create, Read, Update, Delete etc). MEAN uses Mongoose.
On client side you create services which will access the server side controllers. The client side objects should have the same attributes as the server side model by convention.
I didn't use the MEAN Article example, but I used the Yeoman code generators to kick off some entities. I assume that the Article example follows the same pattern, so for server side model please check app/models, for the server side controllers check app/controllers, for client side services check public/modules/article/services.
Let's say we have a User entity. Should I have two smaller services (User and Users)? Or one larger service that deals with both a collection of Users and an individual User? If it is the latter, is it best practice to name the service User or Users?
I use one service per entity that houses the collection, methods used by the entity collection controller, and methods used by the individual entity's controller. This works for my team as we follow the repository pattern in our server code. I save the collection in the service because it is accessed often, and parts of the collection are nice to share in other area's like to keep a count in the menu, or create a relational list in another controller. The individual entity is typically only accessed by the controller for the view, and can be disposed of when the route is changed as long as the list item in the controller was updated if the entity was changed.
The only time I used separate services was one edge case where the customer wanted an entity to save state if the route was changed without persisting the entity to the server or cache. The entity needed to be saved somewhere so that was reason enough to create a service for the individual entity.
I do use a separate service per entity to manage http requests. So each entity does have two services, one to manage the collection and all crud+ functionality, and the other for http for separation of concerns.
Do I have to explicitly add the [Include] attribute for any entities that has ICollection property so that result LoadOperation callback will populate my entity with its collection?
Querying the data from the Service is fine, but when being passed to the LoadOperation it doesn't add the collection of a certain entity. Does it have something to do with serialization/deserialization from the service message?
If yes, what is the reason why? I have used EF with DbContext.Includes in WPF but I did not have any issues with my T4 template generated items when I wanted to query my entity's collection.
The [Include] attribute is required to advise the WCF marshaller to serialize related entities. Specifically, MSDN advises
this attribute specifies that the association should be part of any
code-generated client entities, and that any related entities should
be included when serializing results to the client.
You will still need to use the query.Include() on your DbContext to retrieve the related data from the database.
I have one table with 20 columns, I want only display this data on UI (not add/edit/delete). I want to know, where is appropriate place to create DTO class for this table, in DAL project or in Web project?
You can create DTO's in the web project, map the entity to it, and have the domain service call return it. You could also just return the entity and mark the Properties you don't want displayed with [Display(AutoGenerated = False)].
If you are using LINQ to Entities or LINQ to SQL, you could use projection to limit what is returned from the WCF service request and work with an anonymous type. Use the LINQ select method. Then you wouldn't have to create a DTO object.
If the UI you mentioned is Silverlight, WCF Data Services (EDIT: and WCF RIA Services does not) support projection across a service.
LINQ example:
context.Displays
.OrderBy(d => d.Title)
.Select (
d =>
new
{
Title = d.Title
})
Julie Lerman says to use the QueryView in an MSDN Magazine article. That might be the solution.
If you only wants to show data, the better approach would be the create a View and directly get it on Client Side. This will be much more cleaner and easier.