Exemplo n.º 1
0
        public Result <Device> Create(CreateDevice cmd)
        {
            var newDevice = new Device
            {
                BluetoothName = cmd.BluetoothName,
                Generation    = cmd.Generation,
                SerialNumber  = cmd.SerialNumber,
                Id            = Guid.NewGuid().ToString(),
            };

            newDevice.Name = $"{newDevice.BluetoothName}_{newDevice.Generation}";
            return(newDevice);
        }
Exemplo n.º 2
0
        public void IRepository_Mapping_IoC_Repository()
        {
            var svc = new DeviceService().AddDeviceFn(new DeviceRepository());


            var cmd = new CreateDevice
            {
                BluetoothName = "HK_Living",
                Generation    = 1,
                SerialNumber  = "ABC123"
            };

            var result = svc(cmd).Result;

            Assert.True(result.HasValue);
            var emp = result.GetOrElse(new DeviceRegistered());

            Assert.True(emp.Name.Contains(cmd.BluetoothName));
        }
Exemplo n.º 3
0
        public void Simple_Func_Mapping_IoC_Repository()
        {
            Func <Device, Task <Result <Device> > > repo = new DeviceRepository().Save;
            var svc = new DeviceService();

            var cmd = new CreateDevice
            {
                BluetoothName = "HK_Living",
                Generation    = 1,
                SerialNumber  = "ABC123"
            };

            var result = svc.Add(repo, cmd).Result;

            Assert.True(result.HasValue);
            var emp = result.GetOrElse(new DeviceRegistered());

            Assert.True(emp.Name.Contains(cmd.BluetoothName));
        }
Exemplo n.º 4
0
        public async void IRepository_With_Reader_As_IoC_Repository()
        {
            var svc = new DeviceService();


            var cmd = new CreateDevice
            {
                BluetoothName = "HK_Living",
                Generation    = 1,
                SerialNumber  = "ABC123"
            };

            var result = await svc.AddDeviceWithReaderMonad(cmd)
                         .Run(new DeviceRepository());

            Assert.True(result.HasValue);
            var emp = result.GetOrElse(new DeviceRegistered());

            Assert.True(emp.Name.Contains(cmd.BluetoothName));
        }
Exemplo n.º 5
0
 public async Task <Result <DeviceRegistered> > Add(Func <Device, Task <Result <Device> > > save, CreateDevice cmd)
 {
     return(await Result(cmd)
            .Map(Spec <CreateDevice>(c => c.Generation > 0 && c.SerialNumber.Length == 6),
                 () => Error.Of(1001))
            .Map(Create)
            .Map(save)
            .Map(d => new DeviceRegistered
     {
         Id = d.Id,
         Name = d.Name
     }));
 }
Exemplo n.º 6
0
 public Reader <IDeviceRepository, Task <Result <DeviceRegistered> > > AddDeviceWithReaderMonad(CreateDevice cmd)
 {
     return(new Reader <IDeviceRepository, Task <Result <DeviceRegistered> > >(async repo =>
     {
         return await this.AddDevice(repo, cmd);
     }));
 }