How do I call a method from a Domain Service class - asp.net

Once again I am in need of some assistance with regard to calling a Domain Service class method from silverlight.
This ties in somewhat to my question of yesterday which was solved. Thanks again!
For those who are interested, my question of yesterday can be found here:
Using ASP.net membership to get aspnet_Users in silverlight
Now onto my current question.
I have the following method in Domain Service Class called MembershipData
[RequiresRole("Managers")]
public void DeleteUser(MembershipServiceUser user)
{
Membership.DeleteUser(user.UserName);
}
This code is from kylemc's tutorial
Now how do I call this method from within Silverlight?
I understand from yesterday's question that
public IEnumerable<MembershipServiceUser> GetAllUsers()
needs to be called by defining the query, then running the query and then calling OnGetAllUsersLoaded when the the results return.
What I am unsure of is, Do you need to call the method in this way because of its return type?
Obviously public void DeleteUser(MembershipServiceUser user) has no return type so cannot be called in this way.
It does not seem to be possible to do:
MembershipDataContext context = new MembershipDataContext();
MembershipServiceUser user = new MembershipServiceUser();
user.UserName = "bob";
context.DeleteUser(user);
But then how do I use the DeleteUser method?
Any assistance is greatly appreciated.
Kind regards,
Neill
Update
Thanks for the info HiTech. I still have one issue I need to solve. Perhaps I am still not doing something correctly.
I am now creating a new instance of MembershipServiceUser, lets call it msu.
Then assigning msu.UserName and msu.Email the user details, and after that calling
context.MembershipServiceUsers.Remove(msu)
where context is my MembershipData domain service context, and I have checked that
context MembershipServiceUsers results
does have my user info. I then however get the following error
"The specified entity is not contained in this EntitySet."
I am positive the data in msu is correct, so any ideas as to why I am getting this error?
Many thanks
Update 2
Am I on the right track with something like the following...
MembershipServiceUser usr = (from a in context.MembershipServiceUsers
where a.UserName == "bob"
select a).First();
context.MembershipServiceUsers.Remove(usr);
context.SubmitChanges(DeleteUser_completed, null);
Or is this way off? because in my callback DeleteUser_completed(SubmitOperation so)
so.HasError = true
while so's ChangeSet -> RemovedEntities = 1, but so's EntitiesInError's result is "enumeration yielded no results"
Once again thanks for helping steer me in the right direction.

RIA services works by creating a change set (literally a set of changes) and sending just those changes to the server. The methods to do CRUD are more like Entity Framework, not direct method calls.
On the client side you will call the Remove method on the domain context's User collection.
On the receiving side it goes through all the changes and says:
Q. "Is this an object deletion?"
A. Yes...
Q. "What object type is it?"
A. MembershipServiceUser
Q. "Do we have a method called Delete that takes a MembershipServiceUser parameter?"
A. Yes...
It then calls that method with the object from the changeset...

Related

Is there any difference between Request.Cookies(name) and Request.Cookies.Get(name)?

As both of them can be used for retrieving cookie through name string, I would like to know if there is any difference between them.
A great way to answer a question like this yourself, for the .NET Framework, is to make use of the Microsoft Reference Source. This lets you see and navigate through the source for the .NET Framework.
Looking at this, Request.Cookies returns an HttpCookieCollection and Request.Cookies.Get is therefore a method on HttpCookieCollection.
The most useful part of the code is for the indexer on HttpCookieCollection that retrieves a cookie by name:
public HttpCookie this[String name]
{
get { return Get(name);}
}
As you can see from that, this calls into the Get(string name) method, meaning that using the Request.Cookies(name) indexer is fundamentally the same as using Request.Cookies.Get(name) as one calls the other.
It is worth mentioning that anything you see here is an implementation detail that's subject to change. You should rely on documented behaviour, not on anything you discover through digging through the code, no matter how informative and interesting it is!

Spring MVC not adding object to parent with Thymeleaf dynamic form

So I have a dynamic form that I created with thymeleaf and everything submits just fine to the database, however the child object, which in this case is row, is not being saved to the parent studySet. When I'm in debug mode I can see that the studySet contains an arraylist of however many row objects, but when they're being saved, it's just not being set to the studySet object.
I'll show some code, to see if I'm forgetting to do anything here. If anyone can see my problem and let me know that would be great. Thanks in advance
Here's my post method in my controller
#RequestMapping(value="createStudySet", method=RequestMethod.POST)
public String createSetPost (#ModelAttribute StudySet studySet, ModelMap model, #AuthenticationPrincipal User user) {
studySet.setUser(user);
user.getStudySet().add(studySet);
List<Row> rows = studySet.getRows();
studySet.setRows(rows);
studySetRepo.save(studySet);
return "redirect:/answers";
}
Also this may be helpful, this is a screenshot of my controller in debug mode, so that you can see that there is rows in the studySet.
So, I just want to set those rows to the studySet. Also I realize that this code is probably useless, but it shows an attempt at what I'm trying to do. List<Row> rows = studySet.getRows(); studySet.setRows(rows);.
Also let me know if it would be useful to see my domain objects, and I would add those to the question.

