<p>I recently started a new webforms project and decided to separate the business classes from any DBML references. My business layer classes instead access discrete Data layer methods and are returned collections of DTO's. So the data layer might project DTO's like the following:</p>

<pre><code>(from c in dataContext.Customers
where c.Active == true 
select new DTO.Customer
{
   CustomerID = c.CustomerID,
   Name = c.CustomerName,
   ...
}).ToList()
</code></pre>

<p>Although building the DTO objects adds work, this feels like a better approach to a tight binding between Business &amp; Data layers and means I can test the Business layer without a database being present.</p>

<p>My question is, is this good practice?, Is there a way of generating the DTO's (maybe via SQLMetal), and what other problems might I strike as the project progresses.</p>

[{'answer_id': 51186, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': '<p>In my opinion in most cases DTO objects are not needed when dealing with LINQ. Generated LINQ classes can be easily tested. LINQ gives you ability to query your data from different sources using identical queries. It gives you ability to test your queries against lists of objects instead of real db.</p>\n'}, {'answer_id': 51196, 'author': 'BirgerH', 'author_id': 5164, 'author_profile': 'https://Stackoverflow.com/users/5164', 'pm_score': 4, 'selected': True, 'text': "<p>I don't know if it's best practice but I have written similar code in the not so recent past because I too felt that I could improve the separation of concerns by using my own classes instead of the LINQ-designer-generated ones within my application.</p>\n\n<p>You may want to consider just returning an IQueryable&lt;Customer&gt; instead of an IList&lt;Customer&gt; from your data-access method. Since IQueryable&lt;T&gt; inherits from IEnumerable&lt;T&gt; the rest of your app should be able to deal with it quite well. You can also convert it to a List when you really need to.</p>\n\n<p>The advantage of this is that you can dynamically modify your query quite easily and minimze the amount of data returned from SQL Server.</p>\n\n<p>E.g. if your method signature is\nIQueryable&lt;Customer&gt; GetCustomers() you could get a single customer by calling GetCustomers().Where(c => c.CustomerID == 101).Single();</p>\n\n<p>In this example only one record would be returned from the database whereas I imagine currently your code would return either all customers or you'd be required to write separate methods (and thus very repetitive code) to cater for all the different things you may want to filter by.</p>\n"}]
['https://Stackoverflow.com/questions/51176', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5136/']