Data object and editor object design pattern - c#

Not sure how to ask this question but I'm looking for a clean way to solve a problem for a project. I'm trying create a class library that will allow someone to create a randomized character for a game and use the data for a web app. I have a CharacterModel class that has all the needed data publicly exposed, however I need a way to edit this information without making the setters publicly available. Also, if I have multiple characters in the future, how do I get the editor to effect specific objects? Is there a design pattern for this type of problem?

Make the setters public. If someone is using your library to create instances of CharacterModel and they plan to use it in a web app, they will know how to wrap access to the data in a safe way.
They will most likely use your library to create instances of your class, then store them serialized in a data store. When they send the data to a user of their app to look at and interact with, they'll either expose it through a web api of some kind to be accessed by a front-end SPA client or they will use a framework to render the data as HTML (e.g. ASP.NET Core w/ razor pages or mvc).
Whenever the user of their app requests changes to the object they will pass requests from the webpage or from the SPA client back to the server (either controller endpoints or through form posts) and they will use an auth system, character model identifiers and other code to determine what changes the user can apply before they save the changes to the data store (usually a database or a document/nosql system).
In other words, in relation to your question:
"...if I have multiple characters in the future, how do I get the editor to effect specific objects?"
...you'll let the consumer of your library figure that out when they are building an application with it.

Related

ASP.NET show dynamic changes on all clients

I'm a beginner with ASP.NET and webapplications in general.
For a project I have to interact with an enginnering software to read some data, for this I have to to use a ASP.NET project based on the .Net Framework 4.8.
For now I called these functions with buttons and displayed the data in gridviews. The problem is I want to show the data on all clients and the data should still be there when I refresh the page on one client.
To load some data in the gridview I tested it by using a function like this.
Load data to datagrid
The problem is I can't see these changes on other clients.
Is there a way to implement this?
Well, running some in-memmory code for one user of course will not work for other users. You probably would be best to write a seperate console application, place it on the server, and then say schedule it to run ever 5 minuites or whatever. That console or desktop program would thus then write out the data to a database table. Now, any and all web pages (and users) can now have a grid display that quieres against that database.
The other possbile (if for some strange reason you wanted to avoid a database to persist and store this information?
You could consider using signalR. It is complex, but allows you to push (send) out information to all web clients connected to your system. This involves some rather fancy foot work, and does require your web page to include some JavaScript. As a result, the simple database idea is less work, simple, and does not require many hand-stands.
but, signalR is what is used for say pushing out information to each client browser, you can get started here, since this is a vast topic well beyond that of a simple Q & A on SO.
https://learn.microsoft.com/en-us/aspnet/signalr/overview/getting-started/

N-Teir Architecture with ASP.NET Web API

I'm just trying to wrap my head around this concept. I have written a couple different Web APIs but they have always been consumed by a website and interacted via JSON. I have a question about how to structure the implementation when the Web API will be consumed by a windows service.
In this case there is already an existing Database so I want to use Entity Framework's Database First approach.
I create a Class Library project for the models and use Entity Framework to look at the existing database and generate all of the required classes.
Then I create a Web API Project and add my Class Library with all of the models to it. Up to this point I am good.
My question is when I go to build the Windows Service that will interact with the Web API, how do I access the classes from my Class Library model project? I know I could add that project to my windows service but that doesn't seem like the correct approach because that would pretty much by-pass the Web API.
I guess my question is if I want to create and pass an Employee object to my Web API (so it can insert it into the database) from my windows Service, how does the windows service get the Employee object without adding the Class Library to the Windows service project?
In an n-tier solution you don't pass domain objects across physical boundaries, but you implement data-transfer objects (DTO) that will only hold the required info by the consumer/caller.
Usually you're going to created a shared library that will have the whole data-transfer objects and this will be referenced both by the server and the client.
After that, it's all about using a JSON serializer in order to serialize and/or deserialize your data-transfer objects.
Domain objects will be always mapped to data-transfer objects because these are lighter than a full object. Make yourself a question: if the consumer only requires the name and the second name of someone, why you need to send more data over the wire?
In addition, it's important to avoid server dependencies in client applications and services.
Some useful tips:
Learn what's a DTO: http://en.wikipedia.org/wiki/Data_transfer_object and http://martinfowler.com/eaaCatalog/dataTransferObject.html
Check AutoMapper and how can save you time in order to map domain objects to data-transfer objects: http://automapper.org/
Normally you create exra model classes that are used for the Web API. Those model classes often contain only a subset of the data of the entities. Furthermore, this distinction allows you to create truly RESTful APIs.
Inside your Web APIs controller classes the mapping between Model and Entity happens.
The windows service only referenes the project with the Model classes but not that with the Entity classes.

Application configuration exposed via Web

Hi guys I need to exposed my application configuration files that can be edited on the fly and some reports on a web page. All of my application are either console / win forms app.
I'm planning to use XSLT and http listener class of .net for the implementation is this a good idea to start or not? or is there any implementation of this kind ?
Editing web.config or app.config should be avoided in lieu of storing such settings that need to change dynamically in another data store, like a database. If you store such settings in a database you can easily create a UI to administer such settings.
If I'm understanding you correctly then what you want is a web interface that users can use to modify some configuration properties and/or generate some reports based on data that is being used/generated by one or more desktop applications.
You need to define a central database, like SQL Server or something similar. Your desktop applications then need to store their data in this database. Then it would be super easy to build a web user interface that could also use the same database and view/manipulate data.
You're question is kind of vague so for now that's the best answer I can give you. I'm thinking you need to re-think your architecture.

Get Serializable Object from Session in Silverlight

I have a complex, [Serializable] object stored in session. I have Silverlight 3.0 islands in my .aspx pages that need access to this data and its data type. It is my understanding that Silverlight does not support [Serializable], and since it is running on the client, it does not have easy access to session. I am looking for a solid way to access this data in my Page.xaml.cs file.
I am open to storing it in ISO Storage once it has been retrieved, but how to retrieve, read it from Silverlight? Hidden fields are not an option as it is a complex data type with dozens of properties, and a few dictionaries, lists of other objects.
The classic way of accessing this type of data would be with a silverlight-enabled WCF service on the ASP.NET site that accesses the data. You then add a service-reference from the silverlight client and ask the server for data (asynchronously).
Note that by default this will be a separate object model (proxies from "mex"). If you need the same type you'll have to repeat the code in the client (you can't really use assembly sharing between client and server here).
I don't know whether the silverlight version of svcutil will allow type re-use (the regular version does), but if not another option is to just return xml or binary from the service and deserialize locally. One option here would be something like protobuf-net.