Customizing Routes for view user profile -ASP.NET MVC

I am new to programming. How can I create a custom URL for each user to retrieve data for the entered user? For instance, www.hellothisis.com/sirushti
I understand that it's very difficult in the beginning. However, have you read the introduction to MVC? There you will understand the basics and go further.
Even though you need to read that, the answer to you question is: this
You will see the default router and the "/sirushti" is the ID. In the controller you will likely to have something like this:
public JsonResult GetUser(string alias) { ... }
Take a look on the links I've sent you and you will get what it takes to use MVC!

How do I get an ID after saving an ExtBase Model?

After creating a model and adding it to a repository I want to have the new ID for different purposes (creating a mail, updating other fields outside the Extbase world)
$page = t3lib_div::makeInstance('Tx_MyExt_Domain_Model_Page');
$page->setTitle('Hello World');
$this->pageRepository->add($page);
At this point $page hasn't got an ID yet, uid is null.
$page->getUid(); // returns null
When does it get it? And how can I retrieve in on runtime?
In ExtBase, objects are "managed". This means every persistence transaction (add/remove/update) is simply noted in the underlying logic, but not yet executed until the appropriate time (like the end of processing a request). So, just because you add an object to a repository doesn't mean that it's actually added yet. That actually happens once $persistenceManager->persistAll() is called, which isn't something you need to do manually, ever. The point is, your $page object won't have a UID until it's saved and that's why $page->getUid() returns null. Look here for a great explanation.
I suspect that you are trying to do something outside of the ExtBase object/MVC lifecycle. At least, last time I got null when I tried to get the UID of an object, it was because I wasn't operating within the framework appropriately.
However, if you post some more code and give us a bigger picture of what you're trying to achieve, maybe we can help you get to a point where that object actually has a UID. For instance, if you're in a Controller object, tell us which Action method you're in, or if you're in a Repository object, tell us what you're trying to get from the repository and where/how you plan on using the query results.
EDIT
Just guessing here, but I'm assuming you're executing this code in some action of a controller. Since after the controller is executed a view is rendered, you can just pass the page object to the view:
$this->view->assign('page', $page);
And then in your view you can use the page object in a link:
<f:link.action action="show" arguments="{page:page}">
See this page object
</f:link.action>
And then in the show action of your controller you can show the page:
public function showAction(Tx_MyExt_Domain_Model_Page $page) {
// Do whatever you need to show the page in the `Show.html` template
}
I really am just guessing here. If you can give us a larger picture of what you're trying to do, what your action methods are supposed to do and things like that, we can answer your question a little more confidently.
(I'm also assuming that your page object isn't a replacement for the regular TYPO3 pages and that they are something totally different. It's much easier to deal with those TYPO3 pages through the backend interface than at the php level.)
You can call persistence manager explicitly in Your controller like this
#TYPO3 4.x
$persistenceManager = $this->objectManager->create('Tx_Extbase_Persistence_Manager');
$persistenceManager->persistAll();
#TYPO3 6.x
$persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
$persistenceManager->persistAll();

Passing a web service an unknown number of parameters

I'm relatively new to utilizing web services. I'm trying to create one that will be accepting data from a ASP.Net form whose input controls are created dynamically at runtime and I don't how many control values will be getting passed.
I'm thinking I'll be using jQuery's serialize() on the form to get the data, but what do I have the web service accept for a parameter? I thought maybe I could use serializeArray(), but still I don't know what type of variable to accept for the JavaScript array.
Finally, I was thinking that I might need to create a simple data transfer object with the data before sending it along to the web service. I just didn't wanna go through with the DTO route if there was a much simpler way or an established best practice that I should follow.
Thanks in advance for any direction you can provide and let me know I wasn't clear enough, or if you have any questions.
The answer to the headline question (assuming this is an ASP.Net web service) is to use the params keyword in your web service method:
[WebMethod]
public void SendSomething(params string[] somethings)
{
foreach (string s in somethings)
{
// do whatever you're gonna do
}
}
Examples:
SendSomething("whatever");
SendSomething("whatever 1", "whatever 2", "whatever 3");
In fact, you don't really even need the params keyword - using an ordinary array as a parameter will let you pass in an unknown number of values.
Well I went with creating my own data transfer object which I guess was always the front of brain solution, I was just thinking that there was probably a recognized best practice on how to handle this.

Resources