none
LinqToSql and WCF, where to put the linq-code? RRS feed

  • Question

  • I'm using LinqToSql and a WCF-service and don't know where to put the linq-code.

    The WCF is in one project and the LinqToSql-class + database in another project.

    My DataContext-class is partial with the following code in it:

            public IList<User> AllUsers
            {
                get
                {
                    return Users.ToList();
                }
            }
    
            public User GetUser(string partialName)
            {
                return (Users.Where(u => u.Name.Contains(partialName)).FirstOrDefault());
            }

    and my WCF service have the following code in it:

            public IList<LinqToSqlData.User> GetUserList()
            {
                using (LinqToSqlData.LinqToSqlDataContext dc = new LinqToSqlData.LinqToSqlDataContext())
                {
                    return dc.AllUsers;
                }
            }
    
            public LinqToSqlData.User GetUser(int id)
            {
                using (LinqToSqlData.LinqToSqlDataContext dc = new LinqToSqlData.LinqToSqlDataContext())
                {
                    return (from user in dc.Users
                               where user.Id == id
                               select user).FirstOrDefault();
                }
            }
    
            public LinqToSqlData.User GetUsr(string partialName)
            {
                using (LinqToSqlData.LinqToSqlDataContext dc = new LinqToSqlData.LinqToSqlDataContext())
                {
                    return dc.GetUser(partialName);
                }
            }

    and what I'm wondering is where to put the logic. I got two methods named GetUser and GetUsr. GetUser have the linq-code in the WCF and the GetUsr have the linq-code in the DataContext. Which should I use?

    Should I put the Linq-code in the WCF service or in the DataContext partial class?

    /Sasse

    Sunday, January 13, 2013 11:04 AM

Answers