The status of Lucene2Objects

After some time without being able to work into it, I’ve managed to put some time into Lucene2Objects again. First thing I did on my last session was to work on separating the attributes from the actual Lucene2Objects project for a very simple reason that was brought to my attention by a fellow user of the library. Currently if you want to annotate your entities on your domain project, you will have to import the Lucene2Objects library into the domain project, thus adding a dependency on the library and on Lucene .NET and on Lucene Contrib project which is used for importing several analyzers and any other dependencies these might bring along. Now, for a domain project, which is supposed to have as less dependencies as possible, this is very heavy duty, hence the need for a separation (of concerns if you will).

The basic idea that I followed on this new update was to separate the project into 2 different libraries, one very light containing the attributes with no dependencies at all and the actual library. Obviously this will make me create another package, which I will do it very soon, but will hopefully allow people to integrate easily with Lucene2Objects .

My next step is working over adding collection support for Lucene2Objects . I have a few ideas on this and I hope a new version should be done soon, but there is nothing worth pushing now. Hopefully, I will manage to put more time into this from now on, so feel free to let me know if there’s something you’d like to see on Lucene2Objects !

Back in business!

I’ve been for sometime without being able to write given that I’ve changed my location to the UK. Now that all arrangements have been take care and I can consider myself settled, I’ll continue with posting a couple of new posts soon. Also, I’m planning on moving my development on Lucene2Objects into the new features that I wanted to get working for version 2.

Thanks to all the folks that reached to me to know about the status of Lucene2Objects!

David

Testing S#arp Lite Repositories with Moq

One pending matter I’ve always had is to improve my testing skills, there I said it. I test, but not as much as I should. When I say test, I mean Unit Test, not just test the application by launching it and starting to poke it. One thing that I found to be a really outstanding idea with S#arp Lite is that repositories eliminated many complications. If you had a repository and needed to run a query against it, just call GetAll and throw some Linq at. Grated, it assumed that the Linq provider for the underlaying data model was mature, but with NHibernate and EntityFramework being the two ORM of my choice always, that seems like a fair assumption.

However, this has a downside, I tried to test the repositories and had a really rough time getting to test a repo that was using an underlying IQueryable item. However, this became quite clear with time, and now I can test my repos. Let’s make a fairly simple test scenario. Let’s assume I have a user class, with a few standard properties, pretty much like this one:

public class User : Entity
{
 public virtual string Password { get; set; }

 public virtual string Email { get; set; }

 public virtual bool Blocked { get; set; }

 public virtual int LoginCount { get; set; }
} 

Now, I have a class called Membership that handles my Membership logic, that is, logging users, blocking them after a couple of bad logins, etc. That class should look like this:

public class Membership
{
 IRepository<User> _usersRepository;

 public Membership( IRepository<User> usersRepository )
 {
  _usersRepository = usersRepository;
 }
 
 public bool IsValidUser( string email, string password )
 {
  //create test first!
  return false;
 }
} 

Now, we need to create a test case. Let’s call it, MembershipTests

[TestFixture]
public class MembershipTests
{
 [TestFixtureSetUp]
 public void SetupTestEnvironment()
 {
 
 }
}

Now, I want to create a Mock repository to pass it along to my test Membership class, but I need to do it so that it simulates the data backed without touching my actual data nor getting too slow. Obviously we need a list, but not just any list, we need a list that can pose for a Repository or at least fake it. That’s why we need to create this sort of list, a QueryableList:

public class QueryableList<T, TId> : List<T>, IQueryable<T> where T : EntityWithTypedId<TId>
{
 #region Constructors
 public QueryableList()
 { }

 public QueryableList(IEnumerable<T> source)
  : base(source)
 { } 
 #endregion

 #region IQueryable<T> implementation
 public Expression Expression
 {
  get { return ToArray().AsQueryable().Expression; }
 }

 public Type ElementType
 {
  get { return typeof(T); }
 }

