• WPF MVVM架构 EF、WCF、IOC 设计示例经典


    概要

    该演示项目利用WPF应用程序构建的MVVM架构示例, 运用了Unity容器接口注入, MVVM的经典设计, 后台利用的EF+WCF。

    后台实现:

    从数据库生成的emdx 结合上下文进行数据交互, 服务以WCF发布:

    public class FactoryManager
        {
            private IDataManager dataManager;
    
            public FactoryManager()
            { 
                //can easily switch to other providers in the future without changing client code
                dataManager = new EntityFrameworkManager();
            }
    
            public ICustomerManager GetCustomerManager()
            {
                return dataManager.GetCustomerManager();
            }
    
            public IOrderManager GetOrderManager()
            {
                return dataManager.GetOrderManager();
            }
        }
     class EntityFrameworkManager : IDataManager
        {
            ICustomerManager IDataManager.GetCustomerManager()
            {
                return new CustomerManager();
            }
    
            IOrderManager IDataManager.GetOrderManager()
            {
                return new OrderManager();
            }
    
            class CustomerManager : ICustomerManager
            {     
                int ICustomerManager.Add(string firstName, string lastName)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Customer c = new Customer { FirstName = firstName, LastName = lastName };
                        context.Customers.AddObject(c);
                        context.SaveChanges();
                        return c.CustomerId;
                    }
                }
    
                void ICustomerManager.Delete(int customerId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Customer c = context.Customers.Where(i => i.CustomerId == customerId).First();
                        context.DeleteObject(c);
                        context.SaveChanges();
                    }
                }
    
                void ICustomerManager.Update(int customerId, string firstName, string lastName)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Customer c = context.Customers.Where(i => i.CustomerId == customerId).First();
                        c.FirstName = firstName;
                        c.LastName = lastName;
                        context.SaveChanges();
                    }
                }
    
                List<Customer> ICustomerManager.FindAll()
                {
                    using (var context = new MasterDetailEntities())
                    {
                        return context.Customers.ToList();
                    }
                }
    
                Customer ICustomerManager.FindByOrder(int orderId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        return context.Orders.Where(i => i.OrderId == orderId).First().Customer;
                    }
                }
            }
    
            class OrderManager : IOrderManager
            {
                int IOrderManager.Add(string description, int quantity, int customerId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Order order = new Order { Description = description, Quantity = quantity };
                        context.Customers.First(i => i.CustomerId == customerId).Orders.Add(order);
                        context.SaveChanges();
                        return order.OrderId;
                    }
                }
    
                void IOrderManager.Delete(int orderId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Order a = context.Orders.Where(i => i.OrderId == orderId).First();
                        context.DeleteObject(a);
                        context.SaveChanges();
                    }
                }
    
                void IOrderManager.Update(int orderId, string description, int quantity)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        Order a = context.Orders.Where(i => i.OrderId == orderId).First();
                        a.Description = description;
                        a.Quantity = quantity;
                        context.SaveChanges();
                    }
                }
    
                List<Order> IOrderManager.FindByCustomer(int customerId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        return context.Customers.First(i => i.CustomerId == customerId).Orders.ToList();
                    }
                }
    
                Order IOrderManager.Find(int orderId)
                {
                    using (var context = new MasterDetailEntities())
                    {
                        return context.Orders.First(i => i.OrderId == orderId);
                    }
                }
            }
    
        }

    WCF服务:

    部分wcf接口实现:

    [ServiceContract]
        public interface ICustomerService
        {
            [OperationContract]
            List<Customer> GetCustomers();
    
            [OperationContract]
            int AddCustomer(string FirstName, string LastName);
    
            [OperationContract]
            void UpdateCustomer(Customer c);
    
            [OperationContract]
            void DeleteCustomer(int customerId);
        }
     public class CustomerService : ICustomerService
        {
    
            List<Customer> ICustomerService.GetCustomers()
            {
                List<Customer> result = new List<Customer>();
                foreach (Business.Customer i in Business.CustomerManager.Instance().CustomerList)
                    result.Add(new Customer(i));
                return result;
            }
    
            int ICustomerService.AddCustomer(string firstName, string lastName)
            {
                return Business.CustomerManager.Instance().AddCustomer(firstName, lastName);
            }
    
            void ICustomerService.UpdateCustomer(Customer c)
            {
                Business.CustomerManager.Instance().UpdateCustomer(new Business.Customer(c.CustomerId, c.FirstName, c.LastName));
            }
    
            void ICustomerService.DeleteCustomer(int customerId)
            {
                Business.CustomerManager.Instance().DeleteCustomer(customerId);
            }
    
    
        }

    Unity实现:

    接口及实现:

    public interface IServiceLocator
        {
            void Register<TInterface, TImplementation>() where TImplementation : TInterface;
    
            TInterface Get<TInterface>();
        }
    class UnityServiceLocator : IServiceLocator
        {
            private UnityContainer container;
    
            public UnityServiceLocator()
            {
                container = new UnityContainer();
            }
        
            void IServiceLocator.Register<TInterface, TImplementation>()
            {
                container.RegisterType<TInterface, TImplementation>();
            }
    
            TInterface IServiceLocator.Get<TInterface>()
            {
                return container.Resolve<TInterface>();
            }
        }

    全局静态类

    class ServiceProvider
        {
            public static IServiceLocator Instance { get; private set; }
    
            public static void RegisterServiceLocator(IServiceLocator s)
            {
                Instance = s;
            }
        }

    进行Unitity容器接口注入:

                //initialize all the services needed for dependency injection
                //we use the unity framework for dependency injection, but you can choose others
                ServiceProvider.RegisterServiceLocator(new UnityServiceLocator());  
    
                //register the IModalDialog using an instance of the CustomerViewDialog
                //this sets up the view
                ServiceProvider.Instance.Register<IModalDialog, CustomerViewDialog>(); 

    ViewModel 模块操作进行Unitity容器获取接口实例(红色实现部分)

            private void ShowAddDialog()
            {
                CustomerViewModel customer = new CustomerViewModel();
                customer.Mode = Mode.Add;
    
                IModalDialog dialog = ServiceProvider.Instance.Get<IModalDialog>();
                dialog.BindViewModel(customer);
                dialog.ShowDialog();
            } 

    MVVM DataContext关联ViewModel:

    通过DataContext关联模块的ViewModel层实现所有的操作。

    this.DataContext=  xxViewModel;

    view页面:

    ViewModel部分绑定实现:

    所有的ViewModel统一实现ViewModelBase 接口, 进行属性通知。

     public ICommand UpdateCommand
            {
                get
                {
                    if (updateCommand == null)
                    {
                        updateCommand = new CommandBase(i => this.Update(), null);
                    }
                    return updateCommand;
                }
            }
    
            public ICommand DeleteCommand
            {
                get
                {
                    if (deleteCommand == null)
                    {
                        deleteCommand = new CommandBase(i => this.Delete(), null);
                    }
                    return deleteCommand;
                }
            }

    源代码

  • 相关阅读:
    oracle11g dataguard部署指南
    扩展Oracle表空间
    ORACLE SQLloader详细语法
    Oracle Data Guard
    struts2学习(4)struts2核心知识III
    struts2学习(3)struts2核心知识II
    struts2学习(2)struts2核心知识
    struts2学习(1)struts2 helloWorld
    java单例模式等一些程序的写法....持续更新...
    峰Spring4学习(8)spring对事务的支持
  • 原文地址:https://www.cnblogs.com/zh7791/p/9264214.html
Copyright © 2020-2023  润新知