2

I implement table data gateway on data access layer and transaction script on business. Is it possible to make table data gateway with non static methods and therefore create DAL objects in BL to calls gateway methods?

Now I do

var result = myGateway.SelectAll(p1, p2, p3);

with non-static methods it would be

var MyGWObject = new myGateway();
var result = MyGWObject.SelectAll(p1,p2,p3);

in every transaction (probably organized as a class and the object would be its private variable).

Is this ok or really bad? And also is the second approach when object is created more like Table Module or just some mess?

2

Firstly, your code is very difficult to read as you are ignoring C# coding conventions: you have a camelCase class name and a PascalCase variable name. So in my examples below, I've fixed that to make it easier for other folk, familiar with C# conventions, to read.

var result = MyGateway.SelectAll(p1, p2, p3);

This is an example of a locator antipattern. As MyGateway.SelectAll is static, there's no easy way to swap out the actual implementation for a simpler one when testing for example. So you are right to want to improve this.

However your proposed solution, along the lines of:

class SomeClass
{
    private readonly MyGateway _myGateWay = new MyGateway();

    public SelectAllResult SomeMethod()
    {
        ...
        var result = _myGateWay.SelectAll(p1,p2,p3);
        ...
    }
}

isn't any better. You are still tightly coupling your class to MyGateway, again making testing harder than it need be.

A third solution, which overcomes these problems, is to use an interface and to pass an instance of a type that implements it into the constructor:

class SomeClass
{
    private readonly IGateway _myGateway;

    public SomeClass(IGateway myGateway)
    {
        _myGateway = myGateway;
    }

    public SelectAllResult SomeMethod()
    {
        ...
        var result = _myGateWay.SelectAll(p1,p2,p3);
        ...
    }
}

This dependency injection approach decouples your class from MyGateway and makes testing far easier.

  • @DavidAmo I really didn't realize how terrible my notation is thank you for that. I just want to know whether nonstatic function in table data gateway are ok or not and according to your answer I assume they are. What makes me wonder is if your solution won't bring me some pain. First what came into my mind I can pass a different class implementing IGateway and then be surprise what result I got. Second thing is, considering SomeClass is supposed to be called at presentation layer is it good to type data access layer classes names at presentation layer? – Bendom Feb 22 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.