How to use entity framework with business objects

225 views Asked by At

In our solution we have business objects like this:

[Serializable()]
public class Vendor : Recordable
{
protected int vendorID;
protected string vendorName;
protected string vendorNumber;

public void Save()
{
    //call to sql stored procedure using ado.net to save
    dbhelper.addSPParam("name",vendorName);
    dbhelper.addSPParam("number",vendorNumber);
    dbhelper.addSPParam("id",vendorID);
    dbhelper.executeSP("sp_name");
}

Vendor(int id)
{
    //call stored procedure to get vendor data by ID
    this.vendorID = reader["vendorID"];
    this.vendorName = reader["vendorName"];
    this.vendorNumber = reader["vendorNumber"];
}

Our code is tied to ADO.net, but we'd like to use entity framework for querying instead of writing everything by hand. We'd like to keep some stored procedures because they have business logic in them, but a majority of the stored procedure calls we'd like to use entity framework instead.

How can we shape our project to use entity framework, some of the stored procedures, and add unit tests (none of our code is tested)?

Would love any and all ideas.

1

There are 1 answers

0
forsvarir On

This question is really too broad, you need to try and break it down into smaller, answerable portions (some of which will probably be duplicates).

This answer may well get deleted, since it's really more of a big comment than a direct answer.

That said, I'd start by creating a test project to validate any approach against a copy of your test database. This will allow you to perform any tweaks you might need to in order get EF working with your database before you get too deeply entrenched in your existing code base.

You'll need to know how to call stored procedures.

You're presumably going to use a database first approach.

For unit tests, you'll need to pick a testing framework (MS, NUnit & XUnit seem the most popular).

You may need a mocking framework (Moq, NSubstitute, etc) to write tests that don't hit the database.

You may also want to look into dependency injection and an IOC container (Unity, Castle Windsor, Ninject) to help with injecting dependencies if you're going to start breaking down your code into interfaces/dependencies to facilitate testing.

You're going to want to start looking into using interfaces if you don't already in order to separate the concerns of your code and make mocking for testing easier.

From your code, it's not entirely clear how you use the protected fields in your class once you're read them from the database, you may also want to look into something like automapper to help with mapping between DB entities and your BL entities.