English 中文(简体)
DI and JSON.NET
原标题:

I m using JSON.NET to serialize and deserialize object for different purposes. I m a big fan of DI but the code below gives me the chills. Is smells like bad code:

public class Foo : Baz
{
    private readonly IBar bar;

    public Foo()
        : this(ObjectFactory.GetInstance<IBar>())
    { }

    public Foo(IBar bar)
    {
       if (bar == null)
            throw new ArgumentNullException("bar");

       this.bar = bar;
    }

   ... rest of class ...
}

The default constructor is the thing that gives me the chills. I ve added this to support the deserialization caused by JSON.NET:

string jsonString = ...;
string concreteBazType = ...;

Baz baz = (Baz)JsonConvert.DeserializeObject(jsonString, Type.GetType(concreteBazType);

Notice the class Foo inherrits from the abstract base class Baz!

My question to all you DI and JSON.NET geeks out there: how can I change to code to avoid the code-smell the default constructor gives me in class Foo?

问题回答

This is a common problem with all sorts of Data Transfer Objects, whether they fit into JSON.NET, WCF or other technologies. In fact, you could say that all application boundary-facing classes suffer from this problem to one degree or the other. The problem is equivalent for Windows Forms Controls and other display technologies.

In the other end of the application stack, we see the same issue with Configuration objects and potentially with some ORM types (such as Entity Framework classes).

In all cases, the best approach is to treat all such Boundary Objects as dumb types with more structure than behavior. We already know that this is the right thing to do for WCF DataContracts, ASP.NET MVC Views, Windows Forms Controls, etc. so it would be a well-known solution to the problem.

Just like we have Controllers to populate Views in a UI, we can have service operations, mappers and whatnot that map DTOs to Domain Objects. In other words, your best recourse would be to not attempt to serialize Foo at all.

Instead, define a FooJson class that represents the static structure of Foo and use a Mapper to translate between the two.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签