question

krishna-5407 avatar image
0 Votes"
krishna-5407 asked DuaneArnold-0443 answered

In WPF MVVM ,where should we write following code a)fetching of data (ADO.NET) from database b) buisness logic

We have been using WPF application, however we were using it similar to web forms project. With most of the code in the code behind. Its like a 3 tier architecture with Views, Controller and Entity (ADO.NET CRUD operations) for Database. I am trying to convert my application from this type of coding to MVVM. I have been following mutilple tutorials on the internet and its very confusing to figure out as to where things go especially model and view model. Some of the tutorials use entity framework and we cant use that as our DB is AS400 and the license for entity framework to be used with AS400 is very high. So we have to use ADO.NET.

I wanted to know mostly

Where do we write the ADO.NET code for CRUD operations? Is it Models or View Models or a seperate Class and just call that class. If it is a seperate class, do we call it from Models or ViewModels?

If I have a function like calulate tax or some buisness logic, should I write it in the setter of models? Where does buisness logic go to?

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DuaneArnold-0443 avatar image
1 Vote"
DuaneArnold-0443 answered

@krishna-5407

Business logic goes in the Model folder the domain objects/classes. The domain objects/classes do CRUD operations with the DAL (data access layer), a classlib project, the WPF project has reference to.

https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP

https://www.c-sharpcorner.com/article/mvvm-architecture/

The WPF project along with the DAL should be using the DTO pattern with the DTO(s) in a classlib project name it Entities.

The WPF and DAL projects have reference to the Entities project, and they know what the DTO(s) are about. The DTO(s) travel between layers, tiers, and client/service or wherever they need to go.

https://en.wikipedia.org/wiki/Data_transfer_object#:~:text=In%20the%20field%20of%20programming,that%20carries%20data%20between%20processes.&text=In%20other%20words%2C%20DTOs%20are,transferring%20data%20over%20the%20wire.

https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

The DTO(s) are an abstraction away from the underlying database technology. So the WPF project using MVVM doesn't care what the database technology is being used. It could be EF using MS SQL server, it could be ADO.NET using AS400, etc. and etc., because all the WPF project, the presentation layer, sees are the DTO(s).

You should look into using the DAO pattern in the DAL.

https://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

You should understand the new is glue principle, using an IoC and dependency injection using Unity or one of the other IoC(s).

https://ardalis.com/new-is-glue/

The DAO should be DI into a domain object.

You talk MVC lingo a little, and there is not too much difference in model usage between, MVVM, MVC and MVP.

https://deviq.com/terms/kinds-of-models

Here are two solutions out on Github you can examine that are .NET Core solutions. One is a Windows form solution using MVP and layered style not ntier there are differences, and the other one using ASP.NET MVC doing CRUD with WebAPI with DAL used by the WebAPI.

https://github.com/darnold924/PubComanyWinCore
https://github.com/darnold924/PublishingCompany

https://codeburst.io/interfaces-in-c-write-reusable-and-maintainable-code-that-scales-a144ca06b1f7#:~:text=When%20classes%20are%20not%20dependent,known%20as%20loose%2Dcoupling%20architecture.&text=It%20means%20we%20can%20have,class%20that%20implements%20multiple%20interfaces.

Services in Core is Core's IoC.

https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

The above link may not work from the Q&A forum, and you will have to go to the browsers home page and paste the link in order to see the content.

HTH







5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.