views:

11

answers:

0

I'm working on heavily dynamic and configurable CMS system. Therefore, many pages are composed of a dynamically loaded set of user controls. To enable loose coupling between containers (pages) and children (user controls), all user controls are responsible for their own persistence. Each User Control is wired up to its data/service layer dependencies via IoC. They also implement an IPersistable interface, which allows the container .aspx page to issue a Save command to its children without knowledge of the number or exact nature of these user controls.

Note: what follows is only pseudo-code:

 public class MyUserControl : IPersistable, IValidatable
    {

        public void Save()
        {
            throw new NotImplementedException();
        }


        public bool IsValid()
        {
            throw new NotImplementedException();
        }
   }

    public partial class MyPage
    {
        public void btnSave_Click(object sender, EventArgs e)
        {
            foreach (IValidatable control in Controls)
            {
                if (!control.IsValid)
                {
                    throw new Exception("error");
                }
            }
            foreach (IPersistable control in Controls)
            {
                if (!control.Save)
                {
                    throw new Exception("error");
                }
            }

    }
}

I'm thinking of using declarative transactions from the System.EnterpriseService namespace to wrap the btnSave_Click in a transaction in case of an exception, but I'm not sure how this might be achieved or any pitfalls to such an approach.