<p>Now that <code>LINQ</code> to <code>SQL</code> is a little more mature, I'd like to know of any techniques people are using to create an <strong>n-tiered solution</strong> using the technology, because it does not seem that obvious to me.</p>

[{'answer_id': 38212, 'author': 'Artem Tikhomirov', 'author_id': 2313, 'author_profile': 'https://Stackoverflow.com/users/2313', 'pm_score': 1, 'selected': False, 'text': '<p>Hm, <a href="http://www.lhotka.net/" rel="nofollow noreferrer">Rockford Lhotka</a> sad, that LINQ to SQL is wonderful technology for fetching data from database. He suggests that afterwards they\'ll must to be bind to "reach domain objects" (aka. CSLA objetcs).</p>\n\n<p>Seriously speaking, LINQ to SQL had it\'s support for n-tier architecture see <strong>DataContext.Update</strong> method.</p>\n'}, {'answer_id': 38255, 'author': 'tbreffni', 'author_id': 637, 'author_profile': 'https://Stackoverflow.com/users/637', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to look into the <a href="http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework" rel="nofollow noreferrer">ADO .Net Entity Framework</a> as an alternative to LINQ to SQL, although it does support LINQ as well.  I believe LINQ to SQL is designed to be fairly lightweight and simple, whereas the Entity Framework is more heavy duty and probably more suitable in large Enterprise applications.</p>\n'}, {'answer_id': 38330, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Seriously speaking, LINQ to SQL had it\'s support for n-tier architecture see DataContext.Update method</p>\n</blockquote>\n\n<p>Some of what I\'ve read suggests that the business logic wraps the DataContext - in other words you wrap the update in the way that you suggest.  </p>\n\n<p>The way i traditionally write business objects i usually encapsulate the "Load methods" in the BO as well; so I might have a method named LoadEmployeesAndManagers that returns a list of employees and their immediate managers (this is a contrived example) .  Maybe its just me, but in my front end I\'d rather see e.LoadEmployeesAndManagers() than some long LINQ statement.  </p>\n\n<p>Anyway, using LINQ it would probably look something like this (not checked for syntax correctness):</p>\n\n<pre><code>var emps = from e in Employees\n                join m in Employees\n                on e.ManagerEmpID equals m.EmpID\n                select new\n                          { e,\n                            m.FullName\n                          };\n</code></pre>\n\n<p>Now if I understand things correctly, if I put this in say a class library and call it from my front end, the only way I can return this is as an IEnumerable, so I lose my strong typed goodness.  The only way I\'d be able to return a strongly typed object would be to create my own Employees class (plus a string field for manager name) and fill it from the results of my LINQ to SQL statement and then return that.  But this seems counter intuitive... what exactly did LINQ to SQL buy me if I have to do all that?</p>\n\n<p>I think that I might be looking at things the wrong way; any enlightenment would be appreciated.</p>\n'}, {'answer_id': 38551, 'author': 'liammclennan', 'author_id': 2785, 'author_profile': 'https://Stackoverflow.com/users/2785', 'pm_score': 0, 'selected': False, 'text': '<p>"the only way I can return this is as an IEnumerable, so I lose my strong typed goodness"</p>\n\n<p>that is incorrect. In fact your query is strongly typed, it is just an anonymous type. I think the query you want is more like:</p>\n\n<pre><code>var emps = from e in Employees\n            join m in Employees\n            on e.ManagerEmpID equals m.EmpID\n            select new Employee\n                      { e,\n                        m.FullName\n                      };\n</code></pre>\n\n<p>Which will return IEnumerable.</p>\n\n<p>Here is <a href="http://www.codeproject.com/KB/architecture/linqrepository.aspx" rel="nofollow noreferrer">an article</a> I wrote on the topic. </p>\n\n<p>Linq-to-sql is an ORM. It does not affect the way that you design an N-tiered application. You use it the same way you would use any other ORM.</p>\n'}, {'answer_id': 38716, 'author': 'crucible', 'author_id': 3717, 'author_profile': 'https://Stackoverflow.com/users/3717', 'pm_score': 2, 'selected': True, 'text': "<p>LINQ to SQL doesn't really have a n-tier story that I've seen, since the objects that it creates are created in the class with the rest of it, you don't really have an assembly that you can nicely reference through something like Web Services, etc.</p>\n\n<p>The only way I'd really consider it is using the datacontext to fetch data, then fill an intermediary data model, passing that through, and referencing it on both sides, and using that in your client side - then passing them back and pushing the data back into a new Datacontext or intellgently updating rows after you refetch them.</p>\n\n<p>That's if I'm understanding what you're trying to get at :\\</p>\n\n<p>I asked ScottGu the same question on his blog when I first started looking at it - but I haven't seen a single scenario or app in the wild that uses LINQ to SQL in this way.  Websites like Rob Connery's Storefront are closer to the provider.</p>\n"}, {'answer_id': 39204, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 0, 'selected': False, 'text': '<p>@liammclennan</p>\n\n<blockquote>\n  <p>Which will return IEnumerable. ... Linq-to-sql is an ORM. It does not affect the way that you design an N-tiered application. You use it the same way you would use any other ORM.</p>\n</blockquote>\n\n<p>Then I guess I am still confused.  Yes, Linq-to-Sql is an ORM; but as far as I can tell I am still littering my front end code with inline sql type statements (linq, not sql.... but still I feel that this should be abstracted away from the front end).</p>\n\n<p>Suppose I wrap the LINQ statement we\'ve been using as an example in a method.  As far as I can tell, the only way I can return it is this way:</p>\n\n<pre><code>public class EmployeesDAL\n{\n    public IEnumerable LoadEmployeesAndManagers()\n    {\n            MyCompanyContext context = new MyCompanyContext();\n\n            var emps = from e in context.Employees\n            join m in context.Employees\n            on e.ManagerEmpID equals m.EmpID\n            select new\n                      { e,\n                        m.FullName\n                      };\n\n            return emps;\n    }\n\n}\n</code></pre>\n\n<p>From my front end code I would do something like this:</p>\n\n<pre><code>EmployeesDAL dal = new EmployeesDAL;\nvar emps = dal.LoadEmployeesAndManagers();\n</code></pre>\n\n<p>This of course returns an IEnumerable; but I cannot use this like any other ORM like you say (unless of course I misunderstand), because I cannot do this (again, this is a contrived example):</p>\n\n<pre><code>txtEmployeeName.Text = emps[0].FullName\n</code></pre>\n\n<p>This is what I meant by "I lose strong typed goodness."  I think that I am starting to agree with Crucible; that LINQ-to-SQL was not designed to be used in this way.  Again, if I am not seeing things correctly, someone show me the way :)</p>\n'}, {'answer_id': 39251, 'author': 'Giovanni Galbo', 'author_id': 4050, 'author_profile': 'https://Stackoverflow.com/users/4050', 'pm_score': 1, 'selected': False, 'text': '<p>OK, I am going to give myself one possible solution.</p>\n\n<p>Inserts/Updates were never an issue; you can wrap the business logic in a Save/Update method; e.g.</p>\n\n<pre><code>public class EmployeesDAL\n{\n    ...\n    SaveEmployee(Employee employee)\n    {\n        //data formatting\n        employee.FirstName = employee.FirstName.Trim();\n        employee.LastName = employee.LastName.Trim();\n\n        //business rules\n        if(employee.FirstName.Length &gt; 0 &amp;&amp; employee.LastName.Length &gt; 0)\n        {\n            MyCompanyContext context = new MyCompanyContext();\n\n            //insert\n            if(employee.empid == 0)\n             context.Employees.InsertOnSubmit(employee);\n            else\n            {\n              //update goes here\n            }\n\n            context.SubmitChanges();\n\n\n        }\n        else \n          throw new BusinessRuleException("Employees must have first and last names");\n     }\n }\n</code></pre>\n\n<p>For fetching data, or at least the fetching of data that is coming from more than one table you can use stored procedures or views because the results will not be anonymous so you can return them from an outside method. For instance, using a stored proc:</p>\n\n<pre><code>    public ISingleResult&lt;GetEmployeesAndManagersResult&gt; LoadEmployeesAndManagers()\n    {\n        MyCompanyContext context = new MyCompanyContext();\n\n        var emps = context.GetEmployeesAndManagers();\n\n        return emps;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/38005', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4050/']