Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Created February 25, 2014 22:16
Show Gist options
  • Save hagbarddenstore/9219062 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/9219062 to your computer and use it in GitHub Desktop.
A simple project structure which separates business layer from the infrastructure layer and the application layer.
/**
* Solution
* |- Domain
* | `- Customers
* | |- Customer.cs
* | `- ICustomersRepository.cs
* |- Infrastructure
* | `- Customers
* | `- LinqToSqlCustomersRepository.cs
* `- Application
* `- Program.cs
*
* Domain references nothing.
* Infrastructure refences both Linq To Sql and Domain.
* Application references both Domain and Infrastructure.
*
* LinqToSqlCustomersRepository.cs maps Customer against the Database and
* implements the ICustomersRepository.cs interface.
*/
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
public interface ICustomersRepository
{
IEnumerable<Customer> FindAll();
}
public class LinqToSqlCustomersRepository : ICustomersRepository
{
public IEnumerable<Customer> FindAll()
{
using (var context = new MyDbContext())
{
var customers = context.Customers.OrderBy(x => x.Name)
.Select(x => new Customer { Id = x.Id, x.Name });
// NOTE: .ToList() is needed to force immediate execution since we
// are disposing the context.
return customers.ToList();
}
}
}
public class Program
{
public static void Main()
{
var customersRepository = new LinqToSqlCustomersRepository();
PrintAllCustomers(customersRepository);
Console.WriteLine("Press Enter to exit...");
Console.ReadLine();
}
private void PrintAllCustomers(ICustomersRepository customersRepository)
{
foreach (var customer in customersRepository.FindAll())
{
Console.WriteLine("{{ \"id\": \"{0}\", \"name\": \"{1}\" }}",
customer.Id, customer.Name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment