-1

I'm starting C# and would like to know if there is any problem in approach below:

enter image description here

Briefly, in the layers there would be instances similar to the following:

UI

var userBll = _userBll.SaveUser(dtoUser); 

BLL

// Validations and other business rules
return _userDal.SaveUser (dtoUser); // Request DAL

DAL

// Return the saved user to the database
return userDb;

Is there a problem with this kind of approach, in which the UserBll class would contain the "SaveUser" method? Would not it be correct to have a User class responsible for calling the Save method? Something like:

var user = new User();
user.SaveUser()

In this case would there be a redundancy between the User object class and the UserDTO data transport class?

Sorry if this may sound like a silly question, but it really confuses me.

Marcoscdoni
  • 955
  • 2
  • 11
  • 31
  • Did you do some reading up? Because as it is that isa BROAD topic - but it is also one that has a ton of books written about it and public posts arguing any approach. As per site rules, specific questions would be welcome here - but as it is, I am not going to write a multi page answer. – TomTom Aug 12 '18 at 18:26
  • It seems like you are conflating “tiers” with “layers”. – RBarryYoung Aug 12 '18 at 18:27
  • This approach makes sense **IFF** you use several **UI** to access the same BLL and several business logic which use the same DAL. – Alex Kudryashev Aug 12 '18 at 18:31
  • If you'd have `user.SaveUser()` which would probably call database then imagine saving 1000 users at once. It would take a lot of time with 1000 queries. – FCin Aug 12 '18 at 18:39
  • @Alex Kudryashev Would not it make sense for ease of maintenance? What about providing a clear division of responsibilities? But my main question would be whether using a method (behavior) in a business class would be a break in concept in OOP. The principle of doubt is: the "Save" behavior should not be in a User class instead of directly in the UserBll class – Marcoscdoni Aug 12 '18 at 21:41
  • @Marcoscdoni Theoretically, you can have `User.Save()` method in BLL which calls some method in DAL (and be ignorant/agnostic what DAL does). Even more, DAL method can be polymorphic and depend on underlying data source (i.e. sql server, mySQL, or whatever). In this case it **does** make sense. Else the multilayer approach just creates a chain of transformations and potentially may damage performance. Again, It Depends. – Alex Kudryashev Aug 12 '18 at 22:02

1 Answers1

0

The DAL is merely an abstraction over the underlying data provider. The BLL doesn't know of what's going on, just knows it wants to save a user, given the validation is successful.

I therefore believe the approach containing _userDal.SaveUser (dtoUser) is more appropriate for such a thing. But in my own apps, I usually have another assembly for common/shared items. Something like your dtoUser. I don't like direct dependencies between UI and DAL in 3-layered apps.

staa99
  • 191
  • 1
  • 2
  • 13