Update Object


S

shapper

Hello,

I am updating a User entity as follows:

public void Update(User user) {
ISession session = new AppContext();
UserRepository repository = new UserRepository(session);
User _user = repository.GetById(user.Id);
_user = user;
session.Commit();
} // Update

I am not sure the fields will work. User is something like:

public class User {
public int Id { get; set; }
public string Name { get; set; }
public List<Role> Roles { get; set; }
public List<String> Emails { get; set; }
}

I would like to update the properties in _user which values are
different from user.

What is the best way to do this?

Thanks,
Miguel
 
Ad

Advertisements

P

Peter Duniho

Hello,

I am updating a User entity as follows:

public void Update(User user) {
ISession session = new AppContext();
UserRepository repository = new UserRepository(session);
User _user = repository.GetById(user.Id);
_user = user;
session.Commit();
} // Update

I am not sure the fields will work. User is something like:

public class User {
public int Id { get; set; }
public string Name { get; set; }
public List<Role> Roles { get; set; }
public List<String> Emails { get; set; }
}

I would like to update the properties in _user which values are
different from user.

I don't understand the question. After your program executes the
statement "_user = user", there are no "properties in _user which values
are different from user". Both variables would reference the same object
and so of course all properties would return identical values.

What are you really trying to do here?

Pete
 
S

shapper

I don't understand the question.  After your program executes the  
statement "_user = user", there are no "properties in _user which values  
are different from user".  Both variables would reference the same object  
and so of course all properties would return identical values.

What are you really trying to do here?

Pete

Let me try to explain it better.
Basically I get an user from the database and on a form some of those
user values might be changed.
Then I need to save that user changes so I call the Membership Service
Updated method.

If I would use only the repository I would do the following:

ISession session = new AppContext();
UserRepository repository = new UserRepository(session);
User _user = repository.GetById(1);
_user.Name="New Name"
session.Commit();

This would submit the change.

But in this case I have a service, Membership Service, over the
Repository.
So I don't know how to get this functionalitty.

In my MembershipService I have methods like:

public void Approve(Int32 id) {
ISession session = new BonsAlunosContext();
UserRepository repository = new UserRepository(session);
User user = repository.GetById(id);
user.Approved = true;
session.Commit();
} // Approve

public User GetById(Int32 id) {
ISession session = new BonsAlunosContext();
UserRepository repository = new UserRepository(session);
return repository.GetById(id);
} // GetById

public void SignOut() {
FormsAuthentication.SignOut();
} // SignOut

public void Update(User user) {
ISession session = new BonsAlunosContext();
UserRepository repository = new UserRepository(session);
User _user = repository.GetById(user.Id);
_user = user;
session.Commit();
} // Update

As you can see updating the user is the only one that has the problem
because it receives a user and it needs to save the changes,

I am not sure if I could this in a better way ... Maybe extending the
Unit Of Work pattern to the service but I am not sure how.

Do you understand now?

Thanks,
Miguel
 
P

Peter Duniho

[...]
As you can see updating the user is the only one that has the problem
because it receives a user and it needs to save the changes,

I am not sure if I could this in a better way ... Maybe extending the
Unit Of Work pattern to the service but I am not sure how.

Do you understand now?

Barely. And there is still not enough context for me to suggest a
solution. Your post doesn't include enough information to know where the
argument "User user" comes from, how it relates to the "_user" member
field, nor why there's a problem saving changes with the code example you
posted.

Your problem really doesn't seem to have anything at all to do with C#.
You should find a forum that is specific to the context in which you're
writing this code (ASP.NET? MVC.NET? I'm not familiar with whatever it
is you're using), so that you can find a large pool of people who are
actually experts on the topic. You _might_ get an answer here from
someone else, but it's a real shot in the dark hoping for that.

Pete
 
S

shapper

Your problem really doesn't seem to have anything at all to do with C#.  

This has nothing to do with ASP.NET MVC or ASP.NET.
It is really a matter of my C# implementation.

Let me try to show an example which may clarify all this:

First I get a User:

MembershipService service = new MembershipService();
User user = service.GetById(1)

Now the form updates some of the user information but let's just
update it in my C# code:
user.Name = "My new name";
user.Roles.Add(aNewRole);

Now I need to save this user. If I was using only the repository all
this could be done as follows:

ISession session = new AppContext();
UserRepository repository = new UserRepository(session);
User user = repository.GetById(1);
user.Name = "My new name";
user.Roles.Add(aNewRole);
session.Commit();

But because I am doing this through the Membership Service I think I
need a method or something that saves the user changes.

Did I explained it well?

Thanks,
Miguel
 
Ad

Advertisements

P

Peter Duniho

This has nothing to do with ASP.NET MVC or ASP.NET.
It is really a matter of my C# implementation.

Then please post a concise-but-complete C# program demonstrating the
problem and which has zero dependence on ASP.NET, "repositories", etc.,
and leave all mention of those things out of your question and any
associated descriptions.

Pete
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top