Azure Mobile Services linked tables - c#

I have an application with 3 tables
Items, Clients, Types
Each item can be associated to one client and one type
This was originally stored using SQL Server CE and I have now pushed the data up to azure mobile services.
I am attempting to reuse this data in a new windows universal application written in c#.
In Azure I created 3 tables itemtable clienttable typetable, in itemtable I have columns for the id of the clienttable and typetable entry (item.clienttableid = clienttable.id).
The Azure mobile services backend is set to javascript, I chose this as I thought it would be more compatible across platforms than the .net backend is that true?
I want to be able to read all the items from the items table and reference the properties of the client and type table from the item (e.g. item.client.clientname)
Is there a way of defining my class so that when I request all items from azure I also get the associated type and client.
This is how I have my class so far
public class ItemTable
{
public string Id { get; set; }
[JsonProperty(PropertyName = "itemdate")]
public DateTime ItemDate { get; set; }
[JsonProperty(PropertyName = "itemamount")]
public decimal ItemAmount { get; set; }
[JsonProperty(PropertyName = "itemdescription")]
public string ItemDescription { get; set; }
[JsonProperty(PropertyName = "ItemClientID")]
public ClientTable Client { get; set; }
[JsonProperty(PropertyName = "ItemTypeID")]
public TypeTable Type { get; set; }
}
public class ClientTable
{
public string Id { get; set; }
[JsonProperty(PropertyName = "clientname")]
public string ClientName { get; set; }
}
public class TypeTable
{
public string Id { get; set; }
[JsonProperty(PropertyName = "typename")]
public string TypeName { get; set; }
}
I have seen this http://blogs.msdn.com/b/carlosfigueira/archive/2013/08/23/complex-types-and-azure-mobile-services.aspx
but cannot wrap my head around how to adapt it to my situation

The Azure mobile services backend is set to javascript, I chose this as I thought it would be more compatible across platforms than the .net backend is that true?
No matter which time of backend you are using, it will be easy in each case because Azure Mobile Services Team created a "Azure Mobile Services SDK" for client applications, which you can install by "Manage Nuget Package".
This is how I have my class so far
I saw the model and next time you can show the class diagram from your model, learn about in this article Class diagram: a easy way to understand code. If this model is for the client/.Net Backend, I think it is not completely correct because you said this
3 tables Items, Clients and Types. Each item can be associated to one client and one type
In the ItemTable class you need to have something like it
public ClientTable ClientId { get; set; }
[ForeignKey("ClientId")]
[JsonProperty(PropertyName = "ItemClientID")]
public virtual ClientTable Client { get; set; }
public string TypeTableId { get; set; }
[ForeignKey("TypeTableId")]
[JsonProperty(PropertyName = "ItemTypeID")]
public virtual TypeTable TypeTable { get; set; }
Note: In client apps remove the attribute ForeignKey.
If you have doubts I recommend to see this
Azure Mobile Services Samples - Samples and articles to help developers to use Azure Mobile Services

Related

Adding run-time only collection to OData list

I'm new to OData and I have a working solution for complex requests of my domain data, which is great. The bit I'm looking for help with is using this data and sending the entire list to another API to append additional data to the OData response for a particular object type.
I don't mind losing the ability to further query the appended collection, this can be done later in my front end. I'm looking for some help in identifying the right architecture for my solution.
I've seen a few possible answers that would be to create a custom serializer or use AutoMapper to populate the DTO but this would send the requests to the other API one at a time. I'm looking to send the entire list as the performance is improved rather than one at a time as there can be 20,000+ items in the response.
Here's my current schema.
Domain
public class InventoryItem
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string DeviceName { get; set; }
}
DTO
public class InventoryItem
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string DeviceName { get; set; }
public IList<ReadingDto> Readings { get; set; } = new List<ReadingDto>();
}
public class ReadingDto
{
public double? Value { get; set; }
}
As the ReadingDto is obtained from another API, this shouldn't be present in my domain model. It would never be populated.
The ideal solution would be to utilise a service within my project which would send the list over to the other API whilst maintaining the flexibility of using OData. Whether this is in a custom serializer, middleware or filters, I'm just not sure what would be best.

