[桌面端应用开发] 从零搭建基于Caliburn的图书馆管理系统(C#合集)

图书馆系统要求:

你是一家新市图书馆的经理。 图书馆拥有大量藏书和不断增长的会员。 为了使图书馆管理更加容易,现在创建一个图书馆管理系统。 图书馆管理系统应具备以下功能:

1.图书管理:系统应该能够向图书馆添加新图书。 每本书都有唯一的书名、作者和 ISBN 号。 系统还应该能够显示和更新有关特定书籍的信息。

2.会员管理:系统应该能够向图书馆添加新的会员。 每个会员都有唯一的姓名和会员号。 系统还应该能够显示和更新有关特定成员的信息。

3、图书借还:系统应该能够向会员借书。 图书借阅时,应标记为"已借",并在借阅图书列表中列出借阅该图书的会员。 该系统还应该能够管理图书的归还。

4.(可选)报告:系统应该能够生成报告,例如借阅的书籍数量、最受欢迎的书籍、活跃会员数量等。

图书管理系统设计:

Book和User分别拥有了不同的属性,而Library实现了User和Book类之间的数据共享,比如用户能从Book类中获取图书信息

1.在Bootstrapper.cs中写入需要引用或者共享的class或library:

注意:整个Bootstrapper.cs文件,是负责应用程序初始化(用Application Library构建)

其中Bootstrapper.cs的configure函数非常非常重要,可以用来:

  • 注册视图模型 通过调用Container.RegisterTypeForNavigation方法,将视图模型与视图进行关联。这样在导航到特定视图时,相应的视图模型也会被自动创建。
  • 注册服务 使用Unity容器(或其他依赖注入容器)注册应用程序所需的各种服务,如数据访问服务、日志服务、配置服务等。这些服务可以在整个应用程序中共享和重用。
  • 配置区域(Regions) 为应用程序中的区域(如菜单、工具栏、主内容区等)注册相应的行为,例如注册导航服务、事件聚合等。
  • 配置模块目录 设置模块目录的位置,用于查找和加载应用程序的模块。
  • 配置设置 配置全局设置,如设置Shell窗口的启动位置、大小等。
  • 注册事件聚合器 注册一个事件聚合器,用于发布和订阅跨模块的事件。
  • 初始化Shell 配置应用程序的Shell,作为主窗口显示。
那要共享的class或library是什么呢?
  • 需要共享的所有数据逻辑(包含各个业务模块):

  • 比如当我们建立一个图书馆系统,我们希望用户在自己用户界面能看到自己借书情况,在书籍页面可以看到书籍的在借状态,也就是说书籍信息和用户信息要实现共享,那如何实现呢?

  • Bootstrapper.cs中的configure函数中引入,以保证实现数据信息的共享:

    <BookViewModel>();
    <UserViewModel>();
    <library>();

  • 还包括其他页面的信息共享,主视图(ShellViewModel),header视图(HeaderViewModel)

    复制代码
          protected override void Configure()
          {
              base.Configure();
              this.container.Singleton<Library>();
              this.container.Singleton<BookViewModel>();
              this.container.Singleton<UserViewModel>();
              this.container.Singleton<IStatusBar, StatusBarViewModel>();
              this.container.Singleton<IHeader, HeaderViewModel>();
              this.container.Singleton<IShell, ShellViewModel>();
              this.container.PerRequest<OptionsDialogViewModel>();
             
          }
2. 分别创建Book和User的类(class)

Book类: 每本书都有唯一的书名、作者和 ISBN 号。 系统还应该能够显示和更新有关特定书籍的信息。注意,因为书名是唯一的,所以会定义在constructor里。

复制代码
namespace LibraryManagementSystem.Basics
{
    /// <summary>
    /// Implements a class to 
    /// </summary>
    public class Book
    {

        #region Fields

        #endregion

        #region Constructor
        /// <summary>
        ///  Initiates a class of the type <see cref="Book"/> 
        /// </summary>
        public Book(string title)
        {
            this.Title = title;
        }
        #endregion

        #region Properties
        public string Title { get; set; }
        public string Author { get; set; }
        public string ISBN { get; set; }
        public Status BookStatus { get; set; }


        #endregion
        /// <summary>
        /// 
        /// </summary>
      
        #region  Methods

        #endregion
        public bool SetBookStatus(Status status)
        {
            this.BookStatus = status;
            return true;
        }
    }

User类,每个会员都有唯一的姓名和会员号。

复制代码
namespace LibraryManagementSystem.Basics
{
    /// <summary>
    /// Implements a class to 
    /// </summary>
    public class User
    {

        #region Fields

        #endregion

        #region Constructor
        /// <summary>
        ///  Initiates a class of the type <see cref="User"/> 
        /// </summary>
        public User(string name)
        {
            this.Name = name;
        }
        #endregion

        #region Properties

        public string Name { get; set; }
        public Book BorrowedBook { get; set; }
        public int UserID { get; set; }
}

定义library,让Book和User之间的数据实现共享,以及Book和User之间的编辑和删改操作都放在这里

复制代码
namespace LibraryManagementSystem.Basics
{
    /// <summary>
    /// Implements a class to 
    /// </summary>
    public class Library
    {



        #region Constructor

        public Library()
        {
            this.Books = new ObservableCollection<Book>();
            this.Users = new ObservableCollection<User>();
        }


        #region Properties

        public ObservableCollection<User> Users { get; set; }


        public ObservableCollection<Book> Books { get; set; }





        #region  Methods
        public bool AddOneBook(string title, string author, string isbn, Status status)
        {
            if (Books == null)
            {
                Books = new ObservableCollection<Book>();
            }

            var newBook = new Book(title)
            {
                Author = author,
                ISBN = isbn,
                BookStatus = status
            };

            Books.Add(newBook);
            return true;
        }




        public bool SetOneBook(Book book, string newTitle, string newISBN, string newAuthor, Status newStatus)
        {
            if (Books == null || Books.Count == 0)
            {
                return false;
            }
            book.ISBN = newISBN;
            book.Author = newAuthor;
            book.BookStatus = newStatus;
            return true;
        }

        public bool DeleteSelectedBook(Book book)
        {
            if (book == null)
            {
                return false;
            }
            if(!Books.Contains(book)||Books == null)
            {
                return false;
            }
            Books.Remove(book);
            book = null;
            return true;
        }



        ///User
        ///
        public bool AddOneUser(string name, int userID, Book borrowedBook)
        {
            if (Users == null)
            {
                Users = new ObservableCollection<User>();
            }

            var newUser = new User(name)
            {
                UserID = userID,
                BorrowedBook = borrowedBook,
            };

            Users.Add(newUser);
            return true;
        }

        public bool DeleteSelectedUser(User user)
        {
            if (user == null)
            {
                return false;
            }
            if (!Users.Contains(user) || Users == null)
            {
                return false;
            }
            Users.Remove(user);
            user = null;
            return true;
        }

        public ObservableCollection<Book> GetAllBooks()
        {
            return Books;
        }

    }
  1. 结合BookView和UserView的视图

在BookViewModel中回溯该函数:

复制代码
    public class BookViewModel: ViewAware
    {

        #region Fields
    
        #endregion

        #region Constructor

        public BookViewModel(Library library)
        {
            this.Library = library;
            this.NewBook = new Book("");
            StatusList = new ObservableCollection<Status>(Enum.GetValues(typeof(Status)).Cast<Status>());
        }
        #endregion

        #region Properties

        public Book NewBook { get; set; }
      
        public ObservableCollection<Status> StatusList { get; set; }
        public Library Library { get; set; }
        public Book SelectedBook { get; set; }
   


        #endregion

        #region  Methods
        public bool AddOneBook()
        {
            return Library.AddOneBook(this.NewBook.Title,this.NewBook.Author,this.NewBook.ISBN,this.NewBook.BookStatus);
        }

        public string GetOneBook(string title)
        {
            return Library.GetOneBook(title);
        }

        //public bool SetOneBook(Book book, string newTitle, string newAuthor, string newISBN, Status newStatus)
        //{
        //    return Library.SetOneBook(book, newTitle, newAuthor, newISBN, newStatus);
        //}

        public bool SetOneBook(Book book, string newTitle, string newAuthor, string newISBN, Status newStatus)
        {
            return Library.SetOneBook(book, newTitle, newAuthor, newISBN, newStatus);
        }

        public bool DeleteSelectedBook()
        {
            return Library.DeleteSelectedBook(SelectedBook);
        }


        #endregion
    }

在UserViewModel中回溯增删的函数:

复制代码
    public class UserViewModel : ViewAware
    {

        #region Fields
        private Library library;
        #endregion

        #region Constructor
        /// <summary>
        ///  Initiates a class of the type <see cref="UserViewModel"/> 
        /// </summary>
        public UserViewModel(Library library)
        {
            this.Library = library;
            this.NewUser = new User("");
            this.SelectedUser = this.NewUser;
            this.AllBooks = new ObservableCollection<Book>(); // Initialize AllBooks
        }
        #endregion

        #region Properties

        public User NewUser { get; set; }
        public User SelectedUser { get; set; }
        public Library Library { get { return library; } set { library = value; } }
        public ObservableCollection<Book> AllBooks { get; set; }
        public Book SelectedBook { get; set; }

        #endregion

        #region  Methods
        public bool AddOneUser()
        {
            return Library.AddOneUser(this.NewUser.Name, this.NewUser.UserID, SelectedBook);
        }

        public bool DeleteSelectedUser()
        {
            return Library.DeleteSelectedUser(SelectedUser);
        }

将两个视图显示到一个页面

复制代码
            <ux:InplaceTabBar  Grid.Row="1" >

                <TabItem Header="{x:Static res:Resources.ViewBooks}" >
                    <Book:BookView  x:Name="BookVM"/>
                </TabItem>

                <TabItem Header="{x:Static res:Resources.ViewUsers}" >
                    <User:UserView  x:Name="UserVM"/>
                </TabItem>


            </ux:InplaceTabBar>

注意:

定义

private Library library;

public Library Library { get { return library; } set { library = value; } }

和 public Library SelectedLibrary { get; set; } 是一致的

相关推荐
q***465210 小时前
Spring Boot 实战:轻松实现文件上传与下载功能
java·数据库·spring boot
Li_76953210 小时前
10分钟快速入手Spring Cloud Config
java·spring·spring cloud
源码技术栈11 小时前
Java基于云计算的社区门诊系统源码 医院门诊系统源码 已实现医保结算 SaaS模式
java·云计算·源码·诊所·门诊·预约挂号·云门诊
孟陬11 小时前
AI 每日心得——AI 是效率杠杆,而非培养对象
前端
程序员西西11 小时前
SpringBoot整合JWT实现安全认证
java·计算机·程序员·编程
漆黑骑士11 小时前
Web Component
前端
San3011 小时前
深入理解 JavaScript 事件机制:从事件流到事件委托
前端·javascript·ecmascript 6
行走在顶尖11 小时前
基础随记
前端
Sakura_洁11 小时前
解决 el-table 在 fixed 状态下获取 dom 不准确的问题
前端
best66611 小时前
Vue3什么时候不会触发onMounted生命周期钩子?
前端·vue.js