sshushliapin / Sitecore.FakeDb

Unit testing framework for Sitecore.
MIT License
117 stars 36 forks source link

Simplify adding a DbItem to a Db and getting the Item instance back out of the FakeDb #98

Open sshushliapin opened 8 years ago

sshushliapin commented 8 years ago

Asked by @kamsar in Twitter. The problem is that in order to get an Item instance, one have to write three lines of code:

DbItem dbItem = new DbItem("item");
db.Add(dbItem);
Item item = db.GetItem("/sitecore/content/item");

There is a suggestion to introduce new db.Create() extension method which would combine the Add() and GetItem() functionality:

DbItem dbItem = new DbItem("item");
Item item = db.Create(dbItem);
kamsar commented 8 years ago

:+1:

pveller commented 8 years ago

I very much like the idea!

I was thinking about the semantic though and I'm not sure I like bolting on extension methods to the Db object. It looks a little bit out of place. I was searching for an example and fluent assertions come to mind with their Should().

What if if we did something like Db.Api.Create() or even Db.Api().Create() anticipating future overloads?

Just thinking out loud. Again, fully support the idea of having a shortcut API for add-and-get. But if it's a beginning of a fluent API of sort then maybe Db itself is not a good home for new extension methods?

Thought? Am I overthinking?

Sent from my iPhone

On Oct 27, 2015, at 2:56 PM, Kam Figy notifications@github.com wrote:

— Reply to this email directly or view it on GitHub.

sshushliapin commented 8 years ago

So, technically it is possible to return an Item from the existing db.Add() methods. It'll not be a breaking change and there are no new methods needed in this case.

I just have a feeling that the Add() method should always be void and Create() should return some result. Tried to find examples in .NET and Sitecore API but realized that it's not so obvious. There are numerous variants available...

I don't think there is a place for the fluent API. There are two user scenarios:

  1. I want to add items to my database, meaning there are some items needs to be there. I don't need the Item instances right here in the test, I might get them in my SUT later if necessary. Db.Add() or collection initializers are used.
  2. I need to get a Sitecore Item instance right here, right now. I want to create it using the minimal code: Item item = db.Create("home").

But.. looking on the second scenario, I also have a feeling that smth is wrong here...

//cc: @kamsar

sshushliapin commented 8 years ago

The alternative might be to introduce builders. For instance:

var item1 = db.Build<Item>().FromDbItem(new DbItem("root"));
var item2 = db.Build<Item>().WithName("home");
sshushliapin commented 8 years ago

@pveller Yep, that is the fluent API :-D

pveller commented 8 years ago

Totally! :) I knew something was wrong with the db.Create() and I also remember mentioning the fluent API back when @kamsar asked for the simple add-and-get.

I like it a lot. Builders it is!