.NET RIA Services and Custom Data Model - CRUD Capabilities - silverlight

I would like to ".NET RIA Service"-enable my custom data model (BLL/DAL). Are there interfaces I have to implement to enable this functionality or all I have to do is create a Domain Service? If so, how can tell my Domain Services about my BLL? For Astoria, one has to implement IQueryable and IUpdatable interfaces for CRUD capabilities. Is this the same for RIA Services?

All you have to do is create a DomainService. The DomainService will have methods like GetMyObjects() and InsertObject(MyObject object) that either return or accept objects for your BLL - that's how you tell the DomainService about your classes. Your Get***() methods in the DomainService need to return a generic IQueryable, but you shouldn't need to implement it yourself. There's an AsQueryable() extension method that I believe you can use on a generic List, IEnumerable or Array that will convert any list of objects to an IQueryable. It should be reasonably easy to create a DomainService to wrap your existing BLL. Brad Abrams's has a great post describing this scenario: http://blogs.msdn.com/brada/archive/2009/07/22/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-6-poco-and-authentication-provider.aspx

Related

keeping custom attributes using a robust model layer with restangular

I have an issue in which I wonder if Restangular has support for. I have a UserModel which is part of my model layer. It may have custom attributes that the server doesn't have in it's model and also behavior. I'm not clear if I'm able to use my custom User model, send it to the backend and when it returns transform it back to the UserModel object of my model layer so I still have the custom attribute and methods.
Here's the plunker: http://plnkr.co/edit/IlYcSRuX3GPWmewxniuq?p=preview
Where do I handle the transformation? Do I add the methods in the config block or should I add it via adding a response interceptor? What about custom attributes that the server might not send back to me? I haven't run across any good examples of this.
The UserInfoCntrl controller sends the UserModel object into the contactInformationService in my example.
Some of this might be design choices, i.e. use what you think is best. However, a common pattern [citation needed ;)] would be to integrate the synchronization logic between client and server in the "model" service.
The UserModel service would then be responsible for providing the User object to the rest of the application, keeping it in sync with the server (perhaps via methods like save(), or perhaps automatically?). The service would then be the only module responsible for communicating with the server, at least when it comes to user objects. It can also automatically pull the user data from the server when instantiated.
The architecture feels very clean, at least to me.
I don't have any concrete examples that exactly suits your needs, but this authentication service by Fnakstad springs to mind. It maintains a object (actually a user object!) using $http and $cookieStore. Restangular is a bit more high-level than $http, but the self-contained service concept providing methods for manipulation and storing stands.

WCF RIA SERVICES and EF 4.1

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.

Silverlight wcf and ClientCredentials

I don't want to set the same username and password 100 times for ClientCredentials. I want a pattern where I can set this once in code then have it automatically set.
How do people typically do this? Do they inherit from the wcf class? Do they use partial classes?
Delegate the responsibility of creating the proxy instance to a separate class. This class can have method to create proxy, assign credentials and return the proxy instance back.This is something similar to creating factory classes for construction of an object.

Where to create DTO class in Silverlight WCF RIA?

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.

How to override Silvelright 4 RIA Service AuthenticationService User.IsInRole

I've started out with the Silverlight 4 Navigation Application template (RIA Services enabled). (As I really don't like the bloated Business Application Template)
I've added an Authentication Service and I'm able to authenticate users,
but want to override the User.IsInRole method.
WebContext.Current.User.IsInRole("Guest");
But I cannot find any place to override the behaviour.
What are you trying to do? User.IsInRole is an implementation of IPrincipal.IsInRole and really shouldn't be overridden.
If you want to set the user roles, you can do it on the server in your AuthenticationService by overridding the GetAuthenticatedUser or GetAnonymousUser methods.
If you want a method similar to IsInRole, you can extend the User type with a partial class on the client and add whatever methods make sense.

Resources