Created
April 27, 2018 15:11
-
-
Save Roemer/f4f26321e30ca85c7f03cd31e76e865e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #r "System.Data" | |
| using System.Data; | |
| public void Test() | |
| { | |
| int id = 1; | |
| var car = EntityHandler.Get<Car>(new Query().Eq(CarMapping.Fields.Id, Id)); | |
| car.Extended = EntityHandler.Get<ExtendedCar>(new Query().Eq(ExtendedCarMapping.Fields.CarId, Id)); | |
| var multipleResults = EntiyHandler.Multiple(); | |
| } | |
| public interface IFields | |
| { } | |
| public interface ITable | |
| { } | |
| public interface IMeta | |
| { } | |
| public interface IMapping | |
| { } | |
| public abstract class EntityBase | |
| { | |
| protected T GetValue<T>(MappingField field) | |
| { | |
| return default; | |
| } | |
| protected void SetValue<T>(MappingField field, T value) | |
| { | |
| } | |
| } | |
| public class Car : EntityBase | |
| { | |
| static Car() | |
| { | |
| MappingProvider.AddMapping(typeof(Car), CarMapping.Instance); | |
| } | |
| public int Id | |
| { | |
| get => GetValue<int>(CarMapping.Fields.Id); | |
| set => SetValue(CarMapping.Fields.Id, value); | |
| } | |
| public DateTime CreateDate { get; set; } | |
| } | |
| public class ExtendedCar : Car | |
| { | |
| static ExtendedCar() | |
| { | |
| MappingProvider.AddMapping(typeof(ExtendedCar), ExtendedCarMapping.Instance); | |
| } | |
| public int Horsepower { get; set; } | |
| } | |
| public class CarFields : IFields | |
| { | |
| public readonly MappingField Id = new MappingField("ID", DbType.Int32).Identity(true).Primary(true); | |
| public readonly MappingField CreateDate = new MappingField("CreateDate", DbType.DateTime); | |
| public readonly MappingField ChangeDate = new MappingField("ChangeDate", DbType.DateTime); | |
| public readonly MappingField Make = new MappingField("Make", DbType.AnsiString); | |
| public readonly MappingField Model = new MappingField("Model", DbType.AnsiString); | |
| } | |
| public class CarTable : ITable | |
| { | |
| public string Name | |
| { get; set; } | |
| } | |
| public class ExtendedCarFields : CarFields | |
| { | |
| public readonly MappingField Horsepower = new MappingField("Horsepower", DbType.Int32); | |
| } | |
| public class CarMapping : Singleton<CarMapping>, IMapping | |
| { | |
| public static CarFields Fields = new CarFields(); | |
| public static CarMeta Meta = new CarMeta(); | |
| } | |
| public class ExtendedCarMapping : Singleton<ExtendedCarMapping>, IMapping | |
| { | |
| public static ExtendedCarFields Fields = new ExtendedCarFields(); | |
| } | |
| public abstract class Singleton<T> where T : class, new() | |
| { | |
| public static T Instance { get; } = new T(); | |
| } | |
| public class MappingField | |
| { | |
| public MappingField(string fieldName, DbType dbType) | |
| { | |
| } | |
| public MappingField Primary(bool isPrimary) | |
| { | |
| PrimaryIndex = isPrimary ? 0 : -1; | |
| return this; | |
| } | |
| public MappingField Identity(bool isIdentity) | |
| { | |
| IsIdentity = isIdentity; | |
| return this; | |
| } | |
| public int PrimaryIndex { get; set; } | |
| public bool IsIdentity { get; set; } | |
| } | |
| public static class MappingProvider | |
| { | |
| private static Dictionary<Type, IMapping> _dict = new Dictionary<Type, IMapping>(); | |
| public static void AddMapping(Type type, IMapping mapping) | |
| { | |
| _dict[type] = mapping; | |
| } | |
| public static T GetMapping<T>(Type type) where T : IMapping | |
| { | |
| return (T)_dict[type]; | |
| } | |
| } | |
| public class EntityHandler | |
| { | |
| public T Get<T>() | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment