wrapping user controls in a transaction

Posted by Hans Gruber on Stack Overflow See other posts from Stack Overflow or by Hans Gruber
Published on 2010-05-19T16:48:52Z Indexed on 2010/05/27 3:21 UTC
Read the original article Hit count: 272

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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about object-oriented-design