Where to convert business model to view model?

Repositories should return domain models, not view models. As far as the mapping between the models and the view models is concerned, personally I use AutoMapper so I have a separate mapping layer but this layer is called from the controller. Here’s how a typical GET controller action might look like: public ActionResult Foo(int id) … Read more

Using Simple Injector with Unit Of Work & Repository Pattern in Windows Form

The problem you have is the difference in lifestyles between your service, repository, unitofwork and dbcontext. Because the MemberRepository has a Singleton lifestyle, Simple Injector will create one instance which will be reused for the duration of the application, which could be days, even weeks or months with a WinForms application. The direct consequence from … Read more

Looking for a Ninject scope that behaves like InRequestScope

UPDATE: This approach works against NuGet current, but relies in an anomaly in the InCallscope implementation which has been fixed in the current Unstable NuGet packages. I’ll be tweaking this answer in a few days to reflect the best approach after some mulling over. NB the high level way of structuring stuff will stay pretty … Read more

Generate POCO classes in different project to the project with Entity Framework model

Actually the T4 templates in EF 4.0 were designed with this scenario in mind 🙂 There are 2 templates: One for the Entities themselves (i.e. ModelName.tt) One for the ObjectContext (i.e. ModelName.Context.tt) You should put the ModelName.tt file in you POCO project, and just change the template to point to the EDMX file in the … Read more

DDD – the rule that Entities can’t access Repositories directly

There’s a bit of a confusion here. Repositories access aggregate roots. Aggregate roots are entities. The reason for this is separation of concerns and good layering. This doesn’t make sense on small projects, but if you’re on a large team you want to say, “You access a product through the Product Repository. Product is an … Read more

Is UnitOfWork and GenericRepository Pattern redundant In EF 4.1 code first?

This is duplicate of many topics already discussed on SO but I agree that some of them can be hard to find because they are nested in other question What’s the point of Generic repository in EF 4.1 Challenges with testable and mockable code in EF Another question about challenges with mocking EF code Implementing … Read more

NOT using repository pattern, use the ORM as is (EF)

I’ve gone down many paths and created many implementations of repositories on different projects and… I’ve thrown the towel in and given up on it, here’s why. Coding for the exception Do you code for the 1% chance your database is going to change from one technology to another? If you’re thinking about your business’s … Read more

When to use PerThreadLifetimeManager?

The Per Thread Lifetime is a very dangerous lifestyle and in general you should not use it in your application, especially web applications. This lifestyle should be considered dangerous, because it is very hard to predict what the actual lifespan of a thread is. When you create and start a thread using new Thread().Start(), you’ll … Read more

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Edit: Original answer used Find instead of Local.SingleOrDefault. It worked in combination with @Juan’s Save method but it could cause unnecessary queries to database and else part was probably never executed (executing the else part would cause exception because Find already queried the database and hadn’t found the entity so it could not be updated). … Read more