 public IQueryProvider Provider
 {
  get { return ToArray().AsQueryable().Provider; }
 }
 #endregion

 public void UpdateEntity(T entity)
 {
  var index = -1;

  for (var i = 0; i < Count; i++)
   if (this[i].Equals(entity))
    index = i;

  if (index == -1)
   Add(entity);
  else
   this[index] = entity;
 }
} 

Voila! We have a List that directly implements IQueryable, which is a good thing, not a hard thing to do, but it will help us a lot. We need to get the entity of the List and the Id that is going to be used on the list to keep it as generic as possible, so when we need to test a repo of entities with typed id’s, we won’t have to rewrite much. The UpdateEntity method will mimic the SaveOrUpdate method we have on our repo using the Equals method to invoke the Equality comparer provided by S#arp Lite. Now, we need to setup our Mocks. We go back to the TestSetup and let’s setup our environment:

[TestFixture]
public class MembershipTests
{
 private Membership _membership;
 
 [TestFixtureSetUp]
 public void SetupTestEnvironment()
 {
  var usersMockedRepo = new Mock<IRepository<User>>();
  
  var users = new List<User> { new User{ Blocked = false, Email = "david@someplace.com", Password = "a password" } };
  var list = new QueryableList<T, int>(users);
  
  //Mock GetAll
  usersMockedRepo.Setup(x => x.GetAll()).Returns(list);
  
  //Mock the Get
  usersMockedRepo.Setup( x => x.Get( It.IsAny<int>() ))
      .Returns( (int id) => list.AsQueryable()
      .SingleOrDefault(x => x.Id.Equals(id)));
  
  //Mock the SaveOrUpdate using our own
  usersMockedRepo.Setup(x => x.SaveOrUpdate(It.IsAny<T>()))
      .Callback((T entity) => list.UpdateEntity(entity));
      
  //Mock the delete
  usersMockedRepo.Setup(x => x.Delete(It.IsAny<T>())).Callback((T entity) => list.Remove(entity));
  _membership = new Membership(usersMockedRepo.Object);
 }
}

Now, we have setup our very own mocked repository. We need to make a test now for the IsValidUser method we left before. Let’s write a simple test case:

[TestCase]
public void CheckBasicAuthentication()
{
 var checkValidUser = Membership.Instance.IsValidUser("david.conde@gmail.com", "a password");
 var checkInvalidUser = Membership.Instance.IsValidUser("david.conde@gmail.com", "another password");

 Assert.AreEqual(checkInvalidUser, false);
 Assert.AreEqual(checkValidUser, true);
}

And that’s it! We have our own test and we can now create as many test cases as we want all relying on a simple structure like a list. There is one final thought here, which came to mind while reading this StackOverflow post. The idea is to put the setup into a helper method, so we can reuse it with different test scenarios:

Please note that the following code can induce headaches 🙂

public static class MockExtensions
{
 public static void SetupIQueryableTypedRepository<T, TId>
  (this Mock<IRepositoryWithTypedId<T, TId>> mockObject, IEnumerable<T> source)
  where T : EntityWithTypedId<TId> where TId : IComparable
 {
  var list = new QueryableList<T, TId>(source);

  mockObject.Setup(x => x.GetAll()).Returns(list);
  mockObject.Setup(x => x.Get(It.IsAny<TId>())).Returns((TId id) => list.AsQueryable().SingleOrDefault(x => x.Id.Equals(id)));

  mockObject.Setup(x => x.SaveOrUpdate(It.IsAny<T>())).Callback((T entity) => list.UpdateEntity(entity));
  mockObject.Setup(x => x.Delete(It.IsAny<T>())).Callback((T entity) => list.Remove(entity));
 }