Sessions in .net6 after upgrading from .net4.8

I just upgraded my .net 4.8 MVC web app to .net6.
I used Sessions to store objects.
for example the User class:
public class User
{
[Key]
public string UserID { get; set; }
public string TenantId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MobilePhone { get; set; }
public bool IsEnabled { get; set; }
public virtual ICollection<Department> Departments { get; set; }
}
This is how I set the session:
Session[Consts.CURRENTUSER] = userFromDb;
This is how I use it:
User _currentUser = Session[Consts.CURRENTUSER] as User;
Now, after the upgrade it does not compile. I get the following error:
Error CS0103 The name 'Session' does not exist in the current context
If i use the following HttpContext.Session[Consts.CURRENTUSER] as User it still does not allow the the above use.
Will appreciate an example on how I will be able to use the above scenario in .net core.
after reading Microsoft docs, I followed this guide from step 4 to allow the usage of Sessions.
Then, in order to use complex objects in .net core I followed this link which provided an extension method that implementing the use of complex objects in sessions.

Practices in integrating client-server communication with .net core MVC web app

I've been using knockout.js handle client-server communication, but now want to know if my way is correct and how it works in other frameworks, like Angular2 or React when they are integrated to MVC web app.
Assume we have a model for a game
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public double SkillRating { get; set; }
public List<Attendance> GameAttendanceHistory { get; set; }
public DateTime MemberSince { get; set; }
}
but when we want to make a chat system, some of the properties aren't necessary for the view, so the viewmodel of the user would be like this
public class UserViewModel
{
public UserViewModel(User user)
{
Id = user.Id;
Name = user.Name;
}
//For guest users
public UserViewModel()
{
Id = 0;
Name = "Guest";
}
public int Id { get; set; }
public string Name { get; set; }
}
and there are some UI models
//One chat channel. Users can open several channels at once.
public class BoardViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string MessageToPost { get; set; }
public virtual List<User> Users { get; set; }
public virtual List<Post> Posts { get; set; }
}
//Message to be posted to each channel. Messages will be stored in SQL database.
public class Post
{
[Key]
public int Id { get; set; }
public string Message { get; set; }
public DateTime DatePosted { get; set; }
public int PostById { get; set; }
public string PostByName { get; set; }
}
and finally, controller passes a viewmodel
public class ChatUIViewModel
{
public UserViewModel CurrentUser { get; set; }
//List of all boards to which the user belonging
public List<BoardViewModel> Boards { get; set; }
}
to the view and I let knockout.mapping parse the json of the data to create the viewmodel to use. If the user join a new channel, post or get a message, then the client will request the data of the target board via SignalR hub, or notified the message from hub and annex it to the DOM.
Here my concern is necessity of giving a data to the view upon action call.
Because the client anyway communicates with the server for updating their status, I doubt that I need to let the controller passes the list of the boards when the user request the action.
Clients can also request the data when the page is loaded via WebAPI, SignalR or some other method, and then it doesn't have to parse the json data to create a viewmodel. Plus, if I place such a function which the returns the data on cloud it would be good for reducing the load of my server instance. Having this assumption on the right track?
Also, I guess in other frameworks it's a fundamental approach that clients post/get all what they need to/from the server, but not sure. Could someone explain the difference between the three frameworks mentioned above in terms of client-server communication in this case of chat app?
To cut a long story short:
You should have the master model on you server.
In the case a new client connects to the server, first the clients should make a GET for the acutal state of the model.
After that the server informs the clients about changes.
I did once a project where I used signalr also for the first get, because the client can also call methods with signalr on the server. There are some developers which think this is not a good solution but I do not understand the argumentation
Sample:
Imaging you have simple tic-tac-toe game (Fields A1/A2/A3/B1/B2/B3) for two player (Player X and player O). First both player have to connect to this playfiled. After both clients are connected you should inform one player (for Example Palyer X) that you can start. The other client see only that the other player is on turn. Player X then clicks on field A1. As a result you send this information to the server like playerMove(gameId, playerId, fieldId). The server validates this and if valid he sends an event to both players playerMoved(gameId, playerId, fieldId). As a result of this both clients will draw the X in A1. The server sends also an event to the client X that client O is on turn, and an event to client O that he is on turn.
All the model stuff is running on the server.

