How To Invoke A Method When It’s Class is Accessed (Any Method) – “OnClassEnter”

I am working on a C# net micro framework project, in particular an I2C bus management/class.

I have several different I2C devices each defined in separate classes which contain all of the devices’ unique methods. In my case, I can only have ONE I2CDevice object “active” to communicate with and to switch to another I2C device to communicate with it requires a call like I2CBus.Config = DeviceConfig1.

For example (pseudo-code):

Class Device1 
-DeviceConfig1
-Method 1
-Method 2
-Method 3

Class Device2
-DeviceConfig2
-Method 1
-Method 2
-Method 3


Main Program
{
I2cBus = new I2CDevice(configuration)
Device1.Method1
Device2.Method3
}

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property. I call it an “OnClassEnter event” that would check that the device settings are correct before it proceeds. I’m looking for an elegant way such that whenever I call a Method for a Device, that the I2CBus.Config is automatically checked and changed. Is there a way that I don’t need to re-type this config code in each method? Have a separate internal method that’s called each time I access any of those methods? Maybe something inherited from a base class or Interface, but where I don’t have to remember call it from each method in the Device class.

1

Instead of attempting to capture when control passes into your class and update the config then, explicitly update the configuration just before the I2CDevice is used.

Write a wrapper for I2CDevice which takes your DeviceConfig and I2CDevice as parameters to its constructor.

The wrapper can provide its own versions of the I2CDevice methods (looks like there are only 3), it would then ensure I2C configuration before executing the corresponding I2CDevice method.

You would then use this wrapper inside your Device1, Device2 classes in lieu of using I2CDevice directly.

public class I2CDeviceWrapper 
{
   private readonly I2CDevice _i2cdevice;
   private readonly DeviceConfig _config;

   public I2CDeviceWrapper(DeviceConfig config, I2CDevice device)
   {
       _config = config;
       _i2cdevice= device;
   }
   private void EnsureConfiguration()
   {
       //verify or set configuration so this wrapper's device is active.
   }
   public int Execute(I2CTransaction[] xActions, int timeout)
   {
       EnsureConfiguration();
       return _i2cdevice.Execute(xActions, timeout);
   }
   public I2CReadTransaction CreateReadTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateReadTransaction(buffer);
   }
   public I2CWriteTransaction CreateWriteTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateWriteTransaction(buffer);
   }

}

EDIT: if the I2CDevice is passed into the wrapper, the wrapper should not attempt to manage its lifecycle.
I2CDevice provides a Dispose() method, in my experience (which does not include the micro framework) this indicates you risk a resource leak if it is not called. If this holds for the micro framework, you would want to provide one for the wrapper as well that calls the I2CDevice one.

Wrapper as Abstract Base:

If you use the wrapper as a base for each of your Device1, Device2 classes, the visibility of the Execute, I2CGetReadTransaction, I2CGetWriteTransaction methods and perhaps the _config field should be made protected.

public class Device1: I2CDeviceWrapper
{
    public Device1(DeviceConfig config, I2CDevice device):base(config, device)
    {}

    public something Method1()
    {
        ... 
        Execute(...);
        ...
    }        
}

Wrapper as member field:
If the wrapper is a member of your Device1, Device2 classes, you would still pass your i2cBus as a parameter to those constructors, create A per Device wrapper using that devices’ configuration and i2cBus and then use it going forward as you previously would have used i2cBuss.

public class Device1
{
    private readonly I2CDeviceWrapper _i2cBus;

    public Device1(I2CDevice i2cBus)
    {
         var config = //set up configuration
         _i2cBus = new I2CDeviceWrapper(config, i2cBus);
    }
    public void Method1()
    {
        ...
        _i2cBus.Execute(...);
        ...
    }
}

4

Disclaimer: My C# experience is probably less than 8 hours total so the syntax that follows may not be accurate.

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property.

You could arrange it so the only way to get a device is through a method like

public void With(DeviceConfig config, Action<Device> function) {
    // configure the hardware
    Device device = /* get the device */
    function(device);
}

So that you could do this:

With(device1Config, (device1) => {
    device1.Method1();
    device1.Method2();
    device1.Method3();
});
With(device2Config, (device2) => {
    device2.Method1();
    device3.Method2();
});

That would ensure you can’t get a hold of a device without first configuring it, but would enable you to make multiple calls without reconfiguration.

11

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How To Invoke A Method When It’s Class is Accessed (Any Method) – “OnClassEnter”

