Solved

adding an interface to my code generated classes e.f 4.1

Posted on 2012-03-17
5
325 Views
Last Modified: 2012-06-27
Hi,

I have created a load of classes from my .edmx using add code generation item.

I want my classes to implement an interface but to do this I have had to create a partial class.

public partial class Costume : IEntity,IDisposable
{

This would mean that for every class coming out of my model I would also need to create a partial class within my app.

Is this normal or is there a better way of doing things?

I'd appreciate a swfit reply on this so I don't go too far in the wrong direction.

Thanks in advance.

One other thing to note, I know I could amend the generated classes but then if I have to re-generate for some reason I would lose my changes....
0
Comment
Question by:scm0sml
  • 2
  • 2
5 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 250 total points
ID: 37732610
basically you need to use POCO and Self tracking entities feature of EF.
Here is the msdn article series on the same:-
http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx
0
 

Author Comment

by:scm0sml
ID: 37732971
Haven't I already done that?

I have ended up with a load of POCO using the add code generation item which created my classes from the model.

My question point is that if I implement my interfaces on those classes and then in the future add more fields to my database hence to my model and then want to re-generate my classes from the model in add my new fields as properties then I would lose all my implemented interfaces....?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37733066
don't generate classes from your model or edmx file.
Write them manually or using T4 template.

This allows you to make changes to your entities without worrying about updating any edmx file.

Also i would suggest you go through the concepts of Domain Driven Design to better understand the working of Domain Entities and how they must be represented in your application....
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 37733576
My advice will depend on whether you are expecting a lot of changes between your interface implementation code in your underling model. If this value is small then doing the partial class implementation is OK,  your new implementation must be on a separate class(file) in your application.
e.g if your generated class declaration is:
public partial class Costume : IEntity,IDisposable

then your new class will look like:
public partial class Costume : IEntity,IDisposable, IMyNewinterface

As you can see  even if you regenerate the entity class your implementation is only affected if you change properties that your new class is accessing it.

Note:  you can use extension methods (http://msdn.microsoft.com/en-us/library/bb383977.aspx) but  you still need to check that modifications to the entity classes are not breaking your extension methods
0
 

Author Comment

by:scm0sml
ID: 37736618
Hi mas_oz2003,

This line:
"e.g if your generated class declaration is:
public partial class Costume : IEntity,IDisposable"

How would the generated class get the interface implementiations or do you mean I would have added them manually?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now