Pergunta

At the moment my website has a repository pattern with the specification pattern in it. I can get data from within my .aspx page with just a few lines of code, example:

private IRepository repository;

    protected void Page_Load(object sender, EventArgs e)
    {
        repository = new GenericRepository();

        Specification<Book> specification = new Specification<Book>(b => b.Year == 1988);
        lvBooks.DataSource = repository.GetAll<Book>(specification);
        lvBooks.DataBind();
    }

Now my question is, do I need a business layer in my website and if your answer is yes, why? At the moment it seems, because of the specification pattern, that I have no need for a business layer who's between the page and the repository.

Thanks for your opinion.

Foi útil?

Solução

The answer is dependant on how big this application is, how big it's going to get, and how much it's likely to change.

The only real point of any layers is to isolate functionality. In a small application you can happily have calls to the repository sprinkled throughout the UI code.

But what if you then change something in the way the repository is structured? You'll need to find and change all those references.

However if you wrote all your repository access code in a business layer which exposes higher level methods to the UI then you've got much less work to do at this point.

There could be specific security considerations. For example if your UI doesn't have access to the repository then you can focus all your security checking on the public API of the business layer. If you have a 200 page web app where the repository can be accessed from anywhere - of course it could be secure - but how certain can you be?

Then there's unit testing...... basically there is no right way - but if your app is small you're fine... if your app is large you're probably going to regret this design at some point.

Outras dicas

From your code it seems that you dont need business layer. As it seems its all about data fetching using simple specification or maybe data insertion. BL would be required when you have some business rules around these objects, ex: Deleting an object should check some specific condition before deleting the object etc

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top