Good practices for creating a Web API 2 & EF based system

I am creating a Web API 2 application based on Entity Framework. My questions are about good practices involved in creating Web API 2 & EF based system.
Suppose that I have class Payment containing class Address.
public class Payment()
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public Address PaymentAddress{ get; set; }
}
public class Address ()
{
public string StreetName{ get; set; }
public string City{ get; set; }
}
Question 1:
Does my Web API should allow for changing Address from endpoint PUT /payments (I mean changing Address's properties not the whole object) called Solution 1
Question 2:
or maybe PUT /payments should update only simple properties of Payment without Address properties? In this scenario Address properties could by changed by endpoint PUT /payments/{paymentId}/address (called Solution 2)?
Question 3:
Currently I have implemented Solution 1 but I have problem to set context properly for Payment updates with updated Address's properties. I have to manually change EF State value of Address. If solution 2 would solve my problem?
Question 4
By default EF one-to-many relation is defined automatically like in my example. But I can use ForeignKey attribute mark column as foreign key. Why this solution is better? Does is help in anything?
public class Payment()
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Amount { get; set; }
public int AddressId{ get; set; }
[ForeignKey("AddressId")]
public Address PaymentAddress{ get; set; }
}

How does one design his various architecture/business models?

I'm currently learning about various application design approaches and there's one thing I can't really get my head around.
Let's say I have a physical device in my network, an IP surveillance camera. I need a model that represents 'IP surveillance cameras' to work with them in my application, which might look like this:
public class IPCamera {
public string Token { get; set; }
public IPAddress IPAddress { get; set; }
public string RtspUri { get; set; }
}
Now, if I want to store the IPCamera into a database (e.g. with Entity Framework), I need maybe other/additional properties and thus, another model with:
public int Id { get; set; }
Oh.. I want to access my application with via a WCF service. I can't use the object "IPAddress" here because it's not very serialization friendly, hence I need another model with a custom IP class that stores the IP address as string.
And last, an Android clients wants to access my data. I design a RESTful API using WebApi2. The client isn't interested in the cameras RTSPUri, but wants to know about the current state (is online, is offline).
I'm wondering: How to name all these models that they don't come in conflict? Should I design a model per each purpose/layer/service? And how to link/map them? With the adapter pattern?
I would include everything in your entity and then create view models that only expose the properties that matter to the domain you're accessing your entities through.
Your entity:
public class IpCamera
{
public int Id { get; set; }
public string Token { get; set; }
public IPAddress IPAddress { get; set; }
public string RtspUri { get; set; }
public bool IsOnline { get; set; }
}
In your WCF service:
public class IpCameraViewModel
{
public int Id { get; set; }
public string IpAddress { get; set; }
public string Token { get; set; }
public string RtspUri { get; set; }
}
In your api project:
public class IpCameraViewModel
{
public int Id { get; set; }
public string IpAddress { get; set; }
public string Token { get; set; }
public bool IsOnline { get; set; }
}
And you can just set the IpAddress as a string to send to a receiving client. You can shed away any properties you don't want to expose. Or you can add properties that don't belong to the IpCamera entity and just add them to your view model from another entity.
As #Smith.h.Neil suggested, you should create one base normalized model (entity if you will) to store and several view models (projections).
You can easily map to/from view models using tools like AutoMapper.
As for the naming, I wouldn't use technical suffixes (like *ViewModel) in API layer. Think hard and figure out correct domain (business oriented) name of each projection. Like IpCameraStatus or perhaps just IpCamera (but in another namespace).

Categories