1

I'm trying to make this Business class to inheritate attributes from a DAL Type (Client).

Client is a Table.

This is the BUS Class, getting attributes from table Client

    public class BusClient : Client
    {
        public void Insert()
        {
            using(MyBIEntities db = new MyBIEntities())
            {
                db.AddToClients(this); //Here's the problem (don't know what to pass)
                db.SaveChanges();
            }
        }

        public bool Validate()
        {
            //Validation Rules goes here.
            return true;
        }
    }

these are the Webform calls:

    try
    {
        BusClient client = new BusClient();

        //Filling properties from Client (table) in the BusClient object all OK.
        client.CityId = int.Parse(ddlCity.SelectedValue());
        client.Name = txtName.Text;
        client.RegisterDate = Convert.ToDateTime("txtDate.Text");

        if (client.Validate())
        {
            client.Insert();
        }
    }
    catch (Exception ex)
    {
        //Catching errors
    }

The problem is obviously that i can't pass a BusClient type to insert.. But I have all those properties already filled, what would be the best way to use them for both Validate() and Insert() methods?

Another thing: What would be the best practice to avoid Type exceptions/errors (Server-side) that occurs on the form BEFORE I try to Validate() ?

4
  • MyBIEntities is probably an IDisposable implementation, so you should be wrapping those statements in a using block in your Insert method. Jun 3, 2011 at 15:02
  • What's the purpose of this BusClient class and why don't you put those two methods Insert and Validate directly into the Client class (which would solve the problem of inserting the correct type)?
    – Slauma
    Jun 3, 2011 at 17:24
  • Client isn't a Class.. it's a Type from a DAL layer
    – Vitor Reis
    Jun 3, 2011 at 20:39
  • But Client is an entity in an Entity Data Model, isn't it? (Because you are using db.AddToClients). What is it if it is not a class? An interface or struct? But this could not be an entity in an EDM, as far as I know. Or do you mean that you cannot modify and add methods to this class?
    – Slauma
    Jun 3, 2011 at 23:45

1 Answer 1

0

As you already have seen you cannot pass in a BusClient into AddToClients. You would have to create a Client from the BusClient in your Insert method by copying the properties which is quite ugly:

public void Insert()
{
    using(MyBIEntities db = new MyBIEntities())
    {
        Client client = new Client();

        client.CityId = this.CityId;
        client.Name = this.Name;
        client.RegisterDate = this.RegisterDate;

        db.AddToClients(client);
        db.SaveChanges();
    }
}

Rethink the design. I cannot see a benefit with deriving a class (BusClient) from an Entity (Client). If you cannot change the Client class you could create either an extension method ...

public static class Extensions
{
    public static bool Validate(this Client client)
    {
        // Run validation rules on client object
    }
}

... or a separate class for your validation:

public class Validator
{
    public bool Validate(Client client)
    {
        // Run validation rules on client object
    }
}

I would also think twice about this separate Insert method. While this may work in your example it can cause trouble (or at least become useless) as soon as you have to manipulate a more complex object graph. In such a case you will need one single ObjectContext for the whole operation. You cannot work then anymore with a separate ObjectContext for each INSERT, UPDATE or DELETE an so on.

So, I'd recommend to remove the DB operations from your special class and put this into the main routine. The whole thing could then look like:

try
{
    using(MyBIEntities db = new MyBIEntities())
    {
        Client client = new Client();

        client.CityId = int.Parse(ddlCity.SelectedValue());
        client.Name = txtName.Text;
        client.RegisterDate = Convert.ToDateTime("txtDate.Text");

        if (client.Validate())
        // works with the extension method, or with the Validator use:
        // Validator validator = new Validator();
        // if (validator.Validate(client))
        {
            db.AddToClients(client);
            db.SaveChanges();
        }
    }
}
catch (Exception ex)
{
    //Catching errors
}

The BusClient class doesn't exist anymore in this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.