Question

This site only queries a database - NO CRUD

I have a Service DLL that I call from a ApiController the ApiController methods are called by ajax requests in my HTML.

My problem is it's getting pretty hairy in my service. Here are just a few examples of the method stubs. I have more methods than this. Then it get's even hairy as I have to wrap every service call in my APIController.

Service

    public IEnumerable<CodedValue> GetStreetSuffix()
    {
        using (var s = new SqlConnection(this.connection))
        {
            s.Open();
            var results = s.Query<CodedValue>(
                 @"SELECT DISTINCT STR_SFX Value,STR_SFX Code FROM Addresses  ORDER BY 1").ToList();
            return results;

        }            
    }

    public IEnumerable<dynamic> GetStreetNumbers(string term)
    {
        using (var s = new SqlConnection(this.connection))
        {
            s.Open();
            var results = s.Query<dynamic>(
                 @"SELECT DISTINCT STR_NUM FROM Addresses where ISNULL(RTRIM(STR_PFX) + ' ','') + RTRIM(STR) + ISNULL(' ' + STR_SFX,'') = @q", new { q = term }).ToList();
            return results;

        }
    }

APIController

[HttpGet]
public IEnumerable<dynamic> ListStreetNumbers(string term)
{
    var searchservice = new SearchService();
    var results = searchservice.GetStreetNumbers(term);
    return results;
}

[HttpGet,HttpPost]
public IEnumerable<dynamic> CountParcelsByAddresses(Address json)
{
    var searchservice = new SearchService();
    var results = searchservice.CountParcelsByAddress(json);
    return results;
}     

What can I do to make this cleaner while not abstracting the hell out of it. Most of what I did was trying to avoid having SQL queries in my controllers or in my HTML.

Was it helpful?

Solution

I agree - there should be no SQL in controllers or HTML.

I create a separate interface-based persistence tier and isolate it in the implementation.

OTHER TIPS

Take a look at SocialGoal.

The project uses Entity Framework that uses a repository pattern to hold an instance of the dbcontext object (Database mapping layer). Each repository also inherited from a repositorybase class that implements generic methods such as Get, GetAll, Create, Delete method for the dbset object. If you prefer SQL statement you can create in that layer. Your service layer will query the repository layer and get the domain objects you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top