Question

Firstly, I want to apologize for my English, not my strongest side.

To the question. In my current project, I have interfaces to my entities so I can use Subsonic attributes at my head entites and I want to be able to seamlessly switch O/R mapper in the future. Anyway, I get an error when I try to use my interfaces and SimpleRepositorys classes like Single<>, All<> and so on. I know why I get the error message but I need help to find a way to get around it. Error message:

System.InvalidCastException: Unable to cast object of type 'SubSonic.DomainObjects.User' to type 'Core.DomainObjects.IUser'.

Code:
public IUser FindById(int id) {
var user = _repository.Single<User>(x => x.Id == id);
return (IUser)user;
}

As you can see I have tried to make User to IUser order to work when I want to add data, but without success. What can I do to make this work?

Thank you,
Timmie

Was it helpful?

Solution

I don't think subsonic is the problem in this situation. This code will work:

namespace Core.Objects
{
    public interface ICustomer
    {
        int CustomerID { get; set; }
        string Name { get; set; }
    }

}

Code for the actual class:

namespace Business.Entities
{
        public class Customer: Core.Objects.ICustomer
        {
            public int CustomerID { get; set; }

            [SubSonicStringLength(50)]
            public string Name { get; set; }
        }
}

And finally, the function to get the customer:

private static ICustomer CustomerByID(int id)
{
      var repos = new SimpleRepository("Test", SimpleRepositoryOptions.None);
      var customer = repos.Single<Customer>(c => c.CustomerID == id);

      return (ICustomer) customer;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top