 public static void SetupIQueryableRepository<T>(this Mock<IRepository<T>> mockObject, IEnumerable<T> source)
  where T : Entity
 {
  var list = new QueryableList<T, int>(source);

  mockObject.Setup(x => x.GetAll()).Returns(list);
  mockObject.Setup(x => x.Get(It.IsAny<int>())).Returns( (int id) => list[id] );
  
  mockObject.Setup(x => x.SaveOrUpdate(It.IsAny<T>())).Callback( (T entity) => list.UpdateEntity(entity) );
  mockObject.Setup(x => x.Delete(It.IsAny<T>())).Callback((T entity) => list.Remove(entity));
 }
}

Now, we can reduce our Setup method to this:

[TestFixtureSetUp]
public void SetupTestEnvironment()
{
 var usersMockedRepo = new Mock<IRepository<User>>();
 var users = new List<User> { new User{ Blocked = false, Email = "david@someplace.com", Password = "a password" } };
 
 usersMockedRepo.SetupIQueryableRepository(users);
 _membership = new Membership(usersMockedRepo.Object);
}

If you just want to be able to test your S#arp Lite repositories using this, then just get this extension to your code, set your mocked repositories using this idea and done! If you have any other thoughts, let me know on the comments!

Lucene2Objects roadmap

Since the release of Lucene2Objects I’ve seen a few people downloading it and some have left a couple comments, some have directly emailed me and others even found me on BitBucket and sent me a message over there. I think that is awesome, which is why I plan to devote a bit more of my time to get more cool stuff into Lucene2Objects, also there is the fact that I actively use it on a couple projects 🙂

So far, there is only one thing that comes to mind that we need to have released on L2O, which is the fact of self tracking objects, that is having the object been able to track any changes to it and update them on the Index. I’ve been devoting some time into this matter and I’ve come with a couple of ideas, but to get them, there is a couple of basic principles I need to keep in mind:

  • Lucene2Objects tracking system needs to be as unobtrusive as possible (And not because the word is trendy)
  • As it has been so far, getting the tracker up and running should be easy for anyone not interested in getting too deep
  • If somebody is indeed interested in going deep it needs to be also easier than going into Lucene .NET(which is not easy at all!!)

But, if you think of it, these are just basic design principles, the first one is basically YAGNI and KISS and the other two are just plain old Bertrand Meyer’s Open Closed Principle. Despite the fact that these should rule most development projects, the actual panorama is not like that. Anyways, I digress…

The other thing that so far I think that is necessary in order to have self-tracking entities (and collections, which are after all, entities) is that to be able to do some self-tracking, the entities to be tracked need to have some sort of primary key, or something that distinguishes them. Having said that, I can think only of forcing the use of a primary key on any entity to be tracked, for instance, using the [PrimaryKey] attribute. The alternative is generating a key somehow, but since you don’t always (actually almost never…) know which fields identify an entity, there is no way of warrantying that this is a safe or unique procedure.

There is an idea for Sharp Lite users which is using the [DomainSignature] attribute for auto generating the so called key. For those of you who don’t know what SharpLite is, is a really nice development framework made mostly by Billy McAfferty for developing .NET apps, and although it has a couple detractors it’s a really good way to start any small and mid-sized projects. So, for all of you developers reading this post and interested on how L2O progresses, Is there something you want to see on L2O? Feel free to comment or email me, I’m interested on hearing your thoughts.

Lucene2Objects goes public!

I’ve been asked a couple times to release the Lucene2Objects code out in the wild and it was the intention all the way, but I didn’t had the proper time to do it, not the proper bandwidth.
The new Lucene2Objects has a couple new changes and it’s more lightweight than the first versions since it doesn’t depends on Ninject anymore. Feel free to browse the code, leave issues or just email me with any problem you have.
If you are using Lucene2Objects, let me know as I want to know if it’s been useful out there.

All the best,
David

S#arpLite: “At least one ISessionFactory has not been registered with IoC”, Darn with the redirections…

Yesterday I installed ASP .NET MVC 4 beta so I can play with it for a couple days. To be honest, I haven’t built one simple app because I haven’t had the time to do it, but I popped up my Visual Studio 2010 today and booted a S#arp lite project from the template. As usual, I made my move to Fluent nHibernate but when I loaded the project:

