-1

The doubt I am encountering is regarding the usage of static methods during the implementation of DAL and BLL access layer in a winform application.

I know that there are many articles here regarding this topic but I didn't find any article which replies to my specific questions.

A brief introduction: I am developing a windows form application which will be used by many users (each of them with their account on a virtual machine - so basically each instance of the application will be executed by different users account). Since I don't need to keep the state of many objects I decided to use (until now I mean and for some operation) static methods and in particular in DAL and BLL to perform operation like: GetUserProfiles, GetProfileByUserName, AddNewUser, UpdateUserByUserID, etc... or to keep the state of the application with some information (like: the user which is currently running the application, lookup data from DB, etc..) which are expected to remain the same for the entire execution of the application.

My question / doubt: is it correct to implement these parts of access to the database with static method since the are needed just to retrieve information and, in my opinion, is not required a state since each time they will be executed it is like a new execution and a previous state is not necessary?

Is it correct my way of thinking and my approach in your opinion or am I doing something wrong which I don't see at the moment?

Thank you every one will reply and give me a suggestion on what is better to do.

Simone
  • 7
  • 1
  • 4

2 Answers2

1

Although the answer to this question fairly depends on the size of the your application/security/performance/Time ... but for most of the professional developers it's called "The Best Practice" witch matters.

Also some might say just KISS (keep it simple and stupid) but if you are going to consider the future you should keep following the least well know DAL best practices for you design.

Let's assume the next year your boss orders you to use Entity Framework in your code, what would you do then? if your code is just a grown up code it will need heavy workload to change all of the stuff to EF. or what happens if you change your database engine, Oracle/MySql/SqlServer? then you are in a deep trouble if you have lots of code there.

I suggest at least to use an Interface combined with using Factory Design Patterin for your DAL if you are not going to struggle with complex patterns.

Another thing to mention is that using static classes you are making your DAL single thread per user witch is not pretty good for Win Form application.

so If you want Flexibility and multi thread ability for your application it;s not the best practice, but if you just want the application to work there you are.

Community
  • 1
  • 1
yekanchi
  • 813
  • 1
  • 12
  • 30
  • I don't follow you statement on threading. Running a (multi threaded) task requires a delegate and delegates can be made of static or instance methods. And each user will start the winforms application as its own process anyway. However I agree on the other points. – Olivier Jacot-Descombes Jul 07 '18 at 15:17
  • @simone StackOverFlow is not for chatting, if you like to discuss use https://chat.stackoverflow.com – yekanchi Jul 07 '18 at 15:49
  • thank you for your responses. I think that this application, since it is directed to a very small company, will not grow up in the future. Basically it should remain as is. Regarding your sentence about DAL, could you exaplain me the reason since the application will be executed by different users with different account and so in different environments? Now I am accessing a SQL Server DB and using stored procedures. Do you see any security break using this approach with static methods at the lights of these new information or do you see any issues using this application by different users? – Simone Jul 07 '18 at 16:09
  • In other terms: do you suggest strictly to change my code and convert everything in instance method instead of static methods? – Simone Jul 07 '18 at 16:10
  • @Simone I maintain the experience is precious, if it;s a small app you can write it like the way you said. implementing a security assessment actually needs other stuff like authentication and using services so go the way you are into it. just look at their code to see how they code, review their code and see how they asses these issues. – yekanchi Jul 07 '18 at 16:14
  • The company might not grow but as French people say "l'appétit vient en mangeant", the appetite comes with the eating. Applications are almost always growing with time. The repository pattern is neither about security nor about multi-user stuff. This article "The Repository Pattern" (Microsoft) explains the objectives of the pattern. – Olivier Jacot-Descombes Jul 07 '18 at 16:32
1

Static members (methods, properties, etc.) cannot benefit from object-orientation, i.e. polymorphism, inheritance, interface implementation etc.

If you are using a repository pattern, you can generalize things and keep the ability to add specific methods to specific repositories. A repository could be described as:

public interface IRepository<T>
{
    T GetById(int id);
    List<T> GetAll();
    void Create(T entity);
    void Delete(T entity);
    void Update(T entity);
}

This is just an example you can tailor to your needs.

As an example for flexibility, imagine a CSV exporter accepting a repository having a method

public void Export<T>(string file, IReporistory<T> repository)
{
    ...
    List<T> result = repository.GetAll();
    ...
}

In a UserProfileRepository implementing this interface you can still add a method GetProfileByUserName not defined in the interface.

If you want it more static, you can implement the interfaces as singletons.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • very firm answer, i'm interested in the Repository design pattern the way you describe it very simply, can you provide tutorial link here in comments if you know any good one witch will use the pattern as you described here. – yekanchi Jul 07 '18 at 15:46
  • If you google for "repository pattern" you will find zillions of answers from very basic implementations to extremely sophisticated ones. Some are hard-coded other very [generic](https://garywoodfine.com/generic-repository-pattern-net-core/). They also differ in the technology used (like ADO.NET, EF, etc.). So it is difficult recommend one rather than the other. – Olivier Jacot-Descombes Jul 07 '18 at 16:01
  • Generic repository is considered an anti-pattern: https://stackoverflow.com/a/51781877/5779732 – Amit Joshi Sep 24 '20 at 14:14