0

I wonder what are the alternatives and the best practices for instance creaation on a standard 3 layer application.

In UI:

-Should i create one BLL object on form load or create instances every time i need to call BLL methods?

In BLL:

-Should i pass a new DAL object in the BLL constructor or i should create the dal in every method?

DAL:

Currently my DAL is traditionally developed using oledb to connect to AS400 and utilizes ado.net to make the needed operations. Every method opens and closes the connection after the completion of the execute command.

-Is this ok? or i should follow something else?

My requirements include the possibility to be flexible in my UI being able to have almost every possible case for UI implementation (web page ,winforms, e.t.c).

e4rthdog
  • 4,841
  • 4
  • 31
  • 79

2 Answers2

2

Utilize dependency injection if that's an option. This will keep your layers testable and loosely-coupled...good things. If you are using services, then this may be the preferred spot for your business logic.

The BLL can then in instantiate the DLL, or better yet have it injected. I think opening and immediately closing your db connections is good, but try to make sure you're using a domain-account or something similar so you can take advantage of connection pooling.

Since your requirements include flexibility on the UI, you may want to consider the MVP pattern (Model-View-Presenter). This will allow you to use choose WinForms, WebForms, or both as your front-end. I highly recommend this if this is a serious requirement, as it's very flexible in these situations.

This is a high-level answer to to high-level question and there are a lot of options at your disposal. This is just one guy's idea. Let me know if you'd like more detail.

Big Daddy
  • 4,840
  • 3
  • 37
  • 66
  • Would you create objects (bll and dal objects) everytime you need them or not? or it dependson the operation? I am creating my first 3 layered structure right now.After i get the initial code to work i want to refactor it to comply with notions i wan tto get aquainted with... – e4rthdog Dec 14 '12 at 20:31
  • 1
    For example, if you're going the stateful, WinForms route, then I would probably create my BLL once. The BLL, in turn, would create the DAL once. A WebForms route will probably be very different. Since this is your first go at this, keep it simple and as loosely coupled as possible. If you may change from web to win, then I highly recommend an MVP pattern. – Big Daddy Dec 14 '12 at 23:00
1

Create your objects so they could be used stand alone and so they could be mocked. It's always nice to be able to test the UI without the dependency to the "real" DB. Make your BLL to accept an interface of your DAL so you could mockit and test without a real DB, and so on.

You should read about Dependency Injection like Ninject or AutoFac. They will help you with the parameter passing to each of the objects.

It's not good if you endup with a BLL that only works with one implementation of the DAL, it will be really hard to make changes in one layer if it will effect another one.

Some part of your application maybe makes a huge request to the DB and you feeling that you maybe wan't to cache this response for the WebPage but not in the WebForms. If you just have one instance for retrieving the data it will be hard to design some cache function without involving the UI layer. If you create new instance everytime you could for example have one DAL that wrapps the orginal one with just a cache function. When you could easily tell your webapp to use the cache DAL instead.

I would create new object's everytime I use them.

Simon Edström
  • 5,941
  • 6
  • 27
  • 51
  • I am aware of these steps, but i am asking something different and i dont know if it has to do with lazy loading...Do i instantiate for example my BLL object once in my app or i write using statements every time i want to use a BLL method? – e4rthdog Dec 14 '12 at 20:09
  • I updated my answer, but I see that's not good. What you have to ask yourself is how would your application work with just one instance of the BLL. Simply you make a singelton, have a look at this question to see why it could be bad http://stackoverflow.com/questions/1020312/are-singletons-really-that-bad – Simon Edström Dec 14 '12 at 20:19