Skip to content

Instantly share code, notes, and snippets.

@henrikherr
Created March 26, 2020 21:53
Show Gist options
  • Save henrikherr/29eb2913d403ab1d6bede52ed011869a to your computer and use it in GitHub Desktop.
Save henrikherr/29eb2913d403ab1d6bede52ed011869a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
namespace Export
{
class Program
{
public static int WheelCount = 0;
public static int CarCount = 0;
public static string path = $@"{Directory.GetCurrentDirectory()}\car.db";
public static string connStr = $"DataSource={path}";
public static SQLiteConnection Conn => new SQLiteConnection(connStr);
static void Main(string[] args)
{
CreateDbAndTableAndFill();
var repo = new CarRepo();
var allCars = repo.GetAllCarsWithoutQuery();
var carsMissingNested = repo.Cars.ToList();
Debugger.Break();
}
public class CarRepo
{
CarMapper mp = new CarMapper();
CarContext context = new CarContext(Conn);
public IEnumerable<Car> GetAllCarsWithoutQuery()
{
return mp.Map.Map<IEnumerable<Car>>(context.CarDtos);
}
public IQueryable<Car> Cars => Qry();
private IQueryable<Car> Qry()
{
return mp.Map.ProjectTo<Car>(context.CarDtos);
}
}
public class CarMapper
{
public IMapper Map => Config.CreateMapper();
IConfigurationProvider Config;
public CarMapper()
{
Config = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(CarMapper).Assembly);
});
Config.AssertConfigurationIsValid();
Config.BuildExecutionPlan(typeof(Car), typeof(CarDto));
}
}
public class CarContext : DataContext
{
public Table<CarDto> CarDtos;
public CarContext(IDbConnection connection) : base(connection)
{
}
}
[AutoMap(typeof(Car), ReverseMap = true)]
[Table(Name = "Cars")]
public class CarDto
{
public CarDto()
{
}
[Column(IsPrimaryKey = true)]
public Int64? Id { get; set; }
[Column]
public string Manufacturer { get; set; }
[Column]
public double Mileage { get; set; }
[Column]
public Int64? WheelId { get; set; }
[Column]
public string WheelManufacturer { get; set; }
}
public class Car
{
public Int64? Id { get; set; }
public string Manufacturer { get; set; } = "Porsche";
public double Mileage { get; set; }
public Wheel Wheel { get; set; }
public Car()
{
this.Id = Program.CarCount;
Mileage = new System.Random(Program.CarCount).Next(0, 500000);
Program.CarCount++;
}
public Car CreateWheels()
{
this.Wheel = new Wheel() { Id = Program.WheelCount };
Program.WheelCount++;
return this;
}
}
public class Wheel
{
public Int64? Id { get; set; }
public string Manufacturer { get; set; }
public Wheel()
{
Id = Program.WheelCount;
this.Manufacturer = "Continental";
}
}
private static void CreateDbAndTableAndFill()
{
if (!File.Exists(path))
{
SQLiteConnection.CreateFile(path);
var ctx = new CarContext(Conn);
ctx.ExecuteCommand(CreateQry());
var cars = new List<Car>();
foreach (var n in Enumerable.Range(0, 10))
cars.Add(new Car().CreateWheels());
var mp = new CarMapper();
ctx.CarDtos.InsertAllOnSubmit(mp.Map.Map<IEnumerable<CarDto>>(cars));
ctx.SubmitChanges();
}
}
public static string CreateQry()
{
return
@"CREATE TABLE IF NOT EXISTS Cars( Id INTEGER PRIMARY KEY NOT NULL,
Manufacturer TEXT,
Mileage REAL,
WheelId INTEGER,
WheelManufacturer TEXT)";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment