Solved

remoting questions

Posted on 2004-10-05
5
186 Views
Last Modified: 2010-04-15
Hi!

I often build my classes like this:
      public class User
      {
            private string username="";
            private string password="";
            public bool GetUser(int userid)
            {
                  try
                  {
                        //get username and password from database
                        //assign private string username and private string password
                        return true;
                  }
                  catch
                  {
                        return false;
                  }
            }
            public string UserName
            {
                  get{return this.username;}
                  set{this.username = value;}
            }
            public string Password
            {
                  get{return this.password;}
                  set{this.password = value;}
            }
            public bool UpdateUser(int userid)
            {
                  try
                  {
                        //update database with this.username and this.password
                        return true;
                  }
                  catch
                  {
                        return false;
                  }
            }
            public bool CreateUser(int userid)
            {
                  try
                  {
                        //save this.username and this.password as new post in database
                        return true;
                  }
                  catch
                  {
                        return false;
                  }
            }
      }

But now I'm making a project that uses remoting and I think that it may cause some problems.

Some background to the project if it may help:
The classes in the project are made in VB.NET because they interact with a program that isn't build to be accessed from C#(I tried but had to abandon it quite quick), I have also made an Interface in C# for the VB.NET project and the "listning server" and clients is also made in C#.
The VB.NET class has properties that get/set datatables, integers and strings.

My concern, (it just hit me..) is that SingleTon calls share info between clients and that may cause big problems with data being mixed up!?
Let's say that the client does this:

IUser user;
user=(IUser)Activator.GetObject(typeof(IUser),Convert.ToString(ConfigurationSettings.GetConfig("User")));
user.IUserName = txtUsername.Text.Trim();
user.IPassword = txtPassword.Text.Trim();
if(user.IUpdateUser(Convert.ToInt32(txtUserID.Text)))
      MessageBox.Show("User updated");
else
      MessageBox.Show("User NOT updated");

That is no problem, but what if 10 users do it at the same time?
I guess there's risk that data may be mixed up?
How can I solve this?
0
Comment
Question by:aleborg
5 Comments
 
LVL 10

Expert Comment

by:123654789987
Comment Utility
0
 
LVL 3

Accepted Solution

by:
skpatra earned 500 total points
Comment Utility
I can suggest 3 options (3rd one requires least effort):

1. Provide a wrapper remote class around such classes. Let this class be completely stateless and accept the values in methods. You can have a class called UserProcessor and provide method like .Add(UserName as String, Password as String). Let this method instantiate/invoke User class to perform the operations.

2. Best approach is to redesign the solution. Have a data class called UserDate with getters and setters for name/password. Make this a Serializable class. Have a User Manager which is a remote object accepting this user class for all the operations.

3. The option with least possible effort (but highest overload) is to invoke the remote components as client-activated objects in stead of singleton objects. You can checkout the method RemotingConfiguration.RegisterActivatedClientType to register a client activated object. Check the following article.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeremotingremotingconfigurationclassregisteractivatedclienttypetopic.asp
0
 
LVL 3

Expert Comment

by:atxondo
Comment Utility
Why don´t ypu use the lock() sentence to stop these problems.
The lock sentence is a simple sentence that locks a variable until the sentences that must be executed during this lock sentence.
You must use a static object, that is unic in all the calls to that function. If the object is sattic and singleton you can use any object you want.
For example why don´t you try;

lock(database object)
{
     // Sentences that must be executed during the lock of the object
}

Lock() will stop any thread if another thread is inside the lock sentences.
0
 

Author Comment

by:aleborg
Comment Utility
skpatra -> Can you provide an example to your second solution?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now