HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpModerate

How do I extract interfaces from existing similar classes?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
howclassesextractexistinginterfacesfromsimilar

Problem

I have about 11 singleton-esque classes divided over a dozen files:

the first file is one that gathers all singleton definitions and makes them callable on a request scope:

public sealed partial class ExpertiseverslagEngine
{
    readonly IOrganizationService _oService;
    readonly XrmServiceContext _oServiceContext;

    static readonly object Padlock = new object();

    ExpertiseverslagEngine()
    {
        _oService = CrmConnector.GetOrganization();
        _oServiceContext = new XrmServiceContext(_oService);
    }

    public static ExpertiseverslagEngine Instance
    {
        get
        {
            lock (Padlock)
            {
                if (UnitOfWorkHelper.CurrentDataStore["ExpertiseverslagEngine"] == null)
                {
                    UnitOfWorkHelper.CurrentDataStore["ExpertiseverslagEngine"] = new ExpertiseverslagEngine();
                }
                return (ExpertiseverslagEngine)UnitOfWorkHelper.CurrentDataStore["ExpertiseverslagEngine"];
            }
        }
    }
}

public sealed partial class OnderhoudEngine
{
    readonly IOrganizationService _oService;
    readonly XrmServiceContext _oServiceContext;

    static readonly object Padlock = new object();

    OnderhoudEngine()
    {
        _oService = CrmConnector.GetOrganization();
        _oServiceContext = new XrmServiceContext(_oService);
    }

    public static OnderhoudEngine Instance
    {
        get
        {
            lock (Padlock)
            {
                if (UnitOfWorkHelper.CurrentDataStore["OnderhoudEngine"] == null)
                {
                    UnitOfWorkHelper.CurrentDataStore["OnderhoudEngine"] = new OnderhoudEngine();
                }
                return (OnderhoudEngine)UnitOfWorkHelper.CurrentDataStore["OnderhoudEngine"];
            }
        }
    }
}


The other 11 files are all but 1 built in this manner:

```
public partial class ExpertiseverslagEngine
{

public List Retrieve_Active()
{
return (from e

Solution

Extracting an interface essentially boils down to taking the signatures for all public members, making them part of the interface you're extracting, and then making the type implement the interface you've extracted.

Tools like ReSharper work amazingly well for this (the partial class is handled, no sweat - in fact, the interface only needs to be specified in one place, if it's in both files one of them is redundant):

But before you start extracting interfaces, you should ask yourself why you have 11 classes that are essentially identical. Wouldn't a generic class make your life easier? It could implement a generic interface, which could look like this:

public interface IEngine
{
    IEnumerable RetrieveActive();
    T Retrieve(Guid id);
    IEnumerable RetrieveSchade(Guid id);
    string Update(T value);
    string Create(T value);
}


Where T would be slfn_expertiseverslag and U would be slfn_schadebeschrijvingwagen, but then the type parameters would need perhaps better names.

Note that I'm exposing IEnumerable, not List - if the goal of the method is to expose data that's meant to be consumed you should expose IEnumerable. Expose a List when the client code needs to be able to Add() and Remove() items.

There's a problem with your naming. slfn_expertiseverslag and slfn_schadebeschrijvingwagen don't follow naming conventions for types. Should be PascalCase, no underscores (save snake_case for python ;).

As to address whether it's worth it to extract an interface, I can't really tell (it could very well be just additional complexity). I think making your class generic would definitely be helpful though.

Code Snippets

public interface IEngine<T, U>
{
    IEnumerable<T> RetrieveActive();
    T Retrieve(Guid id);
    IEnumerable<U> RetrieveSchade(Guid id);
    string Update(T value);
    string Create(T value);
}

Context

StackExchange Code Review Q#42854, answer score: 10

Revisions (0)

No revisions yet.