Question

I need some opinions on choosing which signatures for my web based business layer function:

function void CreateUser(Company UserCompany, string name...)

or

function void CreateUser(int CompanyID, string name...)

If this is a window based I would choose function to take object instead of int because since the company already loaded to the window form, why not just utilize it. Also, I think business layer should take business objects and it's type safe(preventing accidently pass in 0 or -1).

But on the other hand, if this is a web based application, only id will be loaded to client sites. So it's kind of redundant to load company object because eventually only companyID will be saved in the dabasebase anyway.

Hopefully my description is not too confusing =P Thank you for reading.

Was it helpful?

Solution

I think you answered your own question, it depends on the presentation layer you're using. But when you want to keep this business layer generic, you'll have to go with the ID.

Here's another idea. I personally like to wrap all mutations into classes, that I call commands. You could for instance have an CreateUserCommand that inherits from a base command. This would allow you to use it like this (I'm assuming C# here):

var command = new CreateUserCommand();

command.UserCompanyId = companyId;
command.UserName = name;

command.Execute();

When wrapping this logic in a command is very nice pattern. It allows you to put a single use case in a single command. Especially when the amount of logic in a command grows you will appreciate this pattern, but I found it to be very effective for CRUD operations as well.

This pattern also allows you to abstract the transactional model away in the base class. The base class can wrap a call to execute in a database transaction. Here is an simple implementation:

public abstract class CommandBase
{
    public void Execute()
    {
        this.Validate();

        using (var conn = ContextFactory.CreateConnection())
        {
            conn.Open();
            using (var transaction = conn.BeginTransaction())
            {
                using (var db = ContextFactory.CreateContext(conn))
                {
                    this.ExecuteInternal(db);

                    db.SubmitChanges();
                }

                transaction.Commit();
            }
        }
    }

    protected virtual void Validate() { }

    protected abstract void ExecuteInternal(YourDataContext context);
}

And this is what the CreateUserCommand would look like:

public class CreateUserCommand : CommandBase
{
    public int UserCompanyId { get; set; }
    public string UserName { get; set; }

    protected override void ExecuteInternal(YourDataContext context)
    {
       this.InsertNewUserInDatabase(context);
       this.ReportCreationOfNewUser();
    }

    protected override void Validate()
    {
        if (this.UserCompanyId <= 0) 
            throw new InvalidOperationException("Invalid CompanyId");
        if (string.IsNullOrEmpty(this.UserName))
            throw new InvalidOperationException("Invalid UserName");
    }

    private void InsertNewUserInDatabase(YourDataContext context)
    {
       db.Users.InsertOnSubmit(new User()
       {
          Name = this.UserName,
          CompanyId = this.CompanyId
       });
    }

    private void ReportCreationOfNewUser()
    {
        var message = new MailMessage();
        message.To = Configuration.AdministratorMailAddress;
        message.Body = "User " + this.Name + " was created.";

        new SmtpClient().Send(message);
    }
}

I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top