How do you to Access Data?

How do you to Access Data?

Welcome to the latest news from TalkIT. This issue will look at accessing data. Always a major theme in any serious software applications. This will be illustrated with the Entity Framework in Visual Studio. The other day, I realised with shock that I have been teaching developers data access for 15 years. So here is a brief distillation of my experience. I would be really happy to hear your thoughts on this. Just add them to end on the blog. Or join the Linked In debate . Data Access Entity Framework Contemporary Programming Techniques Can you solve this coding problem? Other Bits Data Access Applications need data. Often this is held in a relational databases. The first version of Visual Studio provided only data readers and datasets for this. These were packaged in the ADO.Net class libraries. Data readers take data from tables to provide a read only and forward only stream of data. They need an open database connection. Datasets are more multifaceted.  They are an in-memory copy of a part of the database. They are disconnected since they do not require an open connection. Since those early days a confusing range of options have evolved, including wizard driven data sources, LINQ to SQL and the Entity Framework. Data sources are almost ridiculously simple to use. Just few clicks will bind a data grid to a table. No code required. But are they too easy? If things go wrong how do we fix them? The Entity Framework is far more sophisticated. This offers a range of options that can cope with more complex data requirements. To find out more about the range of data access options, just take a look at our courses and tutorials: Courses Tutorials Entity Framework Data when it is processed in applications is held in objects. But the same data is permanently stored in relational databases tables. These are two very different ways of structuring data. A big job in any application is mapping the data between the objects and the database.  This job is automated by Object Relational Mapping (ORM) systems. So ORMs map database tables to object models. Both LINQ to SQL and the Entity Framework (EF) are ORM systems. The Entity Framework is a more sophisticated ORM tool and permits more manual coding. It is more suitable for complex object graphs. It allows for more complex inheritance or association features. The entity classes can be completely independent from the database schema they map to.  EF is proving more popular than LINQ to SQL. Here is the Wiki on EF: The EF offers three strategies for mapping data

  • Database-first
    • The database already exists
    • You generate an Entity Data Model (EDM) from the database
  • Model-first
    • You write the EDM (as an .edmx file) first
    • Then you generate the database from the EDM
  • Code-first
    • You write C# classes to represent the model
    • These can be Plain Old CLR Classes (POCOs)
    • POCOs are pure code (no need for any database-related mapping)

Contemporary Programming Techniques Software drives the world. Good software is robust, scalable and maintainable. It does what it was designed to do, but can be easily changed to meet new requirements and take advantage of new technologies. The contemporary approach is to build the software as a set of components that can be easily replaced. For example, the user interface component can be replaced to make the application available over desktop computers, websites or mobile devices. Or the data access component can be replaced to allow the application to hold data on different data providers. It’s a bit like a kit house, where you add or replace any room after the house is built. To effortlessly replace a component, the application must not be totally dependent it. To reduce this dependency components must be “loosely coupled” or exchangeable. Here are some techniques to create loosely coupled and easily testable applications.

  • Create a domain, where the real business logic will reside in external class libraries or entities.
  • Use separate repository classes to manage persistence of domain entities to the database.
  • Use dependency injection to decouple application components from each other by injecting dependent object.
  • Create mock objects for unit tests, rather than real database objects.

Can you solve this coding problem? Here is a coding puzzle. The aim is to write short and elegant programs. Write a program in a language of your choice to demonstrate the Collatz Conjecture.

  1. Inputs a positive integer.
  2. If odd, multiply by 3 then add 1.
  3. If even, divide by 2.
  4. Displays result.
  5. Stops when result is, as it will always eventually be 1

The conjecture is that no matter what number you start with, you will always eventually reach 1. This is easy to demonstrate, but is unproven. You can add your code to the end of the blog. Other Bits TalkIT in collaboration with VBug organises monthly Geek Speak talks for techies in Bristol. The talks are aimed at developers and team leaders. We have some interesting talks scheduled for 2014. For descriptions just click: New online courses in MVC and C#.Net will be added to the TalkIT site in April. Take an advanced look at what is in the course: Developing in ASP.Net MVC Developing in C#.Net

Photos David Ringsell 2014 ©

Scroll to Top