After spending a couple hours digging my way into my machine and debugging I went into the S#arp Lite discussion group and found this post of somebody having the same issue, finally the last answer hit the nail:

Have you installed MVC4 on you machine? If yes, check reference in Init project, it references MVC4 whereas Web project references MVC3.

Mmmmm… I did… so I tried this…

<dependentAssembly>
 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
 <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
 

Voilà! It works now… Darn with the redirections…

Introducing Lucene2Objects

I’ve been playing with Lucene .NET for over 2 years now. It all started as part of my incorporation to a NLP investigation group and my first task was to look into Lucene since nobody was using it. I was baffled with the strength that Lucene had, besides, the biggest players were using it! Now that I’ve get to know it a bit better I see why so many people use it, put simple: It’s awesome! However, Lucene does have a problem which is the learning curve. Wrapping your head around the concept of documents, queries, analyzers and how to get a pseudo efficient search working are a few of the issues with using Lucene on a project.

Enter Lucene2Objects, my basic idea is to make a simple interface into Lucene for those developers wanting to incorporate search annotations into the domain model. Now, let’s take an example of a system handling messages (of the “Hi! How do you do?” kind, not the WM_PAINT kind), is most probably that users would like to search for something inside their messages. A (very) basic approach gives us a simple class:

public class Message
{
 public int Id { get; set; }

 public string Text { get; set; }

 public string Title { get; set; }

 public DateTime Sent { get; set; }
}

This is neat, but if I want to implement search I can either use the services provided by my DB backend as Full Text Indexing from SQL Server (which is awesome by the way, but lacks some other cool stuff) but the biggest problem is that we would then be fixing (or tightly coupling, for the fan boys of OOP/IoC/SOLID) the data store to the solution of finding a text, which is almost definitely a bad thing.

Now, if we want to use Lucene, we need to make a few configuration stuff, learn some stuff about indexing, tokenizers, analyzers and a huge list of stuff that some folks (me included) find amusing, but others find really boring (not to mention those who find it daunting). But imagine a world where you could do something like this:

var iWriter = new IndexWriter(Environment.CurrentDirectory + @"index");
var message = new Message { Id = 12, Sent = DateTime.Now, 
                            Text = "Some text on the message!", 
                            Title = "This is the title" 
              };
iWriter.AddEntity(message);
iWriter.Close();

Cool uh? Just point a folder and save. Nice! Well, and how would I search for stuff on that folder? Easy piece

var iReader = new IndexReader(Environment.CurrentDirectory + @"index");
var messages = iReader.Search&lt;Message&gt;("text");

foreach (var message in messages) {
 Console.WriteLine("Message: {0}", message.Title);
}

Fine! And how does my model knows where to search? What to index? What not to index? Well, validations were a similar issue, so, why not give it a similar solution? Just annotate away!

[SearchableEntity(DefaultSearchProperty = "Text")]
public class Message
{
 public int Id { get; set; }

 [Indexed]
 public string Text { get; set; }

 [Indexed]
 public string Title { get; set; }

 public DateTime Sent { get; set; }

 public DateTime? Read { get; set; }
}

If you liked that way of handling things with Lucene, you’ll love Lucene2Objects. Keep in mind however, that I’m the only person working with this idea, so if you like it and want to put something into it, let me know! For now, I’ll leave the Lucene2Objects as a package in Nuget, so you can play with it. I’ll put it into my BitBucket repo this week along with my Scaffolders for SharpLite.

SharpLite scaffolding is out! (beta)

This simple post is to show how to gt started using s small Scaffolder I made for SharpLite projects. It takes as input a contoller name and generates the controller to use with a SharpLite repository and the views using the regular Mvc3 scaffolding from Steven Sanderson. As a disclaimer, my code is just some tweaks different from the actual scaffolder from Steven Sanderson, and I haven’t tested it too much so don’t take this as a final product. I will, however keep working on this as time goes by and hopefully we may add some more new stuff!

