简体   繁体   中英

WPF MVVM and Data Acces Layer Organisation

I have developed my first WPF applicationa (tryingt) to use MVVM. I'm still learning and would appreciate the following questions answered:

  1. Should I keep TestReportItem class in Repository class library or move it to it's own class library?

  2. My ViewModel does not reference a Model. It refererences the class TestReportItem. I display the TestReportItem using XAML and a datatemplate to access a string field "Title". Is this acceptable/best practice?

在此处输入图像描述

TestReportItem

public class TestReportItem
{
    public string Title { get; set; } = string.Empty;
    public string SubTitle { get; set; } = string.Empty;
    public bool HasTable { get; set; }
    public string Reference { get; set; } = string.Empty;
    public bool HasAdditionalInformation { get; set; }
}

TestReportItemRepository

public interface ITestReportItemRepository
{
    List<TestReportItem> GetAllTestReportItems();
    TestReportItem GetByName(string testName);
}

XMLTestReportItemRepository

public class XMLTestReportTestStandardRepository : ITestReportItemRepository
    {
        private string _filePath;

        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        public XMLTestReportTestStandardRepository(string sourceFilePath)
        {
            FilePath = sourceFilePath;
        }
        public TestReportItem GetByName(string testName)
        { ... }

        public List<TestReportItem> GetAllTestReportItems()
        { ... }

MVVM is a rule of thumb and not a dogma; meaning its really flexible. Originally MVVM was based off of the three tiered data organization system. View/Business layer/DB layer. And in a sense, it is just that.

Should I keep TestReportItem class in Repository class library or move it to it's own class library?

Whether your classes reside with the main project or in an external class library is up to the design. If the design calls for reuse between different projects, then yes extract it. Otherwise being external does not add any value except in separation of work.

Remember that an external library is in a sense a different namespace to structure your code.

My ViewModel does not reference a Model. It refererences the class TestReportItem.

As to TestReportItem it is a Model. Just because it has/may have methods and operations is moot. If one needs create partial class files where the model esque properties are contained in one partial and the operations et all are in another partial are fine and one achieves separation . But that is optional

datatemplate to access a string field "Title". Is this acceptable/best practice?

Does Title get derived or generated by its being in the class. If it does, then yes, if not, place Title on the main VM and extract/build it in the getter of Title .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM