views:

99

answers:

1

I have a L2S generated class called Accounts, I have a L2S class called UsersInAccounts I need to add a function call AddUserToAccount(accountid, userid) should/could this function be added to the partial Accounts class I have created or are partial classes used for getting data rather than editing data

 public partial class Account
    {

        public void addUser(Guid userid)
        {
           // code
        }
    }
A: 

I don't think that what you are doing is a problem. In your code, you'd probably have an Account instance that you want to do things with so being able to do this:

Account theAccountIWant = GetTheAccount();
theAccountIWant.addUser(myUsersGUID);

...seems pretty intuitive. It might be an idea to do some error trapping inside your addUser method and pass back some sort of success status but that's another discussion.

edit: As advised, if you then retrieve a User object and want to attach it to the Account using the AccountUsers property then this is no use unless you pass the DataContext in.

Neil Trodden
the problem with my suggested approach is with the addUser function I cant get linq syntax to do this.UsersInAccounts.InsertOnSubmit(userinaccount)
monkeylee
I apologise, I did not realise you were adding sql entities. There's nothing wrong with your entity extensions changing data, but your issue there is that the DataContext will be out of scope.You can always pass that in as an extra argument to addUser but I admit that is a bit clunky. You might prefer to do that though if you want to keep that sort of thing in the partial class.
Neil Trodden
I guess that is why people see Linq to sql as a dal, looks like I need to create a Business Object Account class, is that what you would do in my senario?
monkeylee
It is! I extend the LINQToSQL entities (as you have done in your example) only when I want to make the entities a bit richer. For example, in one of my apps, I have a Customer entity that maps to my Customer SQL table. It has firstname and surname fields but I also create a FullName field that concatenates the two and fixes any capitalisation issues.I then have a class (which I actually call DAL) which has all the methods I have to call - AddCustomer, GetCustomer etc. I just don't call linq methods in my app other than via this class. It is a huge relief when making db changes in future.
Neil Trodden