views:

196

answers:

2

Hello there,

I am thinking to create a Custom Control button that would do a CRUD tasks for me. Let me elaborate:

I wanted something that save time to writeup code on every UI for CRUD tasks. I am here becuause I want to make sure the approach I am taking should be verified before I am putting hours and taking strain.

A Custom (could be a User Control) Control button which will take a Delegate. This delegate would have a Custom EventArg takes the bussiness class reference, and a CRUD enum (Create, Read, Update, Delete). Every business class will implement an interface that will enforce to define 4 functions (CRUD functions). Now, Once you dragged this control on a form what would you have to do is create a delegate which will need the business class reference and the CRUD enum to perform one of CRUD function.

I am not sure the approach I took is correct. But it looks that it will make my life much easier and the code managable. Please, help me what you think will this cure me or curse me :)

P.S: I'll appreciate if explanation comes with a sample example. Thanks.

+1  A: 

I would seperate the CRUD in 4 seperate command. So one for Create, one for Read... and then I would implement the button.Click event on the form the button is used. In the event i would execute the right CRUD-command.

Example:

public interface ICommand
  {
    void Execute ();
  }

  public class UpdateCommand : ICommand
  {
    public UpdateCommand ()
    {
      // Maybe some business logic has to be passed in the ctor.
    }

    public void Execute ()
    {
      // Do the update stuff here
    }
  }

  // somewhere in the Form:

  public void Button_Update_Click(object sender, EventArgs e)
  {
    UpdateCommand command = new UpdateCommand();
    command.Execute()
  }

Maybe this link will help you understand the command pattern.

It helps you seperating the functionality and testing it quiet easily using TDD.

crauscher
Thanks crauscher.Could you please elaborate with an example. I will appreciate that.
Thanks a bunch :)I am also writing up code for my aformentioned query. Surely share this to you.Actually, am thinking on something based on delegation. A single delegate that takes business class and a enum type that defines the operation (command) and do the job.
do not distinguish the operations based on an enum. See http://www.objectmentor.com/resources/articles/ocp.pdf and http://www.objectmentor.com/resources/articles/lsp.pdf. Actually I don't know how to solve your concrete problem, but I would prefer it my way, not by an enum and a delegate.
crauscher
A: 

You can do something very similar with the built-in ObjectDataSource and DetailsView or FormView controls. I would stick with those rather than writing your own. If you really want to save some time, look into the ASP.NET Dynamic Data sites.

Jamie Ide
But that would mean a one tier application. Is that really usefull or is the tradeoff too big?
crauscher
Database + business objects + UI = 3 layers. I don't use the ODS myself but I think it is designed to work best with active record business objects, which is what you're proposing. Without knowing your business and this application, I can't comment on the trade-offs.Dynamic Data is a different beast and one that's new to me. We may use it for an admin site for editing lookup tables.
Jamie Ide