Ok, enough of that! Just install the package from the NuGet feed…

Install-Package SharpLite.Scaffolding

Fire up your Package Manager Console and assuming you have an entity called Product and you want to make your own controller to manage products, just type:

 Scaffold slController ProductsController 

That’s it! This is a quite quick and dirty Scaffolder, but as I said, I will keep doing some more stuff into it. If you have any doubts or issues, feel free to comment or just send me a mail.

EDIT: For those of you who want to check out the source code of the scaffolders and follow the project, the code is hosted here

Using Sharp Lite with Fluent nHibernate

One thing I didn’t liked too much when first met S#arp Lite was the fact that Billy McCafferty decided to drop Fluent nHibernate as the defacto mapper for entities. There was a big discussion on the matter of nHibernate making it’s own fluent (or was it Loquacious?) API to map entities on a lot of places, and finally James Gregory, the man who gave us Fluent nHibernate spoke on the matter of nHibernate making an API that looked a lot like his.

The conclusion is pretty simple, Fluent nHibernate is not dead, not even close, there are a lot of folks (me included) that like it and we are not willing to let it go. Having said that, I wanted to integrate my Fluent nHibernate into S#arp Lite, and just as Billy said, is a dead simple thing to do, still will share it here.

The first thing you need to do is to reference the Fluent nHibernate library from your NorthwindDemo.NHibernateProvider project, so you can use it there. After that, you can make your own mappings on the provider. If you don’t know what is a Fluent Mapping, you should read this wiki before you start here. Now that we have our mappings made, we need to change the nHibernate boot process found in NHibernateInitializer so we use Fluent nHibernate instead. Go to the NHibernateInitializer class and change the code for this:

public static Configuration Initialize()
{
 var cnf 
          = Fluently.Configure()
      .Database(MySQLConfiguration.Standard.ConnectionString( 
    x => x.Database("MyDb")
          .Username("MyUserName")
          .Password("MyPassword")
          .Server("MyServer"))
   )
       .Mappings( m => m.FluentMappings.AddFromAssemblyOf&lt;CustomerMapping&gt;());

 var configuration 
          = cnf.BuildConfiguration()
        .Proxy( p => p.ProxyFactoryFactory&lt;DefaultProxyFactoryFactory&gt;() )
        .CurrentSessionContext&lt;LazySessionContext&gt;();

 return configuration;
} 
 

Notice that I’m not using SQL Express like the default install uses, I’m using MySQL instead. The magic of the thing is on the line:

.Mappings( m => m.FluentMappings.AddFromAssemblyOf<customerMapping>()); 
 

Here, I’m telling FNH to load all the mappings specified in the assembly of CustomerMapping, which is one of my entities’ mappings. After that, we register the DefaultProxyFactoryFactory to manage the lazy loading, which basically gets rid of the old nHibernate.Castle so we don’t have to use an external loader for proxy classes. With this configuration, you should be on your way to use Fluent nHibernate with your S#arp Lite project.

Finally, there is one thing. I think, not so sure though, that there is a version of Fluent NH for nHibernate 3.2, however, if you are using an older one, like me!, I’m using v1.2 which looks for nHibernate 3.1, you will need to make an Assembly redirect, which is easy, just lk in your web.config file for this line:

<runtime>
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
   <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
 </assemblyBinding>
</runtime>
 

And change it for this one:

<runtime>
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
   <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
   <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
   <bindingRedirect oldVersion="0.0.0.0-3.1.0.4000" newVersion="3.2.0.4000" />
  </dependentAssembly>
 </assemblyBinding>
</runtime>
 

The basic idea is to tell the system that when someone (FNH) asks for nHibernate from version 0.0.0.0 to 3.1.0.4000 the assembly to resolve is version 3.2.0.4000, so the binding is done properly. This should be it, you should have your own S#arp Lite project running on Fluent nHibernate!!

Happy coding! And as usual, comenting is not forbidden 😉