MVVM bez Entity Framework, co z modelem?

0

Jest wiele tutoriali dla MVVM+EF ale nic nie znalazłem bez EF. Zrobiłem to tak na czuja i nie wiem czy dobrze, chciałbym aby ktoś to sprawdził.
Zamiast DbContext którego użyłbym w EF rozszerzyłem ObservableCollection aby pobierało i zapisywało dane do bazy. Nie wiem czy to tak powinno być czy może lepiej uzupełniać listę w ViewModelu, czy może wywalić model i przez ViewModel przekazywać dane z bazy od razu do widoku albo zrobić to jeszcze inaczej?
Model:

class Data
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Pass { get; set; }
    }

    class ContextObservableCollection : ObservableCollection<Data>
    {
        public ContextObservableCollection()
        {
            Fill();
        }

        public void Insert(Data data)
        {
            this.Add(data);
            SQLiteConnection conn = new SQLiteConnection("Data Source=Passwords.s3db");
            SQLiteCommand cmd = conn.CreateCommand();
            cmd.CommandText = String.Format("insert into Password (Name, Pass) values ('{0}', '{1}')", data.Name, data.Pass);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Fill()
        {
            SQLiteConnection conn = new SQLiteConnection("Data Source=Passwords.s3db");
            SQLiteCommand cmd = conn.CreateCommand();
            cmd.CommandText = String.Format("select * from Password");
            try
            {
                conn.Open();
                var dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Data data = new Data();
                    data.Id = dr.GetInt32(0);
                    data.Name = dr.GetString(1);
                    data.Pass = dr.GetString(2);
                    this.Add(data);
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

ViewModel

class DataViewModel
    {
        public DataViewModel()
        {
            DataContext = new ContextObservableCollection();
            AddCommand = new DataAddCommand(this);
        }
        public ContextObservableCollection DataContext { get; set; }
        public ICommand AddCommand { get; private set; }

        internal void AddData()
        {
            DataContext.Insert(new Data() { Name = "abc", Pass = "def" });
        }
    }

View

<Grid>
        <ListView HorizontalAlignment="Left" ItemsSource="{Binding Path=DataContext}" Height="201" x:Name="listview" Margin="277,54,0,0" VerticalAlignment="Top" Width="200">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Header="Name" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Pass}" Header="Pass" Width="50"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Content="Add" Command="{Binding AddCommand}" HorizontalAlignment="Left" Margin="93,82,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
0

Zostaw ViewModel w spokoju. ;)
Ja bym zrobił Model coś w ten deseń:

   class Person
   {
      public int Id { get; set; }

      public string Login { get; set; }

      public string Password { get; set; }
   }

   interface IRepository<T> where T : class
   {
      void Add(T entity);

      void Delete(T entity);

      /* Pozostałe standardowe metody... */
   }

   interface IPersonRepository : IRepository<Person>
   {
      IEquatable<Person> InactiveAccounts();

      /* Pozostałe nie standardowe metody... */
   }

   class PersonRepository : IPersonRepository
   {
      private SqlConnection _sqlConnection = new SqlConnection("");

      public void Add(Person entity)
      {
         // SQL Query
         throw new NotImplementedException();
      }

      public void Delete(Person entity)
      {
         // SQL Query
         throw new NotImplementedException();
      }

      public IEquatable<Person> InactiveAccounts()
      {
         // SQL Query
         throw new NotImplementedException();
      }
   }

   class PersonService : IPersonService
   {
      private readonly IRepository<Person> _personRepository;

      public PersonService(IRepository<Person> personRepository)
      {
         _personRepository = personRepository;
      }

      public bool AddPerson(Person person)
      {
         // Wlidacja
         _personRepository.Add(person);
         return true;
      }

      public bool DeletePerson(Person person)
      {
         // Wlidacja
         _personRepository.Delete(person);
         return true;
      }

      // Pozostała biznesowa logika...
   }

   interface IPersonService
   {
      bool AddPerson(Person person);
      bool DeletePerson(Person person);

      // ...
   }

Ogólnie: repository/service pattern, unit of work jakieś cuda typu CQRS. Ogólnie zainteresuj się tematem to następne hasła zaczną się pojawiać. :)

0

Ok, zobaczę to jutro.

0

Odradzam bardzo mocno korzystanie z UoW i całej tej reszty - niepotrzebnie komplikuje się prosty kod. Tutaj coś więcej na ten temat pisałem: Testowanie Repository

1 użytkowników online, w tym zalogowanych: 0, gości: 1