How do you make a form in Sitecore 6?

Most CMS' have a way to create forms for users or anonymous users to fill out. Is there a mechanism for creating forms like this in Sitecore 6?
As far as I can tell, the only way to make forms like this would be to manually create them in a sub-layout (user control), and then add that sub-layout to a page. Does anyone know a better way?
It depends on who you want creating the forms.
If you want content authors or admins to be able to use a wysiwyg-like interface for creating and updating forms, then I would recommend using the Web Forms for Marketers Module that is available for Sitecore. While certainly not the panacea for allowing content authors to create forms, it is pretty robust and also very extensible.
However, if it will primarily be developers creating and maintaining the forms, then it will probably be easier for you to go with the sublayout approach.
To ctford's point, the Web Forms for Marketers Module does use it's own database for form data storage, not the typical Sitecore content databases. And you'd probably want to do something similar for your forms as well, rather than writing data back into Sitecore. But that will depend on your business requirements.
Your UserControl suggestion is the way I would do it.
Sitecore, like other CMSes (such as RedDot), has a disconnected publishing model. Content is populated in a separate editing environment before being pushed to a live environment.
Consequently, the live environment is read-only as far as the CMS is concerned. This disallows native form processing because Sitecore doesn't expect updates to be made to the live database. To store data from anonymous users you would need to create custom database tables outside the control of the CMS.
As far as I know, there is no forms API comparable to Drupal's.

Categories

Resources