none
POCO with old domain model RRS feed

  • Question

  • I have a old domain model like this:

    public partial class Master
    {
        private int _id;
        public virtual int Id
        {
            get { 
     		return _id; 
     	}
            set { 
                    _id = value;
     	}
        }
    
        private string _desc;
        public virtual string Desc
        {
            get { 
     		return _desc; 
     	}
            set { 
                    _desc = value;
     	}
        }
    
        private IEnumerable<Detail> _details;
        public virtual IEnumerable<Detail> Details
        {
            get { 
     		return _details; 
     	}
            set { 
                    _details = value;
     	}
        }   
    }
    
    public partial class Detail
    {
        private int _id;
        public virtual int Id
        {
            get { 
     		return _id; 
     	}
            set { 
                    _id = value;
     	}
        }
    
        private string _desc;
        public virtual string Desc
        {
            get { 
     		return _desc; 
     	}
            set { 
                    _desc = value;
     	}
        }
    }
    
    
    

    In my DAL I query the context with linq to entities in this way:

    public override IQueryable<DomainModel.Master> ReadAll() 
    {
        IQueryable<DomainModel.Master> result = null;
        result = from a in this.Context.Master
              select new DomainModel.Master {
                   Id = a.Id,
                   Desc = a.Desc,
                   Details = (from b in a.Details
                            select new DomainModel.Details {
                                Id = b.Id,
                                Desc = b.Desc
                             })
                    };
         return result;
    }
    

    where this.Context.Master derive from EntityObject and is generated by EntityModelCodeGenerator.


    After in my biz I enumerate the IQueryable.

    Now I want to use POCO and I want to fill directly my Domain Model. POCO needs ICollection for the association.

    How can I use my old domain model with POCO?


    Denis Grilli
    Tuesday, September 20, 2011 8:42 AM

Answers

  • Hi Denis,

    According to your description, you don't want to modify your old models, I think you can use partial class to add properties to Detail class:

    namespace Test.Entities
    {
    	public partial class Vacation
    	{
    		public int VacationId { get; set; }
    		public int Days { get; set; }
    	
    	}
        public partial class Vacation
        {
            public int EId { get; set; }
            public virtual Employee Employee { get; set; }
        }
    }
    	public partial class Employee
    	{
    	    public Employee()
    		{
    			this.Vacations = new List<Vacation>();
    		}
    
    		public int EmployeeId { get; set; }
    		public string Name { get; set; }
    		public virtual ICollection<Vacation> Vacations { get; set; }
    	}
       
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 9:03 AM
    Moderator

All replies

  • Hi Denis.

    To use your old domain model (namely Master and Detail classes), you need to perform the following steps:

    1. Here is the modified version of Detail class (added 2 properties MasterId and Master):

    public partial class Detail
    {
    	private int _id;
    	public virtual int Id
    	{
    		get
    		{
    			return _id;
    		}
    		set
    		{
    			_id = value;
    		}
    	}
    
    	private string _desc;
    	public virtual string Desc
    	{
    		get
    		{
    			return _desc;
    		}
    		set
    		{
    			_desc = value;
    		}
    	}
    
    	public int MasterId { get; set; }
    	public Master Master { get; set; }
    
    }
    
    


    2. modified Master class (changed type of Desc / _desc to ICollection<Detail> ) :

    public partial class Master
    {
    	private int _id;
    	public virtual int Id
    	{
    		get
    		{
    			return _id;
    		}
    		set
    		{
    			_id = value;
    		}
    	}
    
    	private string _desc;
    	public virtual string Desc
    	{
    		get
    		{
    			return _desc;
    		}
    		set
    		{
    			_desc = value;
    		}
    	}
    
    	private ICollection<Detail> _details;
    	public virtual ICollection<Detail> Details
    	{
    		get
    		{
    			return _details;
    		}
    		set
    		{
    			_details = value;
    		}
    	}
    }
    
    

    3. create new context class that inherits from ObjectContext:

    public class MyContext : ObjectContext
    {
    	public ObjectSet<Master> Masters { get; set; }
    	public ObjectSet<Detail> Details { get; set; }
    
    	public MyContext()
    		: base("name=yourconnectionstring")
    	{
    		this.Masters = CreateObjectSet<Master>();
    		this.Details = CreateObjectSet<Detail>();
    	}
    }
    
    


    , where 'yourconnectionstring' is the name of the entry in connectionstrings section of your configuration file, that holds information on connection string of your Model.

    3. write method that returns all of your entities

    public IQueryable<Master> ReadAll()
    {
    	return Context.Masters.Include("Details");
    }
    
    
    , where Context is an instance of MyContext type .

     

    Hope this help

    Tuesday, September 20, 2011 10:37 AM
  • This no help because I can't modify my domain model. I have to keep my collections with IEnumerable in my domain model.

    Other suggestions?

    Thanks


    Denis Grilli
    Tuesday, September 20, 2011 10:50 AM
  • Nobody have a solution for me?
    Denis Grilli
    Wednesday, September 21, 2011 7:57 AM
  • Hi Denis,

    According to your description, you don't want to modify your old models, I think you can use partial class to add properties to Detail class:

    namespace Test.Entities
    {
    	public partial class Vacation
    	{
    		public int VacationId { get; set; }
    		public int Days { get; set; }
    	
    	}
        public partial class Vacation
        {
            public int EId { get; set; }
            public virtual Employee Employee { get; set; }
        }
    }
    	public partial class Employee
    	{
    	    public Employee()
    		{
    			this.Vacations = new List<Vacation>();
    		}
    
    		public int EmployeeId { get; set; }
    		public string Name { get; set; }
    		public virtual ICollection<Vacation> Vacations { get; set; }
    	}
       
    

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 22, 2011 9:03 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 26, 2011 3:15 AM
    Moderator