'Handle Data that is Unified from Multiple Sources using an Interface

I'm working on a WinForms app to try to allow the user to set up discounts for products based on a wide variety of discount strategies. I want to have a way to display all discount strategies in a single table so that the user can arrange them into priority order. I will use separate controls generated at runtime for giving the user the options for CRUD for each of the individual strategies. I need the table to be able to get updates to the lists of strategies and display them correctly.

As a brief example, a purchase detail line could be discounted because the customer is buying the item by the case. A purchase detail line could be discounted because at least 3 of a vitamin / supplement are being purchased. A purchase detail line could be discounted because the customer is a regular wholesale customer.

I need a view that allows the user to create as many custom rules as they want to for each of these possible methods of giving a customer a discount. Then, I need to display the rules in a single table so the user can adjust the rules into priority order.

I currently plan have a database table for DiscountByMeasurement, one for DiscountByDepartment, and one for DiscountByWholesale. All of these database tables implement the interface IDiscount.

public interface IDiscount
{
    string Name { get; set; }
    string Description { get; set; }
    int Priority { get; set; }
    decimal Discount { get; set; }
}

Each type has a helper class associated with it to get the data out of the database and cast it to IDiscount.

public class DiscountByMeasurementRepository : IDiscountManager
{
    public DiscountByMeasurementRepository(Session session)
    {
        _helper = new MeasurementDiscountHelper(session);
        Discounts = _helper.Discounts.ToList<IDiscount>();
    }
}

And finally, loading the information into the table.

private void LoadDiscounts()
{
    var types = typeof(IDiscountManager).Assembly.GetTypes();

    var persistentTypes = types.Where(x => x.IsClass && !x.IsAbstract &&
        x.GetInterfaces().Contains(typeof(IDiscountManager)));

    foreach (var type in persistentTypes)
    {
        var manager = (IDiscountManager)Activator.CreateInstance(type, unitOfWork);
        _discounts.AddRange(manager.Discounts);
    }
    gridViewDiscounts.Columns.Clear();
    gridcontrolDiscounts.DataSource = _discounts;
}

My desired outcome is that if I use other controls on the form to CRUD the underlying database tables, that the changes would show in the table that contains all the discounts. My additional desired outcome is to be able to directly update the Priority field on the underlying objects, rearranging them in the table.

After searching SO and other sources, I'm not coming across any design patterns for how to handle this situation. If someone who understands what I'm getting at could point me in the right direction, that would be greatly appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source