Good day. I have a large project on MVVM Light. The database-first approach on the Entity Framework is used. There is a class DataService. There are 5809 lines in this file! I want to fix this somehow. Will it be good if I divide the DataService into several different services? Maybe it is worth looking in the direction of the Repository or UnitOfWork patterns, but you have heard that they are rather meaningless with EF. Unfortunately, there are no cool uncles of developers in our organization, so there is no one to tell.

DataService - a service to access data from the database. Uses EF. I give for example one relevant method in the DataService.

public firma GetFirmById(int id_firm, params string[] includes) { using (var clientEntities = new ClientEntities(Application.Current.Properties["connectionStringForClient"].ToString(), false)) { DbQuery<firma> query = clientEntities.firms; foreach (var include in includes) query = query.Include(include); return query.First(f => f.id_firm == id_firm); } } 
  • one
    class name DataService does not speak about anything. you can have anything there. specify the question. Show sample code for the method from the DataService. but in general, I would advise you to try to break into classes by root entities, for a start. - PashaPash ♦
  • @PashaPash updated the question. - cvvvlad

2 answers 2

The number of lines does not indicate the number of public methods. If the deadlines allow, then of course you can spend time and spread the methods to more specialized classes. If the class is alien, then it is better not to touch it at all, so that you will not be responsible for other problems (the author of the class will then say that everything worked fine before your refactoring). In this case, the maximum - scatter inside the class by thematic regions.

  • The author of the code is no longer working here. I am more interested in how this is done in other large projects. Is it really everywhere where MVVM is used one class is used for all types of database access? After all, there are projects and our largest. - cvvvlad
  • I can not name my projects especially large. Here was one project with four parallel bases. ~ 40, ~ 30, 5, 5 tables, respectively. The architecture was built as follows: SQL <=> WCF (with EF) = <WPF MVVM> True databases basically already existed, so the approach was Data-First. Using EF, I received four sets of classes for each table and a new method for each stored procedure. Yes - this is a mess! But no one bothers to share all this wealth in the interface, where WCF contracts are described. Then the WCF client will have access to the already structured as I need methods and data. - Anatoly Nikolaev
  • By the way, WCF classes can already be used as models for MVVM without proxy classes. - Anatoly Nikolaev

The “repository” pattern is appropriate here.

The essence of the pattern is that for each type a separate repository class is created with operations related to this type. Most often it is CRUD + specific operations.

For example, FirmaRepository with the methods Create, GetById, Get (if you need several), Update, Delete.

  • Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of inspections - YuriySPb ♦
  • I added the answer, although I sincerely believe that he did not need it. The “repository” pattern is very widespread and this particular link leads only to the top Google result for this query. - Anton Papin