I am working on a C# net micro framework project, in particular an I2C bus management/class.

I have several different I2C devices each defined in separate classes which contain all of the devices’ unique methods. In my case, I can only have ONE I2CDevice object “active” to communicate with and to switch to another I2C device to communicate with it requires a call like I2CBus.Config = DeviceConfig1.

For example (pseudo-code):

Class Device1 
-DeviceConfig1
-Method 1
-Method 2
-Method 3

Class Device2
-DeviceConfig2
-Method 1
-Method 2
-Method 3


Main Program
{
I2cBus = new I2CDevice(configuration)
Device1.Method1
Device2.Method3
}

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property. I call it an “OnClassEnter event” that would check that the device settings are correct before it proceeds. I’m looking for an elegant way such that whenever I call a Method for a Device, that the I2CBus.Config is automatically checked and changed. Is there a way that I don’t need to re-type this config code in each method? Have a separate internal method that’s called each time I access any of those methods? Maybe something inherited from a base class or Interface, but where I don’t have to remember call it from each method in the Device class.

1

Instead of attempting to capture when control passes into your class and update the config then, explicitly update the configuration just before the I2CDevice is used.

Write a wrapper for I2CDevice which takes your DeviceConfig and I2CDevice as parameters to its constructor.

The wrapper can provide its own versions of the I2CDevice methods (looks like there are only 3), it would then ensure I2C configuration before executing the corresponding I2CDevice method.

You would then use this wrapper inside your Device1, Device2 classes in lieu of using I2CDevice directly.

public class I2CDeviceWrapper 
{
   private readonly I2CDevice _i2cdevice;
   private readonly DeviceConfig _config;

   public I2CDeviceWrapper(DeviceConfig config, I2CDevice device)
   {
       _config = config;
       _i2cdevice= device;
   }
   private void EnsureConfiguration()
   {
       //verify or set configuration so this wrapper's device is active.
   }
   public int Execute(I2CTransaction[] xActions, int timeout)
   {
       EnsureConfiguration();
       return _i2cdevice.Execute(xActions, timeout);
   }
   public I2CReadTransaction CreateReadTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateReadTransaction(buffer);
   }
   public I2CWriteTransaction CreateWriteTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateWriteTransaction(buffer);
   }

}

EDIT: if the I2CDevice is passed into the wrapper, the wrapper should not attempt to manage its lifecycle.
I2CDevice provides a Dispose() method, in my experience (which does not include the micro framework) this indicates you risk a resource leak if it is not called. If this holds for the micro framework, you would want to provide one for the wrapper as well that calls the I2CDevice one.

Wrapper as Abstract Base:

If you use the wrapper as a base for each of your Device1, Device2 classes, the visibility of the Execute, I2CGetReadTransaction, I2CGetWriteTransaction methods and perhaps the _config field should be made protected.

public class Device1: I2CDeviceWrapper
{
    public Device1(DeviceConfig config, I2CDevice device):base(config, device)
    {}

    public something Method1()
    {
        ... 
        Execute(...);
        ...
    }        
}

Wrapper as member field:
If the wrapper is a member of your Device1, Device2 classes, you would still pass your i2cBus as a parameter to those constructors, create A per Device wrapper using that devices’ configuration and i2cBus and then use it going forward as you previously would have used i2cBuss.

public class Device1
{
    private readonly I2CDeviceWrapper _i2cBus;

    public Device1(I2CDevice i2cBus)
    {
         var config = //set up configuration
         _i2cBus = new I2CDeviceWrapper(config, i2cBus);
    }
    public void Method1()
    {
        ...
        _i2cBus.Execute(...);
        ...
    }
}

4

Disclaimer: My C# experience is probably less than 8 hours total so the syntax that follows may not be accurate.

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property.

You could arrange it so the only way to get a device is through a method like

public void With(DeviceConfig config, Action<Device> function) {
    // configure the hardware
    Device device = /* get the device */
    function(device);
}

So that you could do this:

With(device1Config, (device1) => {
    device1.Method1();
    device1.Method2();
    device1.Method3();
});
With(device2Config, (device2) => {
    device2.Method1();
    device3.Method2();
});

That would ensure you can’t get a hold of a device without first configuring it, but would enable you to make multiple calls without reconfiguration.

11

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

How To Invoke A Method When It’s Class is Accessed (Any Method) – “OnClassEnter”

I am working on a C# net micro framework project, in particular an I2C bus management/class.

I have several different I2C devices each defined in separate classes which contain all of the devices’ unique methods. In my case, I can only have ONE I2CDevice object “active” to communicate with and to switch to another I2C device to communicate with it requires a call like I2CBus.Config = DeviceConfig1.

For example (pseudo-code):

Class Device1 
-DeviceConfig1
-Method 1
-Method 2
-Method 3

Class Device2
-DeviceConfig2
-Method 1
-Method 2
-Method 3


Main Program
{
I2cBus = new I2CDevice(configuration)
Device1.Method1
Device2.Method3
}

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property. I call it an “OnClassEnter event” that would check that the device settings are correct before it proceeds. I’m looking for an elegant way such that whenever I call a Method for a Device, that the I2CBus.Config is automatically checked and changed. Is there a way that I don’t need to re-type this config code in each method? Have a separate internal method that’s called each time I access any of those methods? Maybe something inherited from a base class or Interface, but where I don’t have to remember call it from each method in the Device class.

1

Instead of attempting to capture when control passes into your class and update the config then, explicitly update the configuration just before the I2CDevice is used.

Write a wrapper for I2CDevice which takes your DeviceConfig and I2CDevice as parameters to its constructor.

The wrapper can provide its own versions of the I2CDevice methods (looks like there are only 3), it would then ensure I2C configuration before executing the corresponding I2CDevice method.

You would then use this wrapper inside your Device1, Device2 classes in lieu of using I2CDevice directly.

public class I2CDeviceWrapper 
{
   private readonly I2CDevice _i2cdevice;
   private readonly DeviceConfig _config;

   public I2CDeviceWrapper(DeviceConfig config, I2CDevice device)
   {
       _config = config;
       _i2cdevice= device;
   }
   private void EnsureConfiguration()
   {
       //verify or set configuration so this wrapper's device is active.
   }
   public int Execute(I2CTransaction[] xActions, int timeout)
   {
       EnsureConfiguration();
       return _i2cdevice.Execute(xActions, timeout);
   }
   public I2CReadTransaction CreateReadTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateReadTransaction(buffer);
   }
   public I2CWriteTransaction CreateWriteTransaction (byte[] buffer)
   {
        EnsureConfiguration();
        return _i2cdevice.CreateWriteTransaction(buffer);
   }

}

EDIT: if the I2CDevice is passed into the wrapper, the wrapper should not attempt to manage its lifecycle.
I2CDevice provides a Dispose() method, in my experience (which does not include the micro framework) this indicates you risk a resource leak if it is not called. If this holds for the micro framework, you would want to provide one for the wrapper as well that calls the I2CDevice one.

Wrapper as Abstract Base:

If you use the wrapper as a base for each of your Device1, Device2 classes, the visibility of the Execute, I2CGetReadTransaction, I2CGetWriteTransaction methods and perhaps the _config field should be made protected.

public class Device1: I2CDeviceWrapper
{
    public Device1(DeviceConfig config, I2CDevice device):base(config, device)
    {}

    public something Method1()
    {
        ... 
        Execute(...);
        ...
    }        
}

Wrapper as member field:
If the wrapper is a member of your Device1, Device2 classes, you would still pass your i2cBus as a parameter to those constructors, create A per Device wrapper using that devices’ configuration and i2cBus and then use it going forward as you previously would have used i2cBuss.

public class Device1
{
    private readonly I2CDeviceWrapper _i2cBus;

    public Device1(I2CDevice i2cBus)
    {
         var config = //set up configuration
         _i2cBus = new I2CDeviceWrapper(config, i2cBus);
    }
    public void Method1()
    {
        ...
        _i2cBus.Execute(...);
        ...
    }
}

4

Disclaimer: My C# experience is probably less than 8 hours total so the syntax that follows may not be accurate.

So, somewhere between Device1.Method1; and Device2.Method3; When switching between devices, I need to change the I2CDevice.Config property.

You could arrange it so the only way to get a device is through a method like

public void With(DeviceConfig config, Action<Device> function) {
    // configure the hardware
    Device device = /* get the device */
    function(device);
}

So that you could do this:

With(device1Config, (device1) => {
    device1.Method1();
    device1.Method2();
    device1.Method3();
});
With(device2Config, (device2) => {
    device2.Method1();
    device3.Method2();
});

That would ensure you can’t get a hold of a device without first configuring it, but would enable you to make multiple calls without reconfiguration.

11

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật