質問

I'm designing an object model to use in a web application.

The model contains users. Each user has sessions. Each session contains records and events for that session. What I need to do is persist the data (looks like mongodb, or similar) How can I keep this code as the API to my database, the consumer of the API will not need to know how it is stored. And it will "magically" work just the same.

Thanks!

Here is the design I am using now.

public class User
    {
        public int id {get; set;}
        public DateTime EnrollDate { get; set; }
        public string Udid { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public HashSet<Session> Sessions { get; set; }
        public HashSet<Session> GetSessions()
        {
            if (Sessions == null)
            {
                Sessions = new HashSet<Session>();
            }
            return Sessions;
        } 
    }

public class Session
    {
        public string SessionId { get; set; }
        public DateTime Time { get; set; }
        public JObject Parameters { get; set; }
        private List<Event> Events { get; set; } 
        private List<Record> Records { get; set; }
        public List<Event> GetEvents()
        {
            if (Events == null)
            {
                Events = new List<Event>();
            }
            return Events;
        }
        public List<Record> GetRecords()
        {
            if (Records == null)
            {
                Records = new List<Record>();
            }
            return Records;
        }
    }
 public class Record
    {
        public Record()
        {
            RecordId = Guid.NewGuid().ToString();
            CreatedAt = DateTime.Now;
        }
        public string Name { get; set; }
        public string RecordId { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime Time { get; set; }
        public JObject Data { get; set; }
    }

 public class Event
    {
        public Event()
        {
            EventId = Guid.NewGuid().ToString();
            CreatedAt = DateTime.Now;
        }
        public string EventId { get; set; }
        public string Name { get; set; }
        public DateTime Time { get; set; }
        private DateTime CreatedAt { get; set; }
        public JObject Parameters { get; set; }
    }
役に立ちましたか?

解決

I would suggest you read up the repository pattern. It allows you code to be written without having to consider the concrete implementations of the data access up front.

I would build these Interfaces into there own assembly along with a repository interface that defines the contract for retrieving data from the data store using the interfaces you have defined above.

Your application would use purely the interface definitions for data access and you can inject the concrete implementations of your repository and data objects using Unity or such like.

Also this will aid in unit testing your code as you could inject a test repository for testing business objects for example

See here for an example.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top