I wrote service layer code as follows:
public GetAllCategoriesResponse GetAllCategories()
{
GetAllCategoriesResponse response = new GetAllCategoriesResponse();
IEnumerable<Category> categories = _categoryRepository.GetAll();
response.Categories = categories.ConvertToCategoryViews();
return response;
}
Then I wrote my Test class as follows:
[TestClass]
public class ProductCatalogServiceTest
{
Mock<ICategoryRepository> _categoryRepository;
ProductCatalogService _catalogService;
[TestInitialize]
public void Setup()
{
_categoryRepository = new Mock<ICategoryRepository>();
_catagoryService = new ProductCatagoryService( _categoryRepository.Object);
}
[TestMethod]
public void Test_All_Catagories()
{
AutoMapperBootStrapper.ConfigureAutoMapper();
var catagories = new List<Category>();
catagories.Add(new Category() {Id=1, Name = "Half Shirt" });
catagories.Add(new Category() {Id=2, Name = "Full Shirt" });
catagories.Add(new Category() {Id=4, Name = "Pant" });
_categoryRepository.Setup(m => m.GetAll()).Returns(catagories.AsEnumerable());
var result = _catagoryService.GetAllCategories();
var response = new GetAllCategoriesResponse();
response.Categories = catagories.ConvertToCategoryViews();
Assert.AreEqual(response, result);
}
}
The mapping between Category and CategoryView as follows:
public class AutoMapperBootStrapper
{
public static void ConfigureAutoMapper()
{
// Category
Mapper.CreateMap<Category, CategoryView>();
}
}
When I run my test is shows me following message:
Assert.AreEqual failed. Expected: Shoppingcart.Services.Messages.GetAllCategoriesResponse>. Actual:Shoppingcart.Services.Messages.GetAllCategoriesResponse>.
Related
I am using Moq and want to verify the contents of a List parameter that is passed to the method.
Now how can I verify if the called Write Method,
public class DBStore
{
public virtual void Write(string retailName, List<CustomerInfo> list, List<Order> orderList)
{
}
}
had Customer Name "a" and the count?
public class CustomerInfo
{
public string Name;
public int CustomerInfoId;
}
I am able to verify string but not contents of a given list or the count.
[TestClass]
public class UnitTests
{
[TestMethod]
public void TestCustomer()
{
var dbStore = new Mock<DBStore>();
dbStore.Setup(x => x.Write(
It.IsAny<string>(),
It.IsNotNull<List<CustomerInfo>>(),
It.IsNotNull<List<Order>>()));
var updateInfo = new UpdateInfo(dbStore.Object);
updateInfo.UpdateCustomer();
dbStore.Verify(
o => o.Write(
"Walmart",
It.IsNotNull<List<CustomerInfo>>(),
It.IsNotNull<List<Order>>()));
}
}
Can I use IsAny<> in the Verify method? If so, how?
Supporting classes:
public class Order
{
public int CustomerInfoId;
public string Details;
public int OrderId;
}
public class UpdateInfo
{
public DBStore DB { get;set; }
public UpdateInfo(DBStore dbStore)
{
this.DB = dbStore;
}
public void UpdateCustomer()
{
// Logic to get some more info and get retail name
string retailName = "walmart";
List<CustomerInfo> customerList = new List<CustomerInfo>();
customerList.Add(new CustomerInfo { Name = "a", CustomerInfoId = 1 });
customerList.Add(new CustomerInfo { Name = "b", CustomerInfoId = 2 });
List<Order> orderList = new List<Order>();
orderList.Add(new Order { OrderId = 1, CustomerInfoId = 1 });
orderList.Add(new Order { OrderId = 2, CustomerInfoId = 1 });
this.DB.Write(retailName, customerList, orderList);
}
}
I am trying to write a unit test for My Processor class I have two problems
I do not know how to test my Methods Only.
this is my processor
OrderProcessor class
public class OrderProcessor
{
public void Process(CustomersOrder order)
{
var oldOrder = _repository.GetOldorderId(order.Code.Value);
if (oldOrder != 0)
{
updateOrder(order);
}
else
{
SaveOrder(order);
}
}
private void updateOrder(CustomersOrder order)
{
_repository.UpdateOrder(order);
}
private void SaveOrder(CustomersOrder order)
{
_repository.SaveOrder(order);
}
}
}
Repository class
public class Repository : IRepository
{
private static PracticeEntities4 _context;
public Repository(PracticeEntities4 context)
{
_context = context;
}
public int GetOldCustomerId( int customerCode)
{
var CuID= _context.Customers.First(e => e.Code == customerCode);
return CuID.Id;
}
public int GetOldorderId(int orderCode)
{
var oldOrder = _context.CustomersOrders.FirstOrDefault(e => e.Code == orderCode);
return oldOrder.Id;
}
public void SaveCustomer(Customer customer)
{
_context.Customers.Add(customer);
_context.SaveChanges();
}
public void SaveOrder(CustomersOrder order)
{
_context.CustomersOrders.Add(order);
_context.SaveChanges();
}
public void UpdateOrder(CustomersOrder order)
{
_context.CustomersOrders.AddOrUpdate(order);
_context.SaveChanges();
}
}
and this is My unit test I don't know how to fix it and where is the problem exactly and also I want to test the Methods too.
UnitTests Class
[TestClass]
public class OrderProcessorTest
{
[ClassInitialize]
{...}
[TestInitialize]
public void TestInitialize()
{
....
}
[TestMethod]
public void Customer_OrderProcess()
{
//Arange
Mock<IRepository> mock= new Mock<IRepository>();
//Act
mock.Setup(e => e.GetOldCustomerId(1001)).Returns(3);
mock.Setup(e => e.GetOldStoreId(200)).Returns(3);
var dtos = OrderDeserializer.Deserialize(path);
var dto = dtos.First(e => e.Code == 300);
OrderBuilder builder = new OrderBuilder(mock.Object);
builder.OrderBuild(dto);
//Asset
Assert.AreEqual(0, _orders.Count);
}
}
Order Builder Class
public class OrderBuilder
{
public IRepository _repository { get; set; }
public OrderBuilder(IRepository repository)
{
_repository = repository;
}
public CustomersOrder OrderBuild(OrderDto dto)
{
var oldStoreId = _repository.GetOldStoreId(dto.StoreCode);
var oldCustomerId = _repository.GetOldCustomerId(dto.CustomerCode);
return new CustomersOrder()
{
OrderDate = Convert.ToDateTime(dto.OrderDate),
OrderStatus = dto.OrderStatus,
DeliveryDate = Convert.ToDateTime(dto.DeliveryDate),
CustomerId = oldCustomerId,
StoreId = oldStoreId,
Code = dto.Code
};
}
}
In your code I see that there are all sorts of Mocking and Initial test setups that is taking place without a clear intention on what to test.
Unit Test: What ?
Tests a unit of an application without its external dependencies
Unit Test: Why ?
Makes refactoring faster and ensures you don't break existing portion of your code
Unit Test: Steps ?
We first need to re-factor the code before we do unit tests. Modularity is the key
By using Interfaces remove the tight couplings in the code
Inject the dependency via method parameters, constructor, properties or use Dependency Injection
Consider using Mock objects, as a good practice, only when dealing with external dependency.
In the [TestMethod] we organize the tests into 3 categories Arrange -> Act -> Assert
Example:
//Arrange
var res = new Reservation();
//Act
var op = res.Method(new User{IsAdmin=true});
// Assert
Assert.IsTrue(op);
Naming Conventions in UnitTests:
TestProjectName: [InserProjectName].UnitTests
TestClasses: [InsertClassName]Tests
TestMethod: [MethodYourTesting]_[Scenario]_[ExpectedBehavior]
I have created a Console app as close as possible to your problem
(minus the DBContext) that you can replicate on your PC to understand
the various portions.
All the domain classes are part of one single
file for the sake of testability to reproduce faster.
Console App Project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackOrderProcessor
{
public class CustomersOrder
{
public OrderDto Order { get; set; }
public List<CustomersOrder> CustomersOrders = new List<CustomersOrder>();
public DateTime OrderDate { get; set; }
public string OrderStatus { get; set; }
public int CustomerID { get; set; }
public int Code { get; set; }
public int ID { get; set; }
}
public class Customer
{
public OrderDto Order { get; set; }
public List<Customer> Customers = new List<Customer>();
public int Code { get; set; }
public int ID { get; set; }
}
public class OrderDto
{
public DateTime OrderDate { get; set; }
public int CustomerCode { get; set; }
public string OrderStatus { get; set; }
public int Code { get; set; }
}
public interface IRepository
{
int GetOldCustomerId(int customerCode);
int GetOldOrderId(int orderCode);
void SaveCustomer(Customer customer);
void SaveOrder(CustomersOrder order);
}
public class Repository : IRepository
{
private readonly Customer _cust;
private readonly CustomersOrder _custOrder;
public Repository(Customer cust, CustomersOrder custOrder )
{
_cust = cust;
_custOrder = custOrder;
}
public int GetOldCustomerId(int customerCode)
{
var cuId = _cust.Customers.First(e => e.Code == customerCode);
return cuId.ID;
}
public int GetOldOrderId(int orderCode)
{
var oId = _custOrder.CustomersOrders.FirstOrDefault(e => e.Code == orderCode);
return oId.ID;
}
public void SaveCustomer(Customer customer)
{
_cust.Customers.Add(customer);
}
public void SaveOrder(CustomersOrder order)
{
_custOrder.CustomersOrders.Add(order);
}
}
public class OrderProcess
{
private readonly IRepository _repository;
public OrderProcess(IRepository repository)
{
_repository = repository;
}
public void Process(CustomersOrder order)
{
var oldOrder = _repository.GetOldOrderId(order.Code);
if (oldOrder == 0)
_repository.SaveOrder(order);
}
}
public class OrderBuilder
{
private readonly IRepository _repository;
public OrderBuilder(IRepository repository)
{
_repository = repository;
}
public CustomersOrder OrderBuild(OrderDto dto)
{
var oldCustomerId = _repository.GetOldCustomerId(dto.CustomerCode);
return new CustomersOrder()
{
Order = dto,
OrderDate = Convert.ToDateTime(dto.OrderDate),
OrderStatus = dto.OrderStatus,
ID = oldCustomerId,
CustomerID = oldCustomerId,
Code = dto.Code
};
}
}
class Program
{
static void Main(string[] args)
{
var cust = new Customer();
var custOrder = new CustomersOrder();
#region PopulatingCustomer
//Populating OrderDto
var dto1 = new OrderDto { Code = 1, CustomerCode = 1, OrderDate = DateTime.Now.Date, OrderStatus = "OK" };
//Populating Customer
var customerList = cust.Customers = new List<Customer>();
var customerOrderList = custOrder.CustomersOrders = new List<CustomersOrder>();
var customer1 = new Customer
{
Code = 1,
ID = 1, Order=dto1
};
var customer2 = new Customer
{
Code = 2,
ID = 2,
};
customerList.Add(customer1);
customerList.Add(customer2);
#endregion
#region PopulatingCustomerOrder
var customersOrder1 = new CustomersOrder { Code = 1, CustomerID = 1, ID = 1, Order = dto1, OrderDate = dto1.OrderDate, OrderStatus = dto1.OrderStatus };
customerOrderList.Add(customersOrder1);
#endregion
#region InvokingMethods
//IRepository
IRepository IRepo = new Repository(cust,custOrder);
//OrderProcessor
var orderProcesor = new OrderProcess(IRepo);
//OrderBuilder
var dto2 = new OrderDto { Code = 2, CustomerCode = 2, OrderDate = DateTime.Now.Date, OrderStatus = "OK" };
var oBuilder = new OrderBuilder(IRepo);
var newCustOrder = oBuilder.OrderBuild(dto2);
customerOrderList.Add(newCustOrder);
#endregion
Console.Read();
}
}
}
UnitTest Project
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StackOrderProcessor;
namespace StackOrderProcessor.UnitTests
{
[TestClass]
public class RepositoryTests
{
[TestMethod]
public void GetOldCustomerId_WhenCalled_ReturnsOId()
{
//Arrange
var cust = new Customer();
var custOrder = new CustomersOrder();
IRepository repo = new Repository(cust,custOrder);
var customerList = cust.Customers = new List<Customer>();
var dto1 = new OrderDto { Code = 1, CustomerCode = 1, OrderDate = DateTime.Now.Date, OrderStatus = "OK" };
var customer1 = new Customer
{
Code = 1,
ID = 1,
Order = dto1
};
var customer2 = new Customer
{
Code = 2,
ID = 2,
};
customerList.Add(customer1);
customerList.Add(customer2);
//Act
repo.GetOldCustomerId(1);
//Assert
Assert.AreEqual(1, 1); //Test will Pass as we have a customer of Code 1
}
[TestMethod]
//MethodName_Scenario_Expectedbehavior
public void SaveCustomer_WhenCalled_AddsNewCustomer()
{
var cust = new Customer();
var custOrder = new CustomersOrder();
IRepository repo = new Repository(cust, custOrder);
var customerList = cust.Customers = new List<Customer>();
var dto1 = new OrderDto { Code = 1, CustomerCode = 1, OrderDate = DateTime.Now.Date, OrderStatus = "OK" };
var customer1 = new Customer
{
Code = 1,
ID = 1,
Order = dto1
};
var customer2 = new Customer
{
Code = 2,
ID = 2,
};
customerList.Add(customer1);
customerList.Add(customer2);
//Act
var custToSave = new Customer
{
Code = 3,
ID = 3,
Order = null
};
repo.SaveCustomer(custToSave);
//Assert
Assert.AreEqual(3, customerList.Count);
}
}
[TestClass]
public class OrderProcessor1Tests
{
[TestMethod]
public void Process_WhenOrderIsZero_AddsNewCustomerOrder()
{
//Arrange
var cust = new Customer();
var custOrder = new CustomersOrder();
var customerOrderList = custOrder.CustomersOrders = new List<CustomersOrder>();
IRepository repo = new Repository(cust, custOrder);
var orderProcessor = new OrderProcess(repo);
var dto1 = new OrderDto { Code = 1, CustomerCode = 1, OrderDate = DateTime.Now.Date, OrderStatus = "OK" };
var custOrder1 = new CustomersOrder { ID = 1, Code = 1, CustomerID = 1, Order = dto1, OrderDate = dto1.OrderDate, OrderStatus = dto1.OrderStatus };
customerOrderList.Add(custOrder1);
//Act
orderProcessor.Process(custOrder1);
//Assert
Assert.AreEqual(1, customerOrderList.Count);
}
}
}
Note: Make sure to add reference of StackOrderProcessor in StackOrderProcessor.UnitTests
You will still need to better organize the Unit Test Methods, this was just for demonstration purposes, I hope concepts are much more clear now
Your question doesn't have enough information, you don't need your repository code for this question, but OrderBuiler class and _orders field needs. I'm sorry for this comment in answers location.
I have a handler class that takes in a Query class with 3 properties. The handler returns a PagedViewModel object. I am trying to test that this handler is returning what it is supposed to return, but I cannot get my test method to work properly.
I attempted to create a PagedViewModel and assert that the test viewModel and the actual viewModel were equal but this is not returning what I would expect. (It is just a regular PagedViewModel not a Mock).
My test results look identical, so I am confused as to what the problem is.
My handler
protected override PagedViewModel<AlertModel> HandleCore(AlertsByOrgQuery request)
{
List<AlertModel> alertsModel = new List<AlertModel>();
List<long> alertTypes = new List<long>();
Expression<Func<Alert, bool>> query = PredicateBuilder.True<Alert>();
if (request.AlertTypes?.Any() == true) {
foreach (var alertType in request.AlertTypes) {
alertTypes.Add((int)alertType);
}
}
PagedList<Alert> pagedAlerts;
try {
var alertsQuery = BuildQuery(request, alertTypes);
pagedAlerts = new PagedList<Alert>(alertsQuery, request.Page, request.Rows);
} catch (Exception ex) {
_logger.Error("Error loading alerts.", ex);
return null;
}
var viewModel = new PagedViewModel<AlertModel>();
viewModel.PageNumber = pagedAlerts.PageNumber;
viewModel.TotalRecordCount = pagedAlerts.TotalItemCount;
viewModel.NumOfPages = pagedAlerts.PageCount;
viewModel.Rows = pagedAlerts.Select(a => new AlertModel
{
Id = a.Id,
TimeStamp = a.TimeStamp,
Message = a.Message,
EntityType = DataEntity.EnumDescription((HIMS.Entities.Alert.AlertType)Convert.ToInt32(a.EntityType))
});
return viewModel;
}
My query class
public class AlertsByOrgQuery : DataPageModel, IRequest<PagedViewModel<AlertModel>>
{
public bool ShowUnread { get; set; }
public IEnumerable<HIMS.Entities.Alert.AlertType> AlertTypes { get; set; }
public HIMS.Entities.Authorization Authorization { get; set; }
}
My Test Method
public async Task HandleCoreGivenValidRequestShouldReturnViewModel() {
//arrange
var repo = new OptionalOrgAuthorizableRepo<Alert>(_mockDb.Object, _auth.OrganizationId);
var query = PredicateBuilder.True<Alert>();
var _testAlerts = repo.Get(query);
PagedList<Alert> pagedAlerts = new PagedList<Alert>(_testAlerts.AsQueryable(), 2, 3);
_mockViewModel = new PagedViewModel<AlertModel>();
_mockViewModel.PageNumber = pagedAlerts.PageNumber;
_mockViewModel.TotalRecordCount = pagedAlerts.TotalItemCount;
_mockViewModel.NumOfPages = pagedAlerts.PageCount;
_mockViewModel.Rows = pagedAlerts.Select(a => new AlertModel {
Id = a.Id,
TimeStamp = a.TimeStamp,
Message = a.Message,
EntityType = DataEntity.EnumDescription((Entities.Alert.AlertType)Convert.ToInt32(a.EntityType))
});
var handler = new AlertsByOrgHandler(_mockLogger.Object, _mockDb.Object);
var request = new AlertsByOrgQuery() {
ShowUnread = false,
AlertTypes = new List<Entities.Alert.AlertType> { (Entities.Alert.AlertType)Alert.AlertType.Case },
Authorization = _auth,
SIdx = "EntityType",
SOrd = "desc",
Page = 2,
Rows = 3
};
//act
var actual = await handler.Handle(request, new CancellationToken());
//assert
Assert.AreEqual(_mockViewModel, actual);
}
}
I am trying to implement a reflexive association in C# sharp but couldn't find any example on the web.
I have come up with following
class Employee
{
public string Name { get; set; }
public Employee Boss { get; set; }
public List<Employee> Junoirs;
public Employee (string name)
{
Junoirs = new List<Employee>();
Name = name;
}
static void Main(string[] args)
{
Employee tom = new Employee("Tom");
Employee marry = new Employee("Marry");
Employee jhon = new Employee("Jhon");
Employee foo = new Employee("Foo");
Employee bar = new Employee("Bar");
tom.Junoirs.AddRange(new Employee[] { marry, jhon });
marry.Boss = tom;
jhon.Boss = tom;
marry.Junoirs.AddRange(new Employee[] { foo, bar });
foo.Boss = marry;
bar.Boss = marry;
}
}
Is this a valid example of reflexive association?
How can I automatically add tom as Boss of employees marry and jhon as I add them to list of Junoirs of tom?
You can use methods to add/remove juniors.
If you need add/remove-functionality on the Juniors property, you can implement your own IList or ICollection which handles the book-keeping.
public class Employee
{
public string Name { get; set; }
public Employee Boss
{
get { return _boss; }
set
{
_boss?.RemoveJunior(this);
value?.AddJunior(this);
}
}
public IReadOnlyList<Employee> Juniors => _juniors.AsReadOnly();
private Employee _boss = null;
private readonly List<Employee> _juniors = new List<Employee>();
public Employee(string name)
{
Name = name;
}
public void AddJunior(Employee e)
{
// Remove from existing boss' list of employees
// Can't set Boss property here, that would create infinite loop
e._boss?.RemoveJunior(e);
_juniors.Add(e);
e._boss = this;
}
public void RemoveJunior(Employee e)
{
_juniors.Remove(e);
e._boss = null;
}
}
public class EmployeeTests
{
[Fact]
public void SettingBoss_AddsToEmployee()
{
var b = new Employee("boss");
var e1 = new Employee("1");
e1.Boss = b;
Assert.Same(b, e1.Boss);
Assert.Contains(e1, b.Juniors);
}
[Fact]
public void AddEmployee_SetsBoss()
{
var b = new Employee("boss");
var e1 = new Employee("1");
b.AddJunior(e1);
Assert.Same(b, e1.Boss);
Assert.Contains(e1, b.Juniors);
}
[Fact]
public void NullBoss_RemovesEmployee()
{
var b = new Employee("boss");
var e1 = new Employee("1");
b.AddJunior(e1);
e1.Boss = null;
Assert.Null(e1.Boss);
Assert.DoesNotContain(e1, b.Juniors);
}
[Fact]
public void RemoveEmployee_NullsBoss()
{
var b = new Employee("boss");
var e1 = new Employee("1");
b.AddJunior(e1);
b.RemoveJunior(e1);
Assert.Null(e1.Boss);
Assert.DoesNotContain(e1, b.Juniors);
}
}
Ill keep the question short, as i suspect the answer may be a short no..
I would like to be able to pass a query into an entity framework function, to enable dynamic runtime querying through the ui to the database?
I imagined this to be like passing an IQueryable into a method, but exactly how i would go about this, i am a little unsure at the moment. Am i thinking about this the wrong way? Perhaps querying in the business layer, not semi-directly to the database?
Based on the comments, I'm providing the two options.
Based on a set of options given to the user.
Use a TreeExpression to build you expression dynamically, somthing like the example above (this example is indeed too simple, but you can have an idea).
ParameterExpression numParam = Expression.Parameter(typeof(int), "num");
ConstantExpression five = Expression.Constant(5, typeof(int));
BinaryExpression numLessThanFive = Expression.LessThan(numParam, five);
Expression<Func<int, bool>> lambda1 = Expression.Lambda<Func<int, bool>>(numLessThanFive, new ParameterExpression[] { numParam });
This link will give you some more info on the subject: https://msdn.microsoft.com/en-us/library/bb882637.aspx
For letting the user type some expression and converting it to a query, search for Antlr http://www.antlr.org/, or some tool like this, padronize the expression syntax you want to implement and go for the 1º solution to build the expression.
My example code is below. You'll need to install the following packages...
install-package entityframework
install-package newtonsoft.json
Be aware that this code is susceptible to injection by inserting valid VB.NET code to escape the query.
I compiled the code into 30742268.exe, which you can see is added as a reference for the IContext interface, etc.
using System;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;
using System.CodeDom.Compiler;
using Model;
using System.Collections.Generic;
using System.Data.Entity;
using Newtonsoft.Json;
namespace _30742268 {
class Program {
const String queryWrapperCode = #"
Imports System.Linq
Imports System.Data.Entity
Imports Model
Public Class DynamicQuery
Implements IDynamicQuery
Public Function Run(data As IContext) As IQueryable Implements IDynamicQuery.Run
Return {0}
End Function
End Class
";
static void Main(String[] args) {
using (var provider = new VBCodeProvider()) {
var parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("EntityFramework.dll");
parameters.ReferencedAssemblies.Add("30742268.exe");
parameters.GenerateInMemory = true;
Console.WriteLine("Enter LINQ queries, 'demo' for an example, 'exit' to stop:");
for (;;) {
try {
var dynamicQueryString = Console.ReadLine();
if (dynamicQueryString == "exit")
return;
if (dynamicQueryString == "demo")
Console.WriteLine(dynamicQueryString = "from person in data.People where person.Name.Length = 4");
var results = provider.CompileAssemblyFromSource(parameters, String.Format(queryWrapperCode, dynamicQueryString));
if (results.Errors.HasErrors) {
var sb = new StringBuilder();
foreach (CompilerError error in results.Errors) {
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
var assembly = results.CompiledAssembly;
var assemblyType = assembly.GetTypes().Single(x => typeof (IDynamicQuery).IsAssignableFrom(x));
var constructorInfo = assemblyType.GetConstructor(new Type[] {});
var dynamicQuery = (IDynamicQuery) constructorInfo.Invoke(null);
using (var context = new Context()) {
dynamic result = dynamicQuery.Run(context);
foreach (var person in result)
Console.WriteLine(person);
}
}
catch (Exception exception) {
Console.WriteLine(exception);
}
}
}
}
}
}
namespace Model {
public interface IDynamicQuery {
IQueryable Run(IContext context);
}
public abstract class Entity {
public override String ToString() {
return JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
}
}
public class Person : Entity {
public Int64 Id { get; protected set; }
public String Name { get; set; }
public virtual Home Home { get; set; }
}
public class Home : Entity {
public Int64 Id { get; protected set; }
public String Address { get; set; }
public virtual ICollection<Person> Inhabitants { get; set; }
}
public interface IContext {
IQueryable<Person> People { get; set; }
IQueryable<Home> Homes { get; set; }
}
public class Context : DbContext, IContext {
public virtual DbSet<Person> People { get; set; }
public virtual DbSet<Home> Homes { get; set; }
IQueryable<Person> IContext.People {
get { return People; }
set { People = (DbSet<Person>)value; }
}
IQueryable<Home> IContext.Homes {
get { return Homes; }
set { Homes = (DbSet<Home>)value; }
}
public Context() {
Configuration.ProxyCreationEnabled = false;
Database.SetInitializer(new ContextInitializer());
}
}
class ContextInitializer : DropCreateDatabaseAlways<Context> {
protected override void Seed(Context context) {
var fakeSt = new Home {Address = "123 Fake St."};
var alabamaRd = new Home {Address = "1337 Alabama Rd."};
var hitchhikersLn = new Home {Address = "42 Hitchhiker's Ln."};
foreach (var home in new[] {fakeSt, alabamaRd, hitchhikersLn})
context.Homes.Add(home);
context.People.Add(new Person { Home = fakeSt , Name = "Nick" });
context.People.Add(new Person { Home = fakeSt , Name = "Paul" });
context.People.Add(new Person { Home = fakeSt , Name = "John" });
context.People.Add(new Person { Home = fakeSt , Name = "Henry" });
context.People.Add(new Person { Home = alabamaRd , Name = "Douglas" });
context.People.Add(new Person { Home = alabamaRd , Name = "Peter" });
context.People.Add(new Person { Home = alabamaRd , Name = "Joshua" });
context.People.Add(new Person { Home = hitchhikersLn, Name = "Anne" });
context.People.Add(new Person { Home = hitchhikersLn, Name = "Boris" });
context.People.Add(new Person { Home = hitchhikersLn, Name = "Nicholes" });
context.People.Add(new Person { Home = hitchhikersLn, Name = "Betty" });
context.SaveChanges